結果

問題 No.697 池の数はいくつか
ユーザー Daigo HIROOKA
提出日時 2018-06-23 03:25:57
言語 Java
(openjdk 23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,212 bytes
コンパイル時間 6,717 ms
コンパイル使用メモリ 78,580 KB
実行使用メモリ 109,996 KB
最終ジャッジ日時 2024-11-08 07:57:44
合計ジャッジ時間 55,249 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

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