結果

問題 No.697 池の数はいくつか
ユーザー Daigo HIROOKADaigo HIROOKA
提出日時 2018-06-23 03:25:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,994 ms / 6,000 ms
コード長 1,212 bytes
コンパイル時間 3,930 ms
コンパイル使用メモリ 79,132 KB
実行使用メモリ 124,712 KB
最終ジャッジ日時 2024-04-25 20:12:25
合計ジャッジ時間 43,496 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
52,988 KB
testcase_01 AC 105 ms
53,272 KB
testcase_02 AC 104 ms
53,044 KB
testcase_03 AC 112 ms
53,836 KB
testcase_04 AC 102 ms
53,000 KB
testcase_05 AC 114 ms
53,996 KB
testcase_06 AC 103 ms
53,148 KB
testcase_07 AC 110 ms
54,072 KB
testcase_08 AC 112 ms
53,996 KB
testcase_09 AC 111 ms
53,872 KB
testcase_10 AC 115 ms
54,000 KB
testcase_11 AC 117 ms
54,428 KB
testcase_12 AC 108 ms
54,256 KB
testcase_13 AC 116 ms
54,156 KB
testcase_14 AC 111 ms
53,948 KB
testcase_15 AC 112 ms
54,260 KB
testcase_16 AC 111 ms
53,920 KB
testcase_17 AC 118 ms
54,288 KB
testcase_18 AC 112 ms
54,056 KB
testcase_19 AC 115 ms
54,100 KB
testcase_20 AC 119 ms
54,388 KB
testcase_21 AC 110 ms
54,088 KB
testcase_22 AC 113 ms
54,280 KB
testcase_23 AC 117 ms
54,056 KB
testcase_24 AC 1,167 ms
73,632 KB
testcase_25 AC 1,189 ms
73,636 KB
testcase_26 AC 1,245 ms
73,712 KB
testcase_27 AC 1,236 ms
73,884 KB
testcase_28 AC 1,239 ms
73,828 KB
testcase_29 AC 4,994 ms
124,712 KB
testcase_30 AC 4,683 ms
122,780 KB
testcase_31 AC 4,979 ms
120,964 KB
testcase_32 AC 4,859 ms
123,028 KB
testcase_33 AC 4,758 ms
122,740 KB
testcase_34 AC 4,843 ms
122,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No697{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		int H = sc.nextInt();
		int W = sc.nextInt();
		int[] dx = {0, 1, 0, -1};
		int[] dy = {-1, 0, 1, 0};

		int[][] a = new int[H][W];
		for(int h = 0; h < H; h++){
			for(int w = 0; w < W; w++){
				a[h][w] = sc.nextInt();
			}
		}

		int ans = 0;
		for(int h = 0; h < H; h++){
			for(int w = 0; w < W; w++){
				if(a[h][w] == 1){
					ans++;

					LinkedList<ArrayList<Integer>> list = new LinkedList<>();
					ArrayList<Integer> wh = new ArrayList<Integer>();
					wh.add(w);
					wh.add(h);
					list.add(wh);
					a[h][w] = 0;
					
					while(list.size()>0){
						ArrayList<Integer> xy = list.poll();
						int x = xy.get(0), y = xy.get(1);
						// System.out.println(y+" "+x);
						
						for(int i = 0; i < 4; i++){
							int xx = x+dx[i], yy = y+dy[i];
							if(xx >= 0 && xx < W && yy >= 0 && yy < H){
								if(a[yy][xx] == 1){
									ArrayList<Integer> xy_new = new ArrayList<Integer>();
									xy_new.add(xx);
									xy_new.add(yy);
									list.add(xy_new);
									a[yy][xx] = 0;
								}
							}
						}
					}
				}
			}
		}
		System.out.println(ans);
	}
}

0