๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/21736
๋ฌธ์ ํ์ด
dfs๋ก ์ ๊ทผํ์ฌ ํ ์ ์์๋ค.
ํ๋ฒ ์ง๋๊ฐ๋ ์ฅ์๋ map[row][column] = 'X' ๋ก ํ๊ธฐํ์๋ค.
//https://www.acmicpc.net/problem/15663
// TEST CASE
// let data = "3 5\nOOOPO\nOIOOX\nOOOXP";
// let input = data.toString().trim().split("\n");
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
let [N, M] = input.shift();
// ์บ ํผ์ค ์ง๋๋ฅผ๋ฅผ 2์ฐจ์ ๋ฐฐ์ด๋ก ์์ฑ
let map = [];
let startRow = undefined,
startColumn = undefined;
for (let row = 0; row < input.length; ++row) {
let line = input[row].split("");
line.forEach((flag, column) => {
if (flag === "I") {
// ๋์ฐ์ด ์์น ํ์ธ
startRow = row;
startColumn = column;
}
});
map.push(line);
}
let personCnt = 0;
let dfs = (nowR, nowC) => {
if (map[nowR][nowC] === "P") {
++personCnt;
}
map[nowR][nowC] = "X";
// ๋์๋จ๋ถ
let nextR = [nowR, nowR, nowR + 1, nowR - 1];
let nextC = [nowC + 1, nowC - 1, nowC, nowC];
for (let i = 0; i <= 3; ++i) {
if (map[nextR[i]] && map[nextR[i]][nextC[i]] && map[nextR[i]][nextC[i]] !== "X") {
dfs(nextR[i], nextC[i]);
}
}
};
dfs(startRow, startColumn);
console.log(personCnt === 0 ? "TT" : personCnt);
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค[node.js] > 2630๋ฒ ์์ข ์ด ๋ง๋ค๊ธฐ (0) | 2024.12.20 |
---|---|
๋ฐฑ์ค[node.js] > 1927๋ฒ ์ต์ ํ (0) | 2024.12.19 |
๋ฐฑ์ค[node.js] > 15663๋ฒ N๊ณผ M (9) (0) | 2024.12.16 |
๋ฐฑ์ค[node.js] > 11727๋ฒ 2xn ํ์ผ๋ง 2 (0) | 2024.12.15 |
๋ฐฑ์ค[node.js] > 30804๋ฒ ๊ณผ์ผ ํํ๋ฃจ (0) | 2024.12.14 |