結果

問題 No.179 塗り分け
コンテスト
ユーザー shin
提出日時 2026-06-18 17:19:58
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 194 ms / 3,000 ms
コード長 2,176 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,740 ms
コンパイル使用メモリ 83,904 KB
実行使用メモリ 48,256 KB
最終ジャッジ日時 2026-06-18 17:20:10
合計ジャッジ時間 10,620 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class No179 {

	public static void main(String[] args) throws IOException {
		
		String[] text = readStr();
		
		int h = Integer.parseInt(text[0].split(" ")[0]);
		int w = Integer.parseInt(text[0].split(" ")[1]);
		
		String[][] strings = new String[h][w];
		
		for(int i = 0;i < h;i++) {
			strings[i]= text[i+1].split(""); 
		}
		
		int sh = -1,sw = -1;
		
		for(int i = 0;i < h;i++) {
			for(int j = 0;j < w;j++) {
				if("#".equals(strings[i][j])){
					sh = i;
					sw = j;
					break;
				}
			}
			if(Math.min(sh, sw)>=0){ 
				break;
			}
		}
		
		if(Math.min(sh, sw) == -1) {
			System.out.println("NO");
			System.exit(0);
		}
		
		boolean ans = false;
		
		for(int i = 0;i < h;i++) {
			for(int j = -1*w+1;j < w;j++) {
				if(i == 0 && j <= 0) {
					continue;
				}
				ans = checkPaint(i, j, strings, sh);
				
				if(ans) {
					break;
				}
			}
			if(ans) {
				break;
			}
		}
		
		if(ans) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}

		
	}
	
	public static boolean checkPaint(int h,int w,String[][] str,int sh) {
		
		boolean ans = true;
		
		String[][] strings = new String[str.length][str[0].length];
		for (int i = 0;i < str.length;i++) {
			for(int j = 0;j < str[0].length;j++) {
				strings[i][j]= str[i][j]; 
			}
		}
		
		for(int i = sh;i < strings.length;i++) {
			for(int j = 0;j < strings[0].length;j++) {
				
				if("#".equals(strings[i][j])){
					if(i + h >= strings.length || j + w < 0 || j + w >= strings[0].length) {
						ans = false;
						break;
					}
					
					if(!"#".equals(strings[i+h][j+w])) {
						ans = false;
						break;
					}
					strings[i+h][j+w] = ".";

				}
				
			}
			if(!ans) {
				break;
			}
		}
		return ans;
	}
	
	public static String[] readStr() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> list = new ArrayList<>();

		do {
			list.add(br.readLine());
		}while(br.ready());

		br.close();

		String[] text = new String[list.size()];
		list.toArray(text);

		return text;

	}

}
0