Skip to content

Commit

Permalink
Merge pull request wangzheng0822#240 from iyoungm/master
Browse files Browse the repository at this point in the history
backtracking 8queens
  • Loading branch information
wangzheng0822 authored Jan 28, 2019
2 parents 0616eae + 01f734b commit c09c5b1
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions php/39_backtracking/queens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* 8皇后解法,共92种解法 回溯思想
* Class Queen
*/
class Queen
{
public $result = [];

function cal8queens($row)
{
if ($row == 8) {
$this->printQueens();
return;
}

//每一行有8中放法
for($column = 0; $column < 8; $column++) {
if ($this->isOk($row, $column)) {
$this->result[$row] = $column;
$this->cal8queens($row + 1);
}
}
}

//row行的column列是否合适
function isOk($row, $column)
{
$leftup = $column - 1;
$rightdown = $column + 1;

for ($i = $row - 1; $i >= 0; $i--) {
//判断上一行的 column 列是否有值
if ($this->result[$i] == $column) {
return false;
}

//左上角是否有值
if ($leftup >= 0 && $this->result[$i] == $leftup) {
return false;
}

//右下角是否有值
if ($rightdown < 8 && $this->result[$i] == $rightdown) {
return false;
}

$leftup--;
$rightdown++;

}

return true;
}

//打印
function printQueens()
{
for ($row = 0; $row < 8; $row++) {
for ($column = 0; $column < 8; $column++) {
if ($this->result[$row] == $column) {
echo 'Q';
} else {
echo '*';
}
}
echo '<br>';
}
}
}

$queen = new Queen();
$queen->cal8queens(0);

0 comments on commit c09c5b1

Please sign in to comment.