結果

問題 No.697 池の数はいくつか
ユーザー Daigo HIROOKA
提出日時 2018-06-23 03:25:57
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 3,200 ms / 6,000 ms
コード長 1,212 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,907 ms
コンパイル使用メモリ 84,152 KB
実行使用メモリ 102,900 KB
最終ジャッジ日時 2026-05-08 14:03:26
合計ジャッジ時間 30,213 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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