Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created September 12, 2011 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaniel/1212218 to your computer and use it in GitHub Desktop.
Save tsaniel/1212218 to your computer and use it in GitHub Desktop.
The eight queens puzzle - get all the soultions

queensSolutions

Solve the famous n queens puzzle with bitwise.

For example, solving an eight queens puzzle, the input should be 255 (11111111 bin), and the output will return an array which contains all the solutions formed as strings.

One of the solutions of the above exapmle is like "1,16,128,32,4,64,2,8". After spliting with the separator ",", it becomes ["1", "16", "128", "32", "4", "64", "2", "8"].

Now it looks like this:

1 16 128 32 4 64 2 8

In binary form:

00000001 00010000 10000000 00100000 00000100 01000000 00000010 00001000

where 1 are queens.

function f(
a, // bitmask representing all queens are placed
b, // result storage
c, // positions record
d, // row
e, // left diagnal
g, // right diagnal
h, // placeholder for places where queens can be placed in
i // placeholder for the position where the queen is placed in
) {
for (a ^ d ? h = a & ~(d | e | g) : b.push(c); i = -h & h; h ^= i) // loop through over the subtrees
f(a, b = b || [], d ? c + [,i] : i, d | i, (e | i) * 2, (g | i) / 2); // go to the next depth
return b;
}
function f(a,b,c,d,e,g,h,i){for(a^d?h=a&~(d|e|g):b.push(c);i=-h&h;h^=i)f(a,b=b||[],d?c+[,i]:i,d|i,(e|i)*2,(g|i)/2);return b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "queensSolutions",
"description": "Get all the soultions of an n queens puzzle.",
"keywords": [
"queens"
]
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Get all soultions of the eight queens puzzle w/bitwise</title>
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset200802.css">
<style>
div {
margin: 24px;
padding: 24px;
border: 1px solid black;
font-size: 2.4em;
text-align: center;
}
</style>
</head>
<body>
<script>
var queens = 8;
var getQueensPos = function f(a,b,c,d,e,g,h,i){for(a^d?h=a&~(d|e|g):b.push(c);i=-h&h;h^=i)f(a,b=b||[],d?c+[,i]:i,d|i,(e|i)*2,(g|i)/2);return b};
getQueensPos((1 << queens) - 1).forEach(function(a, b) {
var board = document.body.appendChild(document.createElement('div'));
board.appendChild(document.createElement('h1')).appendChild(document.createTextNode(b + 1))
a.split(',').forEach(function(c) {
board.appendChild(
document.createElement('p')
).appendChild(document.createTextNode(((1 << queens | c).toString(2)).slice(-queens).replace(1, '♛').replace(/0/g, '♕')))
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment