結果

問題 No.307 最近色塗る問題多くない?
ユーザー htensai
提出日時 2020-05-12 19:25:29
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,978 ms / 4,000 ms
コード長 2,327 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 79,044 KB
実行使用メモリ 64,328 KB
最終ジャッジ日時 2024-09-13 20:26:07
合計ジャッジ時間 25,288 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		boolean[][] field = new boolean[h][w];
		int tCount = 0;
		int fCount = 0;
		for (int i = 0; i < h; i++) {
		    for (int j = 0; j < w; j++) {
		        field[i][j] = (sc.nextInt() == 1);
		        if (field[i][j]) {
		            tCount++;
		        } else {
		            fCount++;
		        }
		    }
		}
		ArrayDeque<Path> deq = new ArrayDeque<>();
		int q = sc.nextInt();
		boolean last = true;
		for (int i = 0; i < q; i++) {
		    if (tCount > 0 && fCount > 0) {
		        deq.add(new Path(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt() == 1));
		    } else {
		        sc.nextInt();
		        sc.nextInt();
		        last = (sc.nextInt() == 1);
		    }
		    while (deq.size() > 0) {
		        Path p = deq.poll();
		        if (p.row < 0 || p.row >= h || p.col < 0 || p.col >= w) {
		            continue;
		        }
		        if (field[p.row][p.col] == p.type) {
		            continue;
		        }
		        field[p.row][p.col] = p.type;
		        last = p.type;
		        if (p.type) {
		            tCount++;
		            fCount--;
		        } else {
		            tCount--;
		            fCount++;
		        }
		        deq.add(new Path(p.row - 1, p.col, p.type));
		        deq.add(new Path(p.row + 1, p.col, p.type));
		        deq.add(new Path(p.row, p.col - 1, p.type));
		        deq.add(new Path(p.row, p.col + 1, p.type));
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < h; i++) {
		    for (int j = 0; j < w; j++) {
		        if (j > 0) {
		            sb.append(" ");
		        }
		        if (tCount > 0 && fCount > 0) {
    		        if (field[i][j]) {
    		            sb.append("1");
    		        } else {
    		            sb.append("0");
    		        }
		        } else {
		            if (last) {
		                sb.append("1");
		            } else {
		                sb.append("0");
		            }
		        }
		    }
		    sb.append("\n");
		}
		System.out.print(sb);
	}
	
	static class Path {
	    int row;
	    int col;
	    boolean type;

	    public Path(int row, int col, boolean type) {
	        this.row = row;
	        this.col = col;
	        this.type = type;
	    }
	}
}
0