Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created June 8, 2011 22:54
Show Gist options
  • Save xeoncross/1015646 to your computer and use it in GitHub Desktop.
Save xeoncross/1015646 to your computer and use it in GitHub Desktop.
Backtracking, prementions, and 8-Queens
<?php
// http://paulbutler.org/archives/n-queens-in-a-tweet/
$n=$argv[1];$s=range(1,$n);while($i<$n*10000){shuffle($s);$a=s($s,range(1,$n));$b=s(s($s,range($n,1)),2*$n);$c=array_merge($a,$b);if(count(array_unique($c))==2*$n){die(join(',',$s));}$i++;}function s($a,$b){if(!is_array($b))$b=array_fill(0,count($a),$b);foreach($a as $i=>$k)$r[$i]=$k+$b[$i];return $r;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment