結果

問題 No.697 池の数はいくつか
ユーザー tentententen
提出日時 2020-12-04 14:00:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 5,252 ms / 6,000 ms
コード長 1,660 bytes
コンパイル時間 3,640 ms
コンパイル使用メモリ 78,316 KB
実行使用メモリ 81,576 KB
最終ジャッジ日時 2024-04-25 20:59:05
合計ジャッジ時間 27,125 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
54,124 KB
testcase_01 AC 131 ms
53,920 KB
testcase_02 AC 134 ms
53,872 KB
testcase_03 AC 135 ms
54,340 KB
testcase_04 AC 137 ms
53,916 KB
testcase_05 AC 135 ms
54,272 KB
testcase_06 AC 134 ms
54,228 KB
testcase_07 AC 134 ms
54,084 KB
testcase_08 AC 132 ms
54,460 KB
testcase_09 AC 131 ms
54,360 KB
testcase_10 AC 135 ms
54,112 KB
testcase_11 AC 137 ms
54,092 KB
testcase_12 AC 134 ms
54,228 KB
testcase_13 AC 137 ms
54,288 KB
testcase_14 AC 135 ms
54,004 KB
testcase_15 AC 132 ms
54,020 KB
testcase_16 AC 134 ms
54,280 KB
testcase_17 AC 130 ms
54,244 KB
testcase_18 AC 136 ms
54,128 KB
testcase_19 AC 135 ms
53,912 KB
testcase_20 AC 134 ms
53,884 KB
testcase_21 AC 134 ms
54,396 KB
testcase_22 AC 131 ms
54,064 KB
testcase_23 AC 137 ms
54,180 KB
testcase_24 AC 1,343 ms
68,456 KB
testcase_25 AC 1,389 ms
69,088 KB
testcase_26 AC 1,335 ms
69,268 KB
testcase_27 AC 1,341 ms
69,216 KB
testcase_28 AC 1,282 ms
69,284 KB
testcase_29 AC 5,252 ms
81,292 KB
testcase_30 AC 4,981 ms
80,676 KB
testcase_31 AC 5,176 ms
81,576 KB
testcase_32 AC 5,037 ms
81,408 KB
testcase_33 AC 5,054 ms
80,100 KB
testcase_34 AC 5,073 ms
81,360 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;
                        if (field[xh][xw + 1]) {
                            deq.add(xh * (w + 2) + xw + 1);
                        }
                        if (field[xh][xw - 1]) {
                            deq.add(xh * (w + 2) + xw - 1);
                        }
                        if (field[xh + 1][xw]) {
                            deq.add((xh + 1) * (w + 2) + xw);
                        }
                        if (field[xh - 1][xw]) {
                            deq.add((xh - 1) * (w + 2) + xw);
                        }
                    }
                }
            }
        }
        System.out.println(count);
    }
}
0