結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
41,364 KB
testcase_01 AC 116 ms
41,520 KB
testcase_02 AC 106 ms
40,220 KB
testcase_03 AC 109 ms
50,988 KB
testcase_04 AC 120 ms
41,600 KB
testcase_05 AC 117 ms
41,344 KB
testcase_06 AC 120 ms
41,456 KB
testcase_07 AC 103 ms
40,308 KB
testcase_08 AC 112 ms
41,468 KB
testcase_09 AC 113 ms
41,192 KB
testcase_10 AC 116 ms
41,388 KB
testcase_11 AC 102 ms
39,848 KB
testcase_12 AC 100 ms
40,240 KB
testcase_13 AC 119 ms
41,528 KB
testcase_14 AC 116 ms
41,316 KB
testcase_15 AC 115 ms
41,464 KB
testcase_16 AC 116 ms
41,160 KB
testcase_17 AC 117 ms
41,308 KB
testcase_18 AC 117 ms
41,240 KB
testcase_19 AC 120 ms
41,680 KB
testcase_20 AC 118 ms
41,368 KB
testcase_21 AC 113 ms
41,480 KB
testcase_22 AC 111 ms
40,960 KB
testcase_23 AC 121 ms
41,192 KB
testcase_24 AC 1,249 ms
59,300 KB
testcase_25 AC 1,297 ms
59,180 KB
testcase_26 AC 1,300 ms
59,248 KB
testcase_27 AC 1,256 ms
67,052 KB
testcase_28 AC 1,287 ms
58,956 KB
testcase_29 AC 4,996 ms
70,424 KB
testcase_30 AC 4,760 ms
70,628 KB
testcase_31 AC 4,999 ms
70,852 KB
testcase_32 AC 4,766 ms
70,616 KB
testcase_33 AC 4,693 ms
70,984 KB
testcase_34 AC 4,722 ms
70,580 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;
		                    deq.add(x - 1);
		                    deq.add(x + 1);
		                    deq.add(x + ww);
		                    deq.add(x - ww);
		                }
		            }
		        }
		    }
		}
		System.out.println(count);
    }
}
0