結果

問題 No.697 池の数はいくつか
ユーザー tentententen
提出日時 2022-10-19 09:32:26
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,474 ms / 6,000 ms
コード長 2,260 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 74,540 KB
実行使用メモリ 67,352 KB
最終ジャッジ日時 2023-09-11 23:59:27
合計ジャッジ時間 18,936 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
33,388 KB
testcase_01 AC 41 ms
33,304 KB
testcase_02 AC 43 ms
33,320 KB
testcase_03 AC 41 ms
33,388 KB
testcase_04 AC 41 ms
33,480 KB
testcase_05 AC 40 ms
33,460 KB
testcase_06 AC 42 ms
33,828 KB
testcase_07 AC 40 ms
33,712 KB
testcase_08 AC 42 ms
33,448 KB
testcase_09 AC 42 ms
33,808 KB
testcase_10 AC 42 ms
33,956 KB
testcase_11 AC 41 ms
33,756 KB
testcase_12 AC 42 ms
33,372 KB
testcase_13 AC 44 ms
33,420 KB
testcase_14 AC 42 ms
33,356 KB
testcase_15 AC 42 ms
33,336 KB
testcase_16 AC 42 ms
33,404 KB
testcase_17 AC 42 ms
33,604 KB
testcase_18 AC 42 ms
33,476 KB
testcase_19 AC 41 ms
33,648 KB
testcase_20 AC 43 ms
33,332 KB
testcase_21 AC 41 ms
33,464 KB
testcase_22 AC 42 ms
33,456 KB
testcase_23 AC 43 ms
47,820 KB
testcase_24 AC 457 ms
57,732 KB
testcase_25 AC 485 ms
43,932 KB
testcase_26 AC 465 ms
42,944 KB
testcase_27 AC 465 ms
44,216 KB
testcase_28 AC 471 ms
43,928 KB
testcase_29 AC 1,474 ms
56,740 KB
testcase_30 AC 1,279 ms
67,352 KB
testcase_31 AC 1,471 ms
55,452 KB
testcase_32 AC 1,240 ms
66,816 KB
testcase_33 AC 1,182 ms
55,412 KB
testcase_34 AC 1,241 ms
55,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        boolean[][] isWater = new boolean[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                isWater[i][j] = (sc.nextInt() == 1);
            }
        }
        ArrayDeque<Integer> current = new ArrayDeque<>();
        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (!isWater[i][j]) {
                    continue;
                }
                ans++;
                current.add(i * w + j);
                while (current.size() > 0) {
                    int x = current.poll();
                    int r = x / w;
                    int c = x % w;
                    if (!isWater[r][c]) {
                        continue;
                    }
                    isWater[r][c] = false;
                    if (r > 0) {
                        current.add(x - w);
                    }
                    if (r < h - 1) {
                        current.add(x + w);
                    }
                    if (c > 0) {
                        current.add(x - 1);
                    }
                    if (c < w - 1) {
                        current.add(x + 1);
                    }
                }
            }
        }
        System.out.println(ans);
    }
    
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0