[深度搜索]n皇后问题暴力选择求解
n 皇后问题暴力全部枚举版本
Coding
#include <iostream>
using namespace std;
int n;
const int N = 30;
char g[N][N];
bool row[N], col[N], dg[N], udg[N];
inline void __init__(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] = '.';
}
}
}
inline void dfs(int x, int y, int s) {//xy表示坐标,s表示存在皇后个数
if (x == n) {
x = 0, y++;
//走到每一行的末尾,进入到下一行最左边进行搜索
}
if (y == n) {
if (s == n) {
for (int i = 0; i < n; i++) {
puts(g[i]);
}puts("");
//走到最后一行,并且有n个皇后了才打印
}
return;
//否则正常返回
}
dfs(x + 1, y, s);
//第一种搜索选择,不放皇后
//第二种选择放皇后
if (!row[y] && !col[x] && !dg[x + y] && !udg[y - x + n]) {
row[y] = col[x] = dg[x + y] = udg[y - x + n] = true;
g[y][x] = 'Q';
dfs(x + 1, y, s + 1);
row[y] = col[x] = dg[x + y] = udg[y - x + n] = false;
g[y][x] = '.';
}
}
int main() {
cin >> n;
__init__(n);
dfs(0, 0, 0);
}
dfs 函数
inline void dfs(int x, int y, int s) {//xy表示坐标,s表示存在皇后个数
if (x == n) {
x = 0, y++;
//走到每一行的末尾,进入到下一行最左边进行搜索
}
if (y == n) {
if (s == n) {
for (int i = 0; i < n; i++) {
puts(g[i]);
}puts("");
//走到最后一行,并且有n个皇后了才打印
}
return;
//否则正常返回
}
dfs(x + 1, y, s);
//第一种搜索选择,不放皇后
//第二种选择放皇后
if (!row[y] && !col[x] && !dg[x + y] && !udg[y - x + n]) {
row[y] = col[x] = dg[x + y] = udg[y - x + n] = true;
g[y][x] = 'Q';
dfs(x + 1, y, s + 1);
row[y] = col[x] = dg[x + y] = udg[y - x + n] = false;
g[y][x] = '.';
}
}
搜索思路

核心思想–暴力枚举每一个棋格,对于每一个棋格只有两种情况:
- 放皇后
- 不放皇后
边界处理
- 从左往右遍历,如果
x越界,进入下一行,y++
返回值判断
- 如果
y出界,代表遍历完一整个棋盘,返回return; - 如果,这个时候
s=n产生了n个皇后,这种情况刚好符合,打印出来即可

浙公网安备 33010602011771号