Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
33
Task/N-queens-problem/JavaScript/n-queens-problem-1.js
Normal file
33
Task/N-queens-problem/JavaScript/n-queens-problem-1.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
function queenPuzzle(rows, columns) {
|
||||
if (rows <= 0) {
|
||||
return [[]];
|
||||
} else {
|
||||
return addQueen(rows - 1, columns);
|
||||
}
|
||||
}
|
||||
|
||||
function addQueen(newRow, columns, prevSolution) {
|
||||
var newSolutions = [];
|
||||
var prev = queenPuzzle(newRow, columns);
|
||||
for (var i = 0; i < prev.length; i++) {
|
||||
var solution = prev[i];
|
||||
for (var newColumn = 0; newColumn < columns; newColumn++) {
|
||||
if (!hasConflict(newRow, newColumn, solution))
|
||||
newSolutions.push(solution.concat([newColumn]))
|
||||
}
|
||||
}
|
||||
return newSolutions;
|
||||
}
|
||||
|
||||
function hasConflict(newRow, newColumn, solution) {
|
||||
for (var i = 0; i < newRow; i++) {
|
||||
if (solution[i] == newColumn ||
|
||||
solution[i] + i == newColumn + newRow ||
|
||||
solution[i] - i == newColumn - newRow) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(queenPuzzle(8,8));
|
||||
129
Task/N-queens-problem/JavaScript/n-queens-problem-2.js
Normal file
129
Task/N-queens-problem/JavaScript/n-queens-problem-2.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ---------------- N QUEENS PROBLEM -----------------
|
||||
|
||||
// queenPuzzle :: Int -> Int -> [[Int]]
|
||||
const queenPuzzle = intCols => {
|
||||
// All solutions for a given number
|
||||
// of columns and rows.
|
||||
const go = nRows =>
|
||||
nRows <= 0 ? [
|
||||
[]
|
||||
] : go(nRows - 1).reduce(
|
||||
(a, solution) => [
|
||||
...a, ...(
|
||||
enumFromTo(0)(intCols - 1)
|
||||
.reduce((b, iCol) =>
|
||||
safe(
|
||||
nRows - 1, iCol, solution
|
||||
) ? (
|
||||
[...b, [...solution, iCol]]
|
||||
) : b, [])
|
||||
)
|
||||
], []
|
||||
);
|
||||
|
||||
|
||||
return go;
|
||||
};
|
||||
|
||||
// safe : Int -> Int -> [Int] -> Bool
|
||||
const safe = (iRow, iCol, solution) =>
|
||||
!zip(solution)(
|
||||
enumFromTo(0)(iRow - 1)
|
||||
)
|
||||
.some(
|
||||
([sc, sr]) => (iCol === sc) || (
|
||||
sc + sr === iCol + iRow
|
||||
) || (sc - sr === iCol - iRow)
|
||||
);
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
// Ten columns of solutions to the 7*7 board
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(
|
||||
showSolutions(10)(7)
|
||||
);
|
||||
|
||||
// --------------------- DISPLAY ---------------------
|
||||
|
||||
// showSolutions :: Int -> Int -> String
|
||||
const showSolutions = nCols =>
|
||||
// Display of solutions, in nCols columns
|
||||
// for a board of size N * N.
|
||||
n => chunksOf(nCols)(
|
||||
queenPuzzle(n)(n)
|
||||
)
|
||||
.map(xs => transpose(
|
||||
xs.map(
|
||||
rows => rows.map(
|
||||
r => enumFromTo(1)(rows.length)
|
||||
.flatMap(
|
||||
x => r === x ? (
|
||||
"♛"
|
||||
) : "."
|
||||
)
|
||||
.join("")
|
||||
)
|
||||
)
|
||||
)
|
||||
.map(cells => cells.join(" "))
|
||||
)
|
||||
.map(x => x.join("\n"))
|
||||
.join("\n\n");
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// chunksOf :: Int -> [a] -> [[a]]
|
||||
const chunksOf = n => {
|
||||
// xs split into sublists of length n.
|
||||
// The last sublist will be short if n
|
||||
// does not evenly divide the length of xs .
|
||||
const go = xs => {
|
||||
const chunk = xs.slice(0, n);
|
||||
|
||||
return Boolean(chunk.length) ? [
|
||||
chunk, ...go(xs.slice(n))
|
||||
] : [];
|
||||
};
|
||||
|
||||
return go;
|
||||
};
|
||||
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
|
||||
// transpose_ :: [[a]] -> [[a]]
|
||||
const transpose = rows =>
|
||||
// The columns of the input transposed
|
||||
// into new rows.
|
||||
// Simpler version of transpose, assuming input
|
||||
// rows of even length.
|
||||
Boolean(rows.length) ? rows[0].map(
|
||||
(_, i) => rows.flatMap(
|
||||
v => v[i]
|
||||
)
|
||||
) : [];
|
||||
|
||||
|
||||
// zip :: [a] -> [b] -> [(a, b)]
|
||||
const zip = xs =>
|
||||
// The paired members of xs and ys, up to
|
||||
// the length of the shorter of the two lists.
|
||||
ys => Array.from({
|
||||
length: Math.min(xs.length, ys.length)
|
||||
}, (_, i) => [xs[i], ys[i]]);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue