結果

問題 No.697 池の数はいくつか
ユーザー kusagame12kusagame12
提出日時 2023-11-25 21:28:56
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,749 bytes
コンパイル時間 3,053 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 152,452 KB
最終ジャッジ日時 2023-11-25 21:29:16
合計ジャッジ時間 19,806 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
54,900 KB
testcase_01 AC 70 ms
54,880 KB
testcase_02 AC 69 ms
54,820 KB
testcase_03 AC 68 ms
54,796 KB
testcase_04 AC 69 ms
54,780 KB
testcase_05 AC 68 ms
54,672 KB
testcase_06 AC 70 ms
54,792 KB
testcase_07 AC 70 ms
54,688 KB
testcase_08 AC 70 ms
54,752 KB
testcase_09 AC 68 ms
54,676 KB
testcase_10 AC 69 ms
54,672 KB
testcase_11 AC 69 ms
54,792 KB
testcase_12 AC 70 ms
54,772 KB
testcase_13 AC 70 ms
54,672 KB
testcase_14 AC 70 ms
54,776 KB
testcase_15 AC 69 ms
54,772 KB
testcase_16 AC 69 ms
54,788 KB
testcase_17 AC 69 ms
54,676 KB
testcase_18 AC 68 ms
54,660 KB
testcase_19 AC 70 ms
54,776 KB
testcase_20 AC 73 ms
54,884 KB
testcase_21 AC 70 ms
54,856 KB
testcase_22 AC 69 ms
54,676 KB
testcase_23 AC 69 ms
54,780 KB
testcase_24 AC 3,467 ms
152,452 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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 {

        try {

    		int[] h_w = readIntArray();
            int h = h_w[0];
            int w = h_w[1];
            
            int[][] field = new int[h][w];
            for(int i = 0 ; i < h ; i++){
                
                int[] terrains = readIntArray();
                field[i] = terrains;
                
            }
            
            boolean[][] seen = new boolean[h][w];
            LinkedList<int[]> todo = new LinkedList<>();
            int ans = 0;
            for(int i = 0 ; i < h ; i++){
                for(int j = 0 ; j < w ; j++){
                    
                    int currentH = i;
                    int currentW = j;
                    if(seen[currentH][currentW] || field[currentH][currentW] == 0){
                        continue;
                    }
                    
                    seen = bfs(field , seen , currentH , currentW , h , w , todo);
                    ans++;
                    
                }
            }
            
            System.out.println(ans);

		} catch (Exception e) {

       
		}
		
    }
    
    private static boolean[][] bfs(int[][] field , boolean[][] seen , int currentH , int currentW , int h , int w , LinkedList<int[]> todo){
     
        todo.clear();
        int[] currentH_W = {currentH , currentW};
        todo.add(currentH_W);
        
        while(!todo.isEmpty()){
            
            currentH_W = todo.pop();
            currentH = currentH_W[0];
            currentW = currentH_W[1];
            
            seen[currentH][currentW] = true;
            
            for(int i = 0 ; i < 4 ; i++){
                
                int nextH = currentH + dy[i];
                int nextW = currentW + dx[i];
                
                if (nextH < 0 || nextW < 0 || nextH >= h || nextW >= w) {
					continue;
                }
                if(seen[nextH][nextW] || field[nextH][nextW] == 0){
                    continue;
                }
                
                int[] nextH_W = new int[2];
                nextH_W[0] = nextH;
                nextH_W[1] = nextW;
                todo.add(nextH_W);
                
            }

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

		return intArray;

	}

}
0