AcWing 843. n-皇后问题 三种语言
原题链接
中等
作者:
FanXY
,
2023-05-16 15:55:03
,
所有人可见
,
阅读 170
n−皇后问题三种语言
思路
N皇后问题所引出的——对主对角线和副对角线的映射问题
if (!col[i] && !dg[u + i] && !udg[n - u + i])
这里采用数形结合的思想,主要的目的是把相同对角线所对应的元素找到一个统一的映射方法(截距)
java 代码
import java.io.*;
public class Main {
static int N = 10, n;
static char[][] path = new char[N][N];
static boolean[] col = new boolean[N];
static boolean[] dg = new boolean[2 * N];
static boolean[] udg = new boolean[2 * N];
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static void dfs(int x) throws IOException {
if (x == n) {
for (int i=0; i<n; i++) {
bw.write(path[i], 0, n);
bw.newLine();
}
bw.newLine();
bw.flush();
return;
}
for(int i=0; i<n; i++)
if (!col[i] && !dg[i + x] && !udg[i - x + n]) {
path[x][i] = 'Q';
col[i] = dg[i+x] = udg[i-x+n] = true;
dfs(x+1);
col[i] = dg[i+x] = udg[i-x+n] = false;
path[x][i] = '.';
}
}
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
path[i][j] = '.';
dfs(0);
}
}
python3 代码
N = int(10)
path = [['.']*N for i in range(N)]
col = [1] * N
dg = [1] * (2*N)
udg = [1] * (2*N)
n = int(input())
def dfs(x):
if x == n:
for i in range(n):
for j in range(n):
print(path[i][j], end = '')
print()
print()
return
for i in range(n):
if col[i] and dg[x+i] and udg[x-i+n]:
path[x][i] = 'Q'
col[i] = dg[x+i] = udg[x-i+n] = 0
dfs(x+1)
col[i] = dg[x+i] = udg[x-i+n] = 1
path[x][i] = '.'
dfs(0)
C++代码
#include <iostream>
using namespace std;
const int N = 10;
char g[N][N];
int n;
bool col[N], dg[N * 2], udg[N * 2];
void dfs(int x)
{
if(x == n)
{
for(int i=0; i<n; i++) puts(g[i]);
puts("");
return;
}
for(int i=0; i<n; i++)
{
if(!col[i] && !dg[i + x] && !udg[x - i + n])
{
g[x][i] = 'Q';
col[i] = dg[i + x] = udg[x - i + n] = true;
dfs(x+1);
col[i] = dg[i + x] = udg[x - i + n] = false;
g[x][i] = '.';
}
}
}
int main()
{
cin >> n;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
g[i][j] = '.';
dfs(0);
return 0;
}
牛逼
没有没有哈哈,我是蒟蒻