結果

問題 No.697 池の数はいくつか
ユーザー conf8o
提出日時 2020-02-10 13:36:50
言語 Swift
(6.0.3)
結果
AC  
実行時間 5,716 ms / 6,000 ms
コード長 1,303 bytes
コンパイル時間 1,770 ms
コンパイル使用メモリ 135,924 KB
実行使用メモリ 84,320 KB
最終ジャッジ日時 2024-11-08 08:32:36
合計ジャッジ時間 29,675 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

func readInt2() -> (Int16, Int16) {
    let l = readLine()!.split(separator: " ").map{Int16($0)!}
    return (l[0], l[1])
}

func readLines(_ n: Int16) -> [[Bool]] {
    return (0..<n).map{(Int16) -> [Bool] in
        return readLine()!.split(separator: " ").map{$0 == "1"}
    }
}

class Task {
    let (H, W): (Int16, Int16) = readInt2()
    let dires: [(Int16, Int16)] = [(-1, 0),(0, 1),(1, 0),(0, -1)]
    var A: [[Bool]]!
    
    init() {
        self.A = readLines(H)
    }

    func dfs(_ i: Int16, _ j: Int16) -> Int{
        var frontier: [(Int16, Int16)] = []
        frontier.append((i, j))
        while let (h, w) = frontier.popLast() {
            A[Int(h)][Int(w)] = false
            for (y, x) in dires{
                let sucY = h+y
                let sucX = w+x
                if sucY < 0 || sucY >= H || sucX < 0 || sucX >= W || !A[Int(sucY)][Int(sucX)] {
                    continue
                }
                frontier.append((sucY, sucX))
            }
        }
        return 1
    }

    func main() {
        var ans = 0
        for i in 0..<H {
            for j in 0..<W {
                if !A[Int(i)][Int(j)] {
                    continue
                }
                ans += dfs(i, j)
            }
        }
        print(ans)
    }
    
}

Task().main()
0