반응형
import Foundation
let nm = readLine()!.components(separatedBy: " ").map { Int($0)! }
let n = nm[0]
let m = nm[1]
var board = [[Int]]()
for _ in 0..<n {
let line = readLine()!.split(separator: " ").map { Int($0)! }
board.append(line)
}
var vis = [[Bool]](repeating: [Bool](repeating: false, count: m), count: n)
let dx = [0, 0, -1, 1]
let dy = [-1, 1, 0, 0]
var cnt = 0
var maxArea = 0
for i in 0..<n {
for j in 0..<m {
if board[i][j] == 0 || vis[i][j] { continue }
cnt += 1
var queue = [(Int, Int)]()
vis[i][j] = true
queue.append((i,j))
var area = 0
while !queue.isEmpty {
area += 1
let cur = queue.removeFirst()
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx<0 || nx>=n || ny<0 || ny>=m { continue }
if vis[nx][ny] || board[nx][ny] != 1 { continue }
vis[nx][ny] = true
queue.append((nx, ny))
}
}
maxArea = max(maxArea, area)
}
}
print(cnt)
print(maxArea)
반응형
'📝 코테 > BOJ' 카테고리의 다른 글
[Swift] 백준 7569 토마토 (0) | 2023.06.02 |
---|---|
[Swift] 백준9375 패션왕 신해빈 (0) | 2023.04.14 |
[Swift] 백준 9095 1, 2, 3 더하기 (0) | 2023.04.14 |
[Swift] 백준 10026 적록색약 (0) | 2023.04.14 |
[Swift] 백준 11399 ATM (0) | 2023.04.12 |