結果

問題 No.697 池の数はいくつか
ユーザー tentententen
提出日時 2020-12-04 13:46:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,829 ms / 6,000 ms
コード長 1,344 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 78,276 KB
実行使用メモリ 81,268 KB
最終ジャッジ日時 2024-04-25 20:56:51
合計ジャッジ時間 41,341 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
54,144 KB
testcase_01 AC 107 ms
52,752 KB
testcase_02 AC 104 ms
53,916 KB
testcase_03 AC 104 ms
53,064 KB
testcase_04 AC 116 ms
54,316 KB
testcase_05 AC 102 ms
52,952 KB
testcase_06 AC 117 ms
54,488 KB
testcase_07 AC 116 ms
54,084 KB
testcase_08 AC 122 ms
54,040 KB
testcase_09 AC 117 ms
53,920 KB
testcase_10 AC 127 ms
54,300 KB
testcase_11 AC 106 ms
52,576 KB
testcase_12 AC 118 ms
53,892 KB
testcase_13 AC 116 ms
53,560 KB
testcase_14 AC 128 ms
54,164 KB
testcase_15 AC 125 ms
54,204 KB
testcase_16 AC 123 ms
53,860 KB
testcase_17 AC 118 ms
53,912 KB
testcase_18 AC 117 ms
53,992 KB
testcase_19 AC 117 ms
54,176 KB
testcase_20 AC 109 ms
52,808 KB
testcase_21 AC 119 ms
54,160 KB
testcase_22 AC 118 ms
54,124 KB
testcase_23 AC 132 ms
54,236 KB
testcase_24 AC 1,212 ms
69,088 KB
testcase_25 AC 1,189 ms
69,368 KB
testcase_26 AC 1,167 ms
69,484 KB
testcase_27 AC 1,208 ms
69,508 KB
testcase_28 AC 1,196 ms
69,468 KB
testcase_29 AC 4,725 ms
81,224 KB
testcase_30 AC 4,622 ms
81,268 KB
testcase_31 AC 4,829 ms
81,036 KB
testcase_32 AC 4,541 ms
80,036 KB
testcase_33 AC 4,556 ms
81,032 KB
testcase_34 AC 4,614 ms
81,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        boolean[][] field = new boolean[h + 2][w + 2];
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                field[i][j] = (sc.nextInt() == 1);
            }
        }
        ArrayDeque<Integer> deq = new ArrayDeque<>();
        int count = 0;
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                if (field[i][j]) {
                    count++;
                    deq.add(i * (w + 2) + j);
                    while (deq.size() > 0) {
                        int x = deq.poll();
                        int xh = x / (w + 2);
                        int xw = x % (w + 2);
                        if (!field[xh][xw]) {
                            continue;
                        }
                        field[xh][xw] = false;
                        deq.add(xh * (w + 2) + xw + 1);
                        deq.add(xh * (w + 2) + xw - 1);
                        deq.add((xh + 1) * (w + 2) + xw);
                        deq.add((xh - 1) * (w + 2) + xw);
                    }
                }
            }
        }
        System.out.println(count);
    }
}
0