結果

問題 No.697 池の数はいくつか
ユーザー htensaihtensai
提出日時 2020-02-14 17:36:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,728 ms / 6,000 ms
コード長 1,345 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 78,548 KB
実行使用メモリ 81,332 KB
最終ジャッジ日時 2024-04-25 20:43:27
合計ジャッジ時間 40,541 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
54,132 KB
testcase_01 AC 105 ms
52,896 KB
testcase_02 AC 115 ms
54,052 KB
testcase_03 AC 113 ms
53,384 KB
testcase_04 AC 117 ms
54,156 KB
testcase_05 AC 110 ms
52,736 KB
testcase_06 AC 108 ms
53,208 KB
testcase_07 AC 110 ms
53,024 KB
testcase_08 AC 104 ms
52,996 KB
testcase_09 AC 116 ms
54,024 KB
testcase_10 AC 114 ms
53,544 KB
testcase_11 AC 117 ms
54,380 KB
testcase_12 AC 107 ms
53,076 KB
testcase_13 AC 122 ms
54,484 KB
testcase_14 AC 119 ms
54,028 KB
testcase_15 AC 102 ms
52,968 KB
testcase_16 AC 102 ms
52,776 KB
testcase_17 AC 118 ms
54,004 KB
testcase_18 AC 106 ms
53,052 KB
testcase_19 AC 122 ms
54,108 KB
testcase_20 AC 117 ms
53,192 KB
testcase_21 AC 106 ms
54,432 KB
testcase_22 AC 116 ms
53,884 KB
testcase_23 AC 120 ms
53,348 KB
testcase_24 AC 1,206 ms
68,948 KB
testcase_25 AC 1,200 ms
69,348 KB
testcase_26 AC 1,119 ms
69,400 KB
testcase_27 AC 1,136 ms
68,908 KB
testcase_28 AC 1,155 ms
69,020 KB
testcase_29 AC 4,693 ms
81,144 KB
testcase_30 AC 4,486 ms
81,048 KB
testcase_31 AC 4,728 ms
81,332 KB
testcase_32 AC 4,507 ms
80,816 KB
testcase_33 AC 4,448 ms
81,164 KB
testcase_34 AC 4,402 ms
81,168 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();
		int ww = w + 2;
		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 * ww + j);
		            while (deq.size() > 0) {
		                int x = deq.poll();
		                if (field[x / ww][x % ww]) {
		                    field[x / ww][x % ww] = false;
		                    if (field[x / ww][x % ww - 1]) {
		                        deq.add(x - 1);
		                    }
		                    if (field[x / ww][x % ww + 1]) {
		                        deq.add(x + 1);
		                    }
		                    if (field[x / ww - 1][x % ww]) {
		                        deq.add(x - ww);
		                    }
		                    if (field[x / ww + 1][x % ww]) {
		                        deq.add(x + ww);
		                    }
		                }
		            }
		        }
		    }
		}
		System.out.println(count);
    }
}
0