Number Of Island by BFS

BbQ (BFS = Queue)

์•ž์˜ "Number Of Island" ๋ฌธ์ œ๋ฅผ BFS ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด๊ฒ ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜

0. BFS๋Š” ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰์ด๊ธฐ ๋•Œ๋ฌธ์— Queue๋ฅผ ์ด์šฉํ•œ๋‹ค! (FIFO)

  1. DFS, BFS ๋ชจ๋‘ 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ์˜ ์ธํ’‹์„ ์ขŒํ‘œ๊ฐ’์œผ๋กœ ํ˜•์ƒํ™”ํ•˜์—ฌ ํ‘ผ๋‹ค.

2. ํ˜„์žฌ ์ขŒํ‘œ ์œ„์น˜(์ขŒํ‘œ๊ฐ’) ๊ธฐ์ค€์œผ๋กœ ์ƒ,ํ•˜,์ขŒ,์šฐ ํƒ์ƒ‰ํ•œ๋‹ค! ํ์— ํ˜„์žฌ ์ขŒํ‘œ์œ„์น˜(์ขŒํ‘œ๊ฐ’) ๋„ฃ๊ณ , ๋นผ์„œ ์ƒ/ํ•˜/์ขŒ/์šฐ ์œ„์น˜๊ฐ’ ๋”ํ•œ๋‹ค.- {{-1,0}, {1,0},{0,-1},{0,1}}

(DFS์—์„œ๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ dfs(i-1,j) dfs(i+1,j) dfs(i,j-1) dfs(i,j+1) ์ด๋ ‡๊ฒŒ ํ˜ธ์ถœ.)

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ Java๋กœ

Last updated