結果

問題 No.307 最近色塗る問題多くない?
ユーザー GrenacheGrenache
提出日時 2015-11-27 23:30:55
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,028 bytes
コンパイル時間 3,791 ms
コンパイル使用メモリ 75,648 KB
実行使用メモリ 67,920 KB
最終ジャッジ日時 2023-10-12 00:55:57
合計ジャッジ時間 12,098 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
56,356 KB
testcase_01 AC 111 ms
55,948 KB
testcase_02 AC 112 ms
55,524 KB
testcase_03 AC 113 ms
55,724 KB
testcase_04 AC 166 ms
56,056 KB
testcase_05 AC 117 ms
55,352 KB
testcase_06 AC 129 ms
55,584 KB
testcase_07 AC 1,205 ms
66,588 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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 {

    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();
        for (int tcase = 0; tcase < q; tcase++) {
        	int r = sc.nextInt() - 1;
        	int c = sc.nextInt() - 1;
        	int x = sc.nextInt();

        	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;
        	while (!qr.isEmpty()) {
        		int cr = qr.remove();
        		int cc = qc.remove();
        		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);
        		}
        	}
        }

        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