Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created September 11, 2011 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaniel/1209102 to your computer and use it in GitHub Desktop.
Save tsaniel/1209102 to your computer and use it in GitHub Desktop.
The eight queens puzzle - get the number of soultions of an n queens puzzle
function (
a, // bitmask that all queens are placed
b // placeholder for result
) {
return function f(
c, // row
d, // left diagnal
e, // right diagnal
g, // placeholder for places where queens can be placed in
h // placeholder for the position where the queen is placed in
) {
for (g = a ^ c && a & ~(c | d | e); h = -g & g; g-=h) // loop through all subtrees
f(c + h, d + h << 1, e + h >> 1); // to the next depth
a^c||++b; // add one solution
}(b = 0, 0, 0), b;
}
function(a,b){return function f(c,d,e,g,h){for(g=a^c&&a&~(c|d|e);h=-g&g;g-=h)f(c+h,d+h<<1,e+h>>1);a^c||++b}(b=0,0,0),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": "nQueen",
"description": "Get the number of soultions of an n queens puzzle.",
"keywords": [
"eight queens",
"queens",
"eight queens puzzle",
"puzzle"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>92</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(a,b){return function f(c,d,e,g,h){for(g=a^c&&a&~(c|d|e);h=-g&g;g-=h)f(c+h,d+h<<1,e+h>>1);a^c||++b}(b=0,0,0),b}
document.getElementById( "ret" ).innerHTML = myFunction((1 << 8) - 1)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment