結果

問題 No.179 塗り分け
ユーザー kenkoooo
提出日時 2015-04-18 00:56:53
言語 Java
(openjdk 23)
結果
AC  
実行時間 303 ms / 3,000 ms
コード長 1,372 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 77,864 KB
実行使用メモリ 57,064 KB
最終ジャッジ日時 2024-07-23 14:29:08
合計ジャッジ時間 12,290 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		char[][] map = new char[H][];
		for (int i = 0; i < map.length; i++) {
			map[i] = sc.next().toCharArray();
		}

		int cnt = 0;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (map[i][j] == '#') {
					cnt++;
				}
			}
		}
		if (cnt % 2 == 1 || cnt == 0) {
			System.out.println("NO");
			return;
		}

		for (int x = 1 - H; x < H; x++) {
			for (int y = 1 - W; y < W; y++) {
				if (x == 0 && y == 0) {
					continue;
				}
				if (check(x, y, map)) {
					System.out.println("YES");
					return;
				}
			}
		}
		System.out.println("NO");

	}

	static boolean check(int x, int y, char[][] origin) {
		char[][] map = new char[origin.length][origin[0].length];
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				map[i][j] = origin[i][j];
			}
		}

		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				int h = i + x;
				int w = j + y;
				if (map[i][j] == '#') {
					if (h >= map.length || w >= map[i].length || h < 0 || w < 0 || map[h][w] != '#') {
						return false;
					}
					map[i][j] = '.';
					map[h][w] = '.';
				}
			}
		}
		return true;
	}
}
0