結果

問題 No.179 塗り分け
ユーザー masa
提出日時 2015-04-06 01:34:54
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 58 ms / 3,000 ms
コード長 1,252 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 66,504 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-23 14:27:23
合計ジャッジ時間 2,340 ms
ジャッジサーバーID
(参考情報)
judge1 / 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';
				} else {
					return false;
				}
			}
		}
	}

	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);
}

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 == 0 || n % 2 == 1) {
		cout << "NO" << endl;
		return 0;
	}

	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