結果

問題 No.179 塗り分け
ユーザー masa
提出日時 2015-04-06 01:16:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 115 ms / 3,000 ms
コード長 1,240 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 66,376 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-23 14:27:07
合計ジャッジ時間 3,175 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

vector<string> board;
int h, w;

bool check(int mx, int my) {
	if (mx == 0 && my == 0) {
		return false;
	}

	auto paint = board;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (0 <= i + my && i + my < h && 0 <= j + mx && j + mx < w &&
				paint[i][j] == '#')
			{

				if (board[i][j] == board[i + my][j + mx]) {
					paint[i][j] = 'A';
					paint[i+my][j+mx] = 'B';
				}
			}
		}
	}

	int na = 0, nb = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			switch (paint[i][j]) {
				case 'A': na++; break;
				case 'B': nb++; break;
				case '#': return false;
				default: break;
			}
		}
	}

	return (na == nb && na > 0);
}

int main() {
	cin >> h >> w;

	board.assign(h, "");

	int n = 0;
	for (int i = 0; i < h; i++) {
		cin >> board[i];
		n += count(board[i].begin(), board[i].end(), '#');
	}

	if (n % 2 == 1) {
		cout << "NO" << endl;
		return 0;
	}

	int half = n / 2;

	for (int my = -h; my < h; my++) {
		for (int mx = -w; mx < w; mx++) {
			if (check(mx, my)) {
				cout << "YES" << endl;
				return 0;
			}
		}
	}

	cout << "NO" << endl;
	return 0;
}
0