From 5144d8618d3b4e7f6b275cc1734c730b4a36c051 Mon Sep 17 00:00:00 2001 From: beurmuz Date: Fri, 3 Feb 2023 22:08:30 +0900 Subject: [PATCH] =?UTF-8?q?#19=2023.02.03=20>=204=20>=20=EC=97=B0=EA=B5=AC?= =?UTF-8?q?=EC=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bj/gold/4/14502.js | 73 +++++++++++++++++++++++++++++++++++++++++ src/bj/gold/4/README.md | 5 +++ 2 files changed, 78 insertions(+) create mode 100644 src/bj/gold/4/14502.js create mode 100644 src/bj/gold/4/README.md diff --git a/src/bj/gold/4/14502.js b/src/bj/gold/4/14502.js new file mode 100644 index 00000000..dfad7b5c --- /dev/null +++ b/src/bj/gold/4/14502.js @@ -0,0 +1,73 @@ +"use strict"; + +const [[n, m], ...inputs] = require("fs") + .readFileSync("/dev/stdin") + .toString() + .trim() + .split("\n") + .map((v) => v.split(" ").map(Number)); + +const solution = (col, row, board) => { + const dx = [0, 0, 1, -1]; + const dy = [1, -1, 0, 0]; + let ans = 0; + + const countingSafeZone = (arr) => { + let cnt = 0; + let queue = []; + + for (let i = 0; i < col; i++) { + for (let j = 0; j < row; j++) { + if (arr[i][j] === 2) queue.push([i, j]); + } + } + + while (queue.length) { + const [curX, curY] = queue.shift(); + + for (let i = 0; i < 4; i++) { + const [nx, ny] = [curX + dx[i], curY + dy[i]]; + + if (nx >= 0 && nx < col && ny >= 0 && ny < row && arr[nx][ny] === 0) { + arr[nx][ny] = 2; + queue.push([nx, ny]); + } + } + } + + for (let i = 0; i < col; i++) { + for (let j = 0; j < row; j++) { + if (arr[i][j] === 0) { + cnt += 1; + } + } + } + + return cnt; + }; + + const dfs = (cnt) => { + if (cnt === 3) { + let arr = board.map((v) => [...v]); + let cntOfSafe = countingSafeZone(arr); + + ans = Math.max(ans, cntOfSafe); + return; + } + + for (let i = 0; i < col; i++) { + for (let j = 0; j < row; j++) { + if (board[i][j] === 0) { + board[i][j] = 1; + dfs(cnt + 1); + board[i][j] = 0; + } + } + } + }; + + dfs(0); + return ans; +}; + +console.log(solution(n, m, inputs)); diff --git a/src/bj/gold/4/README.md b/src/bj/gold/4/README.md new file mode 100644 index 00000000..aff94def --- /dev/null +++ b/src/bj/gold/4/README.md @@ -0,0 +1,5 @@ +# Gold 4 + +| | 문제번호 | 문제명 | 응시 날짜 | 채점 결과 | 재응시 | +| :-: | :------: | :------------------: | :-------: | :-------: | :----: | +| 1 | 14502 | [연구소](./14502.js) | 23.02.03 | X |