๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

22:57

์ธ์ ‘ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง€๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ฃผ๋กœ ํ’€์–ด์„œ ์ฒ˜์Œ์— ๊ฐ์ด ์ž˜ ์•ˆ ์žกํ˜”๋Š”๋ฐ, ๋น„์Šทํ•˜๊ฒŒ ์ ‘๊ทผํ•ด์„œ ํ’€์—ˆ๋‹ค.


๋ฌธ์ œ

https://www.acmicpc.net/problem/11403


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let n = Int(readLine()!)! 
var board = [[Int]]() 
for _ in 0..<n {     
	let line = readLine()!.split(separator: " ").map { Int(String($0))! }     
	board.append(line) 
}  

var answer = Array(repeating: Array(repeating: 0, count: n), count: n) 
for i in 0..<n {     
	for j in 0..<n {         
    	if board[i][j] == 1 {
        	answer[i][j] = 1
            
            var visited = [j]
            var queue = [j]
            while !queue.isEmpty {
            	let v = queue.removeFirst()
                for w in 0..<n {
                	if !visited.contains(w) && board[v][w] == 1 {
                    	answer[i][w] = 1
                        visited.append(w)
                        queue.append(w)
					}
                }
            }
        }
    }
}

for a in answer {     
	print(a.map { String($0) }.joined(separator: " ")) 
}
๋ฐ˜์‘ํ˜•