From 5a8ead4481a1addc2c65483984e6892c2700873d Mon Sep 17 00:00:00 2001 From: jichan <66819791+jcdororo@users.noreply.github.com> Date: Wed, 31 Jan 2024 21:52:46 +0900 Subject: [PATCH] =?UTF-8?q?[BOJ]=204963=20=EC=84=AC=EC=9D=98=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=20/=20=EC=8B=A4=EB=B2=842=20/=2060=EB=B6=84=20/=20O?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0_\354\243\274\354\247\200\354\260\254.js" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "week16/BOJ_4963/\354\204\254\354\235\230 \352\260\234\354\210\230_\354\243\274\354\247\200\354\260\254.js" diff --git "a/week16/BOJ_4963/\354\204\254\354\235\230 \352\260\234\354\210\230_\354\243\274\354\247\200\354\260\254.js" "b/week16/BOJ_4963/\354\204\254\354\235\230 \352\260\234\354\210\230_\354\243\274\354\247\200\354\260\254.js" new file mode 100644 index 0000000..af402bd --- /dev/null +++ "b/week16/BOJ_4963/\354\204\254\354\235\230 \352\260\234\354\210\230_\354\243\274\354\247\200\354\260\254.js" @@ -0,0 +1,66 @@ +let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n') + +let start = 0; + +while(true) { + if(start == input.length - 1) break; + + // width, height + const [w, h] = input[start].split(' ').map(Number) + if(w == 0 && h == 0) continue; + // 카운트 변수 + let cnt = 0; + // 지도 만들기 + let map = []; + for(let i = 0; i < h; i++) { + map.push([]) + } + for(let i = start; i < start + h; i++) { + const land = input[i+1].split(' ').map(Number) + for(let j = 0; j < land.length; j++) { + map[i - start][j] = land[j] + } + } + // console.log('map',map) + // visited 만들기 + let visited = []; + for(let i = 0; i < h; i++) { + visited.push(Array(w).fill(false)) + } + // console.log(visited) + + for(let i = 0; i < map.length; i++) { + for(let j = 0; j < map[i].length; j++) { + if(visited[i][j] == false && map[i][j] == 1) { + dfs(i,j); + cnt++ + } + } + } + + // 상하 좌우 왼상 우하 우상 좌하 함수 + function dfs(x, y) { + + // 예외처리면 return + if((x < 0) + || (x >= map.length) + || (y < 0) + || (map[x].length <= y) + || (visited[x][y]) == true + || map[x][y] == 0) return; + + visited[x][y] = true; + dfs(x-1,y) // 상 + dfs(x+1,y) // 하 + dfs(x, y-1) // 좌 + dfs(x, y+1) // 우 + dfs(x-1,y-1) // 왼상 + dfs(x+1,y+1) // 우하 + dfs(x-1,y+1) // 우상 + dfs(x+1,y-1) // 좌하 + } + + console.log(cnt) + + start += h + 1 +}