結果

問題 No.307 最近色塗る問題多くない?
ユーザー GrenacheGrenache
提出日時 2015-11-27 23:46:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,357 ms / 4,000 ms
コード長 3,432 bytes
コンパイル時間 3,878 ms
コンパイル使用メモリ 79,016 KB
実行使用メモリ 68,528 KB
最終ジャッジ日時 2023-08-14 03:49:48
合計ジャッジ時間 20,740 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
55,872 KB
testcase_01 AC 114 ms
55,900 KB
testcase_02 AC 113 ms
55,712 KB
testcase_03 AC 115 ms
56,004 KB
testcase_04 AC 162 ms
55,916 KB
testcase_05 AC 121 ms
55,696 KB
testcase_06 AC 131 ms
55,876 KB
testcase_07 AC 284 ms
63,972 KB
testcase_08 AC 481 ms
66,436 KB
testcase_09 AC 353 ms
64,852 KB
testcase_10 AC 257 ms
59,320 KB
testcase_11 AC 315 ms
59,440 KB
testcase_12 AC 117 ms
55,640 KB
testcase_13 AC 279 ms
60,324 KB
testcase_14 AC 213 ms
58,560 KB
testcase_15 AC 233 ms
58,320 KB
testcase_16 AC 230 ms
63,548 KB
testcase_17 AC 393 ms
66,316 KB
testcase_18 AC 647 ms
66,848 KB
testcase_19 AC 633 ms
66,924 KB
testcase_20 AC 177 ms
58,284 KB
testcase_21 AC 530 ms
66,728 KB
testcase_22 AC 509 ms
67,972 KB
testcase_23 AC 1,238 ms
67,136 KB
testcase_24 AC 701 ms
66,048 KB
testcase_25 AC 749 ms
66,496 KB
testcase_26 AC 1,297 ms
68,528 KB
testcase_27 AC 402 ms
61,836 KB
testcase_28 AC 526 ms
64,592 KB
testcase_29 AC 248 ms
60,060 KB
testcase_30 AC 1,357 ms
67,332 KB
testcase_31 AC 463 ms
61,276 KB
testcase_32 AC 445 ms
61,452 KB
testcase_33 AC 370 ms
65,624 KB
testcase_34 AC 450 ms
61,208 KB
testcase_35 AC 458 ms
59,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Queue;


public class Main_yukicoder307_1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int[] dr = {-1, 1, 0, 0};
        int[] dc = {0, 0, -1, 1};

        int h = sc.nextInt();
        int w = sc.nextInt();
        int[][] a = new int[h][w];
        for (int i = 0; i < h; i++) {
        	for (int j = 0; j < w; j++) {
        		a[i][j] = sc.nextInt();
        	}
        }

        int q = sc.nextInt();
        boolean flag = false;
        int retc = 0;
        for (int tcase = 0; tcase < q; tcase++) {
        	int r = sc.nextInt() - 1;
        	int c = sc.nextInt() - 1;
        	int x = sc.nextInt();

        	if (flag) {
        		retc = x;
        		continue;
        	}

        	if (x == a[r][c]) {
        		continue;
        	}

        	int cp = a[r][c];
        	Queue<Integer> qr = new ArrayDeque<Integer>();
        	Queue<Integer> qc = new ArrayDeque<Integer>();
        	qr.add(r);
        	qc.add(c);
        	a[r][c] = x;
        	int cnt = 0;
        	while (!qr.isEmpty()) {
        		int cr = qr.remove();
        		int cc = qc.remove();
        		cnt++;
        		for (int i = 0; i < dr.length; i++) {
        			int tmpr = cr + dr[i];
        			int tmpc = cc + dc[i];

        			if (tmpr < 0 || tmpr >= h || tmpc < 0 || tmpc >= w) {
        				continue;
        			}

        			if (a[tmpr][tmpc] != cp) {
        				continue;
        			}

        			a[tmpr][tmpc] = x;
        			qr.add(tmpr);
        			qc.add(tmpc);
        		}
        	}
        	if (cnt == h * w) {
        		flag = true;
        	}
        }

        if (flag) {
            for (int i = 0; i < h; i++) {
            	for (int j = 0; j < w; j++) {
            		a[i][j] = retc;
            	}
            }
        }

        for (int i = 0; i < h; i++) {
        	for (int j = 0; j < w; j++) {
        		if (j == 0) {
        			pr.printf("%d", a[i][j]);
        		} else {
        			pr.printf(" %d", a[i][j]);
        		}
        	}
        	pr.println("");
        }

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0