結果

問題 No.307 最近色塗る問題多くない?
ユーザー yun_appyun_app
提出日時 2017-04-05 15:19:30
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,591 bytes
コンパイル時間 2,789 ms
コンパイル使用メモリ 74,496 KB
実行使用メモリ 65,392 KB
最終ジャッジ日時 2023-09-22 20:08:01
合計ジャッジ時間 16,873 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 44 ms
49,416 KB
testcase_02 AC 44 ms
49,344 KB
testcase_03 WA -
testcase_04 AC 129 ms
51,684 KB
testcase_05 AC 52 ms
49,976 KB
testcase_06 AC 60 ms
50,816 KB
testcase_07 AC 272 ms
56,444 KB
testcase_08 AC 615 ms
62,696 KB
testcase_09 AC 420 ms
61,248 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 199 ms
56,556 KB
testcase_14 AC 188 ms
56,116 KB
testcase_15 AC 209 ms
56,652 KB
testcase_16 WA -
testcase_17 AC 420 ms
61,552 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 161 ms
55,856 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 621 ms
62,240 KB
testcase_33 AC 491 ms
61,904 KB
testcase_34 AC 602 ms
63,028 KB
testcase_35 AC 633 ms
63,488 KB
権限があれば一括ダウンロードができます

ソースコード

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};
    private static int H,W;
    private static int cnt;
    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(" ");
        H = Integer.parseInt(lines[0]);
        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]);
                if(map[i][j] == 0){
                    cnt++;
                }
            }
        }
        
        int Q = Integer.parseInt(br.readLine());
        int col = 0;
        for(int i=0;i<Q;i++){
            lines = br.readLine().split(" ");
            col = Integer.parseInt(lines[2]);
            if(cnt==0)continue;
            paint(map
                 ,Integer.parseInt(lines[0])-1
                 ,Integer.parseInt(lines[1])-1
                 ,col);
        }
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                System.out.print(cnt==0?col: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;
        }
        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;
                        if(map[n_h][n_w] == 0){
                            cnt++;
                        }else{
                            cnt--;
                        }
                        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