# 递推算法，AI衍生

n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上，并且使皇后彼此之间不能相互攻击。

1 <= n <= 9
皇后彼此不能相互攻击，也就是说：任何两个皇后都不能处于同一条横行、纵行或斜线上。

## 题解1

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 `/**` `     ``* @author: Troy.Chen(失足程序员, 15388152619)` `     ``* @version: 2021-07-08 10:53` `     ``**/` `    ``class` `Solution {`   `        ``public` `List> solveNQueens(``int` `n) {` `            ``List> ret = ``new` `ArrayList<>();` `            ``List<``boolean``[][]> ot = ``new` `ArrayList<>();` `            ``for` `(``int` `z = ``0``; z < n; z++) {` `                ``for` `(``int` `x = ``0``; x < n; x++) {` `                    ``boolean``[][] action = action(ot, n, x, z);` `                    ``if` `(action == ``null``) {` `                        ``continue``;` `                    ``}` `                    ``List item = ``new` `ArrayList<>();` `                    ``for` `(``boolean``[] booleans : action) {` `                        ``String str = ``""``;` `                        ``for` `(``boolean` `a : booleans) {` `                            ``str += a ? ``"Q"` `: ``"."``;` `                        ``}` `                        ``item.add(str);` `                    ``}` `                    ``ret.add(item);` `                ``}` `            ``}` `            ``return` `ret;` `        ``}`   `        ``public` `boolean``[][] action(List<``boolean``[][]> ot, ``int` `n, ``int` `startX, ``int` `startZ) {` `            ``boolean``[][] tmp = ``new` `boolean``[n][n];` `            ``tmp[startZ][startX] = ``true``;` `            ``int` `qN = ``1``;` `            ``for` `(``int` `z = ``0``; z < tmp.length; z++) {` `                ``for` `(``int` `x = ``0``; x < tmp.length; x++) {` `                    ``if` `(check(tmp, x, z)) {` `                        ``tmp[z][x] = ``true``;` `                        ``qN++;` `                    ``}` `                ``}` `            ``}` `            ``if` `(qN >= n) {` `                ``if` `(!ot.isEmpty()) {` `                    ``for` `(``boolean``[][] tItem : ot) {` `                        ``boolean` `check = ``true``;` `                        ``for` `(``int` `z = ``0``; z < tmp.length; z++) {` `                            ``for` `(``int` `x = ``0``; x < tmp.length; x++) {` `                                ``if` `(tmp[z][x]) {` `                                    ``if` `(tmp[z][x] != tItem[z][x]) {` `                                        ``check = ``false``;` `                                        ``break``;` `                                    ``}` `                                ``}` `                            ``}` `                            ``if` `(!check) {` `                                ``break``;` `                            ``}` `                        ``}` `                        ``if` `(check) {` `                            ``return` `null``;` `                        ``}` `                    ``}` `                ``}` `                ``ot.add(tmp);` `                ``return` `tmp;` `            ``} ``else` `{` `                ``return` `null``;` `            ``}` `        ``}`   `        ``public` `boolean` `check(``boolean``[][] tmp, ``int` `checkx, ``int` `checkz) {` `            ``/*检查横向*/` `            ``for` `(``int` `x = ``0``; x < tmp.length; x++) {` `                ``if` `(tmp[checkz][x]) {` `                    ``return` `false``;` `                ``}` `            ``}` `            ``/*检查纵向*/` `            ``for` `(``int` `z = ``0``; z < tmp.length; z++) {` `                ``if` `(tmp[z][checkx]) {` `                    ``return` `false``;` `                ``}` `            ``}` `            ``int` `tx;` `            ``int` `tz;` `            ``{` `                ``/*从左到右，从上到下*/` `                ``tx = checkx;` `                ``tz = checkz;` `                ``while` `(``true``) {` `                    ``if` `(tmp[tz][tx]) {` `                        ``return` `false``;` `                    ``}` `                    ``tx--;` `                    ``tz--;` `                    ``if` `(tx < ``0` `|| tz < ``0``) {` `                        ``break``;` `                    ``}` `                ``}`   `                ``tx = checkx;` `                ``tz = checkz;` `                ``while` `(``true``) {` `                    ``if` `(tmp[tz][tx]) {` `                        ``return` `false``;` `                    ``}` `                    ``tx++;` `                    ``tz++;` `                    ``if` `(tx >= tmp.length || tz >= tmp.length) {` `                        ``break``;` `                    ``}` `                ``}` `            ``}` `            ``{` `                ``/*从右到左，从上到下*/` `                ``tx = checkx;` `                ``tz = checkz;` `                ``while` `(``true``) {` `                    ``if` `(tmp[tz][tx]) {` `                        ``return` `false``;` `                    ``}` `                    ``tx++;` `                    ``tz--;` `                    ``if` `(tx >= tmp.length || tz < ``0``) {` `                        ``break``;` `                    ``}` `                ``}` `                ``tx = checkx;` `                ``tz = checkz;` `                ``while` `(``true``) {` `                    ``if` `(tmp[tz][tx]) {` `                        ``return` `false``;` `                    ``}` `                    ``tx--;` `                    ``tz++;` `                    ``if` `(tx < ``0` `|| tz >= tmp.length) {` `                        ``break``;` `                    ``}` `                ``}` `            ``}` `            ``return` `true``;` `        ``}`   `    ``}`

这个写法，是我脑袋里的第一想法，代码怎么理解呢；

leetcode的输出结果是有4种可能性，但是我输出可能性只有1种；

## 题解2

```    /**
* @author: Troy.Chen(失足程序员, 15388152619)
* @version: 2021-07-08 10:53
**/
class Solution {

public List<List<String>> solveNQueens(int n) {
List<List<String>> ret = new ArrayList<>();
List<boolean[][]> ot = new ArrayList<>();

boolean[][] tmp = new boolean[n][n];
check(ot, tmp, 0, 0);

for (boolean[][] a : ot) {
}
return ret;
}

/*按照规定转化字符串*/
public List<String> convert(boolean[][] tmp) {
List<String> item = new ArrayList<>();
for (boolean[] booleans : tmp) {
String str = "";
for (boolean a : booleans) {
str += a ? "Q" : ".";
}
}
return item;
}

public void check(List<boolean[][]> ot, boolean[][] tmp, int checkx, int checkz) {
for (int x = checkx; x < tmp.length; x++) {
if (check0(tmp, x, checkz)) {
/*相当于逐行进行扫描所以要拷贝代码*/
int tmpz = checkz;
boolean[][] clone = clone(tmp);

clone[tmpz][x] = true;
tmpz++;
if (tmpz < tmp.length) {
check(ot, clone, 0, tmpz);
} else {
}
}
}
}

/*拷贝数组*/
public boolean[][] clone(boolean[][] tmp) {
boolean[][] clone = tmp.clone();
for (int i = 0; i < tmp.length; i++) {
clone[i] = tmp[i].clone();
}
return clone;
}

public boolean check0(boolean[][] tmp, int checkx, int checkz) {
/*检查横向*/
for (int x = 0; x < tmp.length; x++) {
if (tmp[checkz][x]) {
return false;
}
}
/*检查纵向*/
for (int z = 0; z < tmp.length; z++) {
if (tmp[z][checkx]) {
return false;
}
}
int tx;
int tz;
{
/*从左到右，从上到下*/
tx = checkx;
tz = checkz;
while (true) {
if (tmp[tz][tx]) {
return false;
}
tx--;
tz--;
if (tx < 0 || tz < 0) {
break;
}
}

tx = checkx;
tz = checkz;
while (true) {
if (tmp[tz][tx]) {
return false;
}
tx++;
tz++;
if (tx >= tmp.length || tz >= tmp.length) {
break;
}
}
}
{
/*从右到左，从上到下*/
tx = checkx;
tz = checkz;
while (true) {
if (tmp[tz][tx]) {
return false;
}
tx++;
tz--;
if (tx >= tmp.length || tz < 0) {
break;
}
}
tx = checkx;
tz = checkz;
while (true) {
if (tmp[tz][tx]) {
return false;
}
tx--;
tz++;
if (tx < 0 || tz >= tmp.length) {
break;
}
}
}
return true;
}

}```

(0)
(0)