結果

問題 No.697 池の数はいくつか
ユーザー tenten
提出日時 2020-12-04 14:04:14
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,225 ms / 6,000 ms
コード長 1,882 bytes
コンパイル時間 2,398 ms
コンパイル使用メモリ 77,900 KB
実行使用メモリ 59,060 KB
最終ジャッジ日時 2024-11-08 08:50:11
合計ジャッジ時間 15,323 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        int h = Integer.parseInt(first[0]);
        int w = Integer.parseInt(first[1]);
        boolean[][] field = new boolean[h + 2][w + 2];
        for (int i = 1; i <= h; i++) {
            String[] line = br.readLine().split(" ", w);
            for (int j = 1; j <= w; j++) {
                field[i][j] = (line[j - 1].charAt(0) == '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 * (w + 2) + j);
                    while (deq.size() > 0) {
                        int x = deq.poll();
                        int xh = x / (w + 2);
                        int xw = x % (w + 2);
                        if (!field[xh][xw]) {
                            continue;
                        }
                        field[xh][xw] = false;
                        if (field[xh][xw + 1]) {
                            deq.add(xh * (w + 2) + xw + 1);
                        }
                        if (field[xh][xw - 1]) {
                            deq.add(xh * (w + 2) + xw - 1);
                        }
                        if (field[xh + 1][xw]) {
                            deq.add((xh + 1) * (w + 2) + xw);
                        }
                        if (field[xh - 1][xw]) {
                            deq.add((xh - 1) * (w + 2) + xw);
                        }
                    }
                }
            }
        }
        System.out.println(count);
    }
}
0