Task: N-queens problem





Java: public class NQueens {

  private static int[] b = new int[8];
  private static int s = 0;

  static boolean unsafe(int y) {
    int x = b[y];
    for (int i = 1; i <= y; i++) {
      int t = b[y - i];
      if (t == x ||
          t == x - i ||
          t == x + i) {
        return true;
      }
    }

    return false;
  }

  public static void putboard() {
    System.out.println("\n\nSolution " + (++s));
    for (int y = 0; y < 8; y++) {
      for (int x = 0; x < 8; x++) {
        System.out.print((b[y] == x) ? "|Q" : "|_");
      }
      System.out.println("|");
    }
  }

  public static void main(String[] args) {
    int y = 0;
    b[0] = -1;
    while (y >= 0) {
      do {
        b[y]++;
      } while ((b[y] < 8) && unsafe(y));
      if (b[y] < 8) {
        if (y < 7) {
          b[++y] = -1;
        } else {
          putboard();
        }
      } else {
        y--;
      }
    }
  }
}


PHP: <html>
<head>
<title>
n x n Queen solving program
</title>
</head>
<body>
<?php
echo "<h1>n x n Queen solving program</h1>";

$boardX = $_POST['boardX'];
$boardY = $_POST['boardX'];

function rotateBoard($p, $boardX) {
    $a=0;
    while ($a < count($p)) {
        $b = strlen(decbin($p[$a]))-1;
        $tmp[$b] = 1 << ($boardX - $a - 1);
        ++$a;
    }
    ksort($tmp);
    return $tmp;
}

function findRotation($p, $boardX,$solutions){
    $tmp = rotateBoard($p,$boardX);

    if (in_array($tmp,$solutions)) {}
    else {$solutions[] = $tmp;}
    
    $tmp = rotateBoard($tmp,$boardX);

    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}
    
    $tmp = rotateBoard($tmp,$boardX);

    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}

    $tmp = array_reverse($p);
    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}
    
    $tmp = rotateBoard($tmp,$boardX);

    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}
    
    $tmp = rotateBoard($tmp,$boardX);

    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}
    
    $tmp = rotateBoard($tmp,$boardX);

    if (in_array($tmp,$solutions)){}
    else {$solutions[] = $tmp;}
    return $solutions;
}

function renderBoard($p,$boardX) {
$img = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAtCAYAAAA6GuKaAAAABmJLR0QA/wD/AP+gvaeTAAAGFUlEQVRYhe2YXWibVRjHf2lqP9JmaRi4YW1IalY3rbZsaddMgsquBm676b6KyNDhLiaUeSEMvPNCcNuNyJjgLiboCnoxKFlv6lcHy7AtMhhaWTVZWhisjDTEtEuW5PHiPWnfvH2TvNk6vekfDm/O+Z/zPP/3PM/5eAMb2MAG/nfYn4LNVuBj4ENgB/Ar8Ogp+KkJbwLfqvKGgbMBPwKiK+Oq3aqNdcebQEEnqAC8ruO7KBVcLF012KiKuhpFv0/prNlU239qw0x0pdBJFXt30NJDjx9Uu1Ub1TSYdq4UutcNfI61oW0Bflb8T6quRzUbNafPFdbm4zcmTucV91kZO18o/osy/GeKnzcRVFWDMT2shO4X4IL6/UqZPv2GpxHFcReUvVo1lMAYunKh+UTxeeB5A/cMkFF8RtX1eF6NE2XHTIN+ltekoHGmf0HLqe9V3Qb8ZWK4Xjf+HQP3KtCgfjeouh7v6PzWsxZ6f98De1kbjbIovumoCfcp2gzkgb8p3cJOUjpTJ3WcTfXPq/Gfmtge1Y01RaV9+jv1fAsYMnAu3XgfENJxfUoU6tmn40Kqf9Gvi1IMKX96/zWJnlLP4i7wrIEvzkQeeFfXvltnt07Vi3iX1RcyzuSzrO46ev81YS+rYcqjbUVFfIl2CSryS4ATcKCF3biQHIpf0rU/UnaKuMLqAhXlv2a4Dc4FOKi4bwyiBTgBvGYyRlT7CUPbI1b334MmY9zlhFVKjwQQ09ULaDNTNKYPbx54j9L81aNP8XldW3G8W9kt6LiY8m8Ksy1Hj0mgA+3eXYeWd2eBRkpf2A4MoO3JOYPdHPA2sMtgu07ZOavsFnegvPL72PiItWEroB0axtwtmPStxOeUHbNxH1USVe1qOm3SVkA7NIwX+1phU3YKJpyZX8swW4y1FOMsVotG1UUI1m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+Mq3vLsdjv5fJ7FxUWWl5eJx+OJubm5mIjMon1O/Yr2N0G6VufrdhwrtAJtaN9+bWihzqB9pNYsbgMbeAz8C3N/JQD4H5KCAAAAAElFTkSuQmCC';
echo "<table border=1 cellspacing=0 style='text-align:center;display:inline'>";
for ($y = 0; $y < $boardX; ++$y) {
	echo '<tr>';
	for ($x = 0; $x < $boardX; ++$x){
	if (($x+$y) & 1) { $cellCol = '#9C661F';}
	else {$cellCol = '#FCE6C9';}
 
	if ($p[$y] == 1 << $x) { echo "<td bgcolor=".$cellCol."><img width=30 height=30 src='".$img."'></td>";}
	else { echo "<td bgcolor=".$cellCol."> </td>";}
	}
	echo '<tr>';
}
echo '<tr></tr></table>&nbsp';
 
}

function pc_next_permutation($p) {
    $size = count($p) - 1;

    
    for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { } 


    if ($i == -1) { return false; } 

    for ($j = $size; $p[$j] <= $p[$i]; --$j) { } 

    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; 

    for (++$i, $j = $size; $i < $j; ++$i, --$j) 
    { $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; } 
    return $p; 
}

function checkBoard($p,$boardX) {
	$a = 0; //this is the row being checked
	while ($a < count($p)) { 
		$b = 1;
		while ($b < ($boardX - $a)){
            $x = $p[$a+$b] << $b;
            $y = $p[$a+$b] >> $b;
            if ($p[$a] == $x | $p[$a] == $y) { 
                return false;
            }		
            ++$b;
		}
	++$a; 
	}
	return true;
}
 
 
if (isset($_POST['process']) && isset($_POST['boardX']))
{


for ($x = 0; $x < $boardX; ++$x){
	$row[$x] = 1 << $x;
}

$solcount = 0;
$solutions = array();
while ($row != false) {
	if (checkBoard($row,$boardX)){
	if(!in_array($row,$solutions)){
		$solutions[] = $row;
			renderBoard($row,$boardX);
			$solutions = findRotation($row,$boardX,$solutions);
			++$solcount;
		}
 
	}
	$row = pc_next_permutation($row);	
}
echo "<br><br>&nbsp&nbsp&nbsp&nbspRows/Columns: ".$boardX."<br>&nbsp&nbsp&nbsp&nbspUnique Solutions: ".$solcount."<br>&nbsp&nbsp&nbsp&nbspTotal Solutions: ".count($solutions)."  - Note: This includes symmetrical solutions<br>";

}

echo <<<_END
<form name="input" action="index.php" method="post">
&nbsp&nbsp&nbsp&nbspNumber of columns/rows <select name="boardX" />
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4" >Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8" selected="selected">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
    <input type="hidden" name="process" value="yes" />
&nbsp<input type="submit" value="Process" />
</form>
 
_END;
 
?>
</body>
</html>