Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1810869

Browse files
committed
Add solution 52.
1 parent 9fcaf9f commit 1810869

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
3+
var $tot = 0;
4+
/**
5+
* @param Integer $n
6+
* @return Integer
7+
*/
8+
function totalNQueens($n) {
9+
$col = array_fill(0, $n, 0);
10+
$cur = 0;
11+
return self::totalQueens($n,$cur,$col);
12+
}
13+
14+
function totalQueens($n,$cur,&$col){
15+
if($cur == $n) $this->tot++;
16+
else{
17+
for($i = 0; $i < $n; $i++){
18+
$ok = true;
19+
$col[$cur] = $i;
20+
for($j = 0; $j < $cur; $j++){
21+
if($col[$cur] == $col[$j] || $cur+$col[$cur] == $j + $col[$j] || $cur-$col[$cur] == $j - $col[$j]){
22+
$ok = false;
23+
break;
24+
}
25+
}
26+
if($ok){
27+
self::totalQueens($n,$cur+1,$col);
28+
}
29+
}
30+
}
31+
return $this->tot;
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /