https://school.programmers.co.kr/learn/courses/30/lessons/12952?language=javascript
뭐 재귀의 형태이긴 하지만, 결국 2차원 배열 돌면서 조건에 맞으면 count 를 하는 방식이다.
모든 프로그래머스 문제 관련 코드들은 GitHub 링크 에 있음.
<script>
const isPossible = (arrX, target) => {
let result = true;
for (let y = 0; y < arrX.length; y++) {
const x = arrX[y];
if (x === target.x
|| y === target.y
|| Math.abs(target.x - x) === Math.abs(target.y - y)) {
return false;
}
}
return result;
};
const getNQueenCount = (arrX, n) => {
if (arrX.length === n) {
return 1;
}
let count = 0;
const y = arrX.length;
for (let x = 0; x < n; x++) {
if (!isPossible(arrX, {x, y})) {
continue;
}
count += getNQueenCount([...arrX, x], n);
}
return count;
};
function solution(n) {
return getNQueenCount([], n);
}
</script>