๐ 25๊ฐ์ค 7๊ฐ๋ฅผ ์ ํํ๊ณ ์ด ํ ์กฐ๊ฑด์ ๋ง๋ ๊ฒ์ ์ฐพ์์ผํ๋ค.
๐ ๋๋ ๊ฐ๋ก,์ธ๋ก ์ฐ๊ฒฐ๋์ด ์๋์ง ํ์ธํ ๋ bfs๋ฅผ ์ฌ์ฉํ๋ค.
๐จ ์ฐธ๊ณ ์ฝ๋
๐จ ์ฐธ๊ณ ์ฝ๋
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map((el)=>el.split(''));
let answer = 0;
const visited = new Array(25).fill(false);
const map = Array.from(new Array(5), () => new Array(5).fill(false));
const check = () => {
let count = 0;
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const bfs = (y, x) => {
const queue = [[y, x]];
const visited = Array.from(new Array(5), () => new Array(5).fill(false));
visited[y][x] = true;
while (queue.length) {
const [Y, X] = queue.shift();
for (let d = 0; d < 4; d++) {
const [nextY, nextX] = [Y + dir[d][0], X + dir[d][1]];
if (
nextY < 0 ||
nextX < 0 ||
nextY >= 5 ||
nextX >= 5 ||
visited[nextY][nextX] ||
!map[nextY][nextX]
)
continue;
queue.push([nextY, nextX]);
visited[nextY][nextX] = true;
count++;
}
}
return count;
};
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
if (map[i][j]) {
count++;
return bfs(i, j);
}
}
}
};
const dfs = (num, count, s) => {
if (count === 7) {
if (s >= 4 && check() === 7) answer++;
} else {
for (let i = num; i < 25; i++) {
if (visited[i]) continue;
visited[i] = true;
const y = Math.floor(i / 5);
const x = i % 5;
let countS = s;
map[y][x] = true;
if (input[y][x] === "S") countS++;
dfs(i, count + 1, countS);
map[y][x] = false;
visited[i] = false;
}
}
};
dfs(0, 0, 0);
console.log(answer);