結果

問題 No.697 池の数はいくつか
ユーザー kusagame12kusagame12
提出日時 2023-11-26 13:40:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,217 ms / 6,000 ms
コード長 2,851 bytes
コンパイル時間 2,319 ms
コンパイル使用メモリ 83,292 KB
実行使用メモリ 73,704 KB
最終ジャッジ日時 2023-11-26 13:40:22
合計ジャッジ時間 15,614 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
54,764 KB
testcase_01 AC 63 ms
54,628 KB
testcase_02 AC 64 ms
54,652 KB
testcase_03 AC 73 ms
54,524 KB
testcase_04 AC 63 ms
54,652 KB
testcase_05 AC 64 ms
54,756 KB
testcase_06 AC 64 ms
54,756 KB
testcase_07 AC 64 ms
54,776 KB
testcase_08 AC 63 ms
54,512 KB
testcase_09 AC 63 ms
54,636 KB
testcase_10 AC 62 ms
54,644 KB
testcase_11 AC 62 ms
54,776 KB
testcase_12 AC 62 ms
54,516 KB
testcase_13 AC 64 ms
54,768 KB
testcase_14 AC 63 ms
54,652 KB
testcase_15 AC 63 ms
54,756 KB
testcase_16 AC 62 ms
54,764 KB
testcase_17 AC 63 ms
54,764 KB
testcase_18 AC 63 ms
52,696 KB
testcase_19 AC 63 ms
54,544 KB
testcase_20 AC 63 ms
54,656 KB
testcase_21 AC 63 ms
54,656 KB
testcase_22 AC 63 ms
54,644 KB
testcase_23 AC 64 ms
54,736 KB
testcase_24 AC 466 ms
62,956 KB
testcase_25 AC 468 ms
62,952 KB
testcase_26 AC 496 ms
62,924 KB
testcase_27 AC 493 ms
62,940 KB
testcase_28 AC 540 ms
62,964 KB
testcase_29 AC 1,190 ms
73,228 KB
testcase_30 AC 1,178 ms
73,624 KB
testcase_31 AC 1,209 ms
71,660 KB
testcase_32 AC 1,217 ms
73,664 KB
testcase_33 AC 1,216 ms
73,304 KB
testcase_34 AC 1,210 ms
73,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    
    private static int[] dy = {0,1,0,-1};
    private static int[] dx = {1,0,-1,0};
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    //訪れたことを更新するタイミングを変えたら、早くなった。
    public static void main(String[] args) throws Exception {

    	int[] h_w = readIntArray();
        int h = h_w[0];
        int w = h_w[1];
        
        boolean[][] field = new boolean[h][w];
        for(int i = 0 ; i < h ; i++){
            String[] terrains = br.readLine().split(" ");
            for(int j = 0 ;  j < w ; j++){
                if(terrains[j].equals("1")){
                    field[i][j] = true;
                }
            }
        }
            
        int ans = 0;
        for(int i = 0 ; i < h ; i++){
            for(int j = 0 ; j < w ; j++){
                    
                if(field[i][j]){
                    field = bfs(field , i , j , h , w);
                    ans++;
                }
                    
            }
        }
            
        System.out.println(ans);
		
    }
    
    private static boolean[][] bfs(boolean[][] field , int currentH , int currentW , int h , int w){
        
        ArrayDeque<Pair> todo = new ArrayDeque<>();
        //Pair p = new Pair(currentH , currentW);
        todo.addLast(new Pair(currentH , currentW));
        
        while(!todo.isEmpty()){
            
            Pair p = todo.pollFirst();
            currentH = p.getY();
            currentW = p.getX();
            
            //ここで訪れたことを更新すると、時間がかかる(TLEかMLEする)。
            //field[currentH][currentW] = false;
  
            for(int i = 0 ; i < 4 ; i++){
                
                int nextH = currentH + dy[i];
                int nextW = currentW + dx[i];
                
                if(nextH >= 0 && nextH < h && nextW >= 0 && nextW < w){
                    if(field[nextH][nextW]){
                        todo.addLast(new Pair(nextH , nextW));
                        //ここで訪れたことを更新すると、早くなった(TLEもMLEもしない)。
                        field[nextH][nextW] = false;
                    }
                }
                
            }

        }
        
        return field;
                
    }
    
    private static int[] readIntArray() throws Exception{
        
		int[] intArray = Arrays.stream(br.readLine().split(" "))
							.mapToInt(Integer::parseInt).toArray();

		return intArray;

	}

}

class Pair{
    
    int y , x;
    
    public Pair(int y  , int x){
        this.y = y;
        this.x = x;
    }
    
    public int getY(){
        return y;
    }
    
    public int getX(){
        return x;
    }
    
}
0