Public
Edited
Jul 1, 2024
Insert cell
Insert cell
function isDiagonal(r, c, r2, c2) {
let dr = r2 - r;
let dc = c2 - c;
return dr == dc || dr == -dc;
}
Insert cell
isDiagonal(1,1,3,3)
Insert cell
isDiagonal(3,0,0,3)
Insert cell
isDiagonal(0,0,1,2)
Insert cell
function queens(n) {
let ways = [];

function place(positions) {
let m = positions.length;

if (m == n) {
ways.push(positions);
return;
}

const hasDiagonal = (i) => {
for (let j = 0; j < m; j++) {
if (isDiagonal(m, i, j, positions[j])) return true
}
return false
}

for (let i = 0; i < n; i++) {
if (positions.includes(i)) continue;
if (hasDiagonal(i)) continue;
place([...positions, i]);
}
}

place([]);
return ways;
}
Insert cell
queens(2)
Insert cell
queens(3)
Insert cell
queens(4)
Insert cell
queens(5)
Insert cell
queens(6)
Insert cell
queens(7)
Insert cell
queens(8)
Insert cell
function plot(positions) {
const n = positions.length;
const empty = new Array(n).fill("·");
let res = [];

for (let i = 0; i < n; i++) {
let line = empty.slice();
line[positions[i]] = "Q";
res.push(line.join(""));
}

return res.join("\n");
}
Insert cell
plot([1,3,0,2])
Insert cell
plot([2,0,3,1])
Insert cell
plot([0,4,7,5,2,6,1,3])
Insert cell
function queensT(n) {
let ways = [];
let c = 0;

function place(positions) {
c++;
let m = positions.length;

if (m == n) {
ways.push(positions);
return;
}

const hasDiagonal = (i) => {
for (let j = 0; j < m; j++) {
if (isDiagonal(m, i, j, positions[j])) return true
}
return false
}

for (let i = 0; i < n; i++) {
if (positions.includes(i)) continue;
if (hasDiagonal(i)) continue;
place([...positions, i]);
}
}

place([]);
return c;
}
Insert cell
queensT(3)
Insert cell
queensT(4)
Insert cell
queensT(5)
Insert cell
queensT(6)
Insert cell
queensT(7)
Insert cell
queensT(8)
Insert cell
queensT(9)
Insert cell
queensT(10)
Insert cell
queensT(11)
Insert cell
queensT(12)
Insert cell
function queensM(n) {
const ways = [];
const col = {}
const left = {}
const right = {}

function place(positions) {
let m = positions.length;

if (m == n) {
ways.push(positions);
return;
}

for (let i = 0; i < n; i++) {
if (col[i]) continue;
if (left[m + i]) continue;
if (right[m - i]) continue;

col[i] = true
left[m+i] = true
right[m-i] = true
place([...positions, i]);
col[i] = false
left[m+i] = false
right[m-i] = false
}
}

place([]);
return ways;
}
Insert cell
queensM(8)
Insert cell
queensM(12)
Insert cell
function queensL(n) {
const ways = [];
const stack = [[[], {}, {}, {}]]
let current
while (current = stack.pop()) {
const [positions, col, left, right] = current
let m = positions.length;

if (m == n) {
ways.push(positions);
continue;
}

for (let i = 0; i < n; i++) {
if (col[i]) continue;
if (left[m+i]) continue;
if (right[m-i]) continue;
stack.push([
[...positions, i],
{...col, [i]: true },
{...left, [m+i]: true },
{...right, [m-i]: true }
])
}
}

return ways;
}

Insert cell
queensL(5)
Insert cell
queensL(6)
Insert cell
queensL(7)
Insert cell
queensL(8)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more