結果

問題 No.307 最近色塗る問題多くない?
ユーザー yun_appyun_app
提出日時 2017-04-05 14:50:11
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,246 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 74,324 KB
実行使用メモリ 63,848 KB
最終ジャッジ日時 2023-09-22 20:06:54
合計ジャッジ時間 9,142 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,400 KB
testcase_01 AC 44 ms
49,940 KB
testcase_02 AC 44 ms
47,552 KB
testcase_03 AC 45 ms
49,460 KB
testcase_04 AC 128 ms
51,516 KB
testcase_05 AC 53 ms
50,524 KB
testcase_06 AC 61 ms
49,616 KB
testcase_07 AC 845 ms
61,808 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 #

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
    private static final int[] dx = { 0, 1, 0, -1};
    private static final int[] dy = {-1, 0, 1, 0};
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] lines = br.readLine().split(" ");
        int H = Integer.parseInt(lines[0]);
        int W = Integer.parseInt(lines[1]);
        
        int[][] map = new int[H][W];
        for(int i=0;i<H;i++){
            lines = br.readLine().split(" ");
            for(int j=0;j<W;j++){
                map[i][j] = Integer.parseInt(lines[j]);
            }
        }
        
        int Q = Integer.parseInt(br.readLine());
        for(int i=0;i<Q;i++){
            lines = br.readLine().split(" ");
            paint(map
                 ,Integer.parseInt(lines[0])-1
                 ,Integer.parseInt(lines[1])-1
                 ,Integer.parseInt(lines[2]));
        }
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                System.out.print(map[i][j]);
                System.out.print(j!=W-1?' ':'\n');
            }
        }
    }
    
    private static void paint(int[][] map,int h,int w,int color){
        if(map[h][w] == color){
            return;
        }else{
            map[h][w] = color;
        }
        Stack<Pair> stack = new Stack<Pair>();
        stack.push(new Pair(h,w));
        while(!stack.isEmpty()){
            Pair now = stack.pop();
            for(int i=0;i<4;i++){
                try{
                    int n_h = now.h+dx[i];
                    int n_w = now.w+dy[i];
                    if(map[n_h][n_w] != color){
                        map[n_h][n_w] = color;
                        stack.push(new Pair(n_h,n_w));
                    }
                }catch(Exception e){}
            }
        }
    }
    private static class Pair{
        public int h;
        public int w;
        public Pair(int h,int w){
            this.h = h;
            this.w = w;
        }
    }
}
0