結果
問題 | No.307 最近色塗る問題多くない? |
ユーザー |
|
提出日時 | 2015-11-27 23:46:20 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,317 ms / 4,000 ms |
コード長 | 3,432 bytes |
コンパイル時間 | 6,420 ms |
コンパイル使用メモリ | 78,868 KB |
実行使用メモリ | 56,216 KB |
最終ジャッジ日時 | 2024-11-21 21:54:11 |
合計ジャッジ時間 | 20,590 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
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);}}}