当前位置:网站首页>2n皇后问题
2n皇后问题
2022-04-23 01:21:00 【OldLeft】
给定一个 n∗n 的棋盘,棋盘中有一些位置不能放皇后。现在要向棋盘中放入 n 个黑皇后和 n 个白皇后,使任意的两个黑皇后都不在同一行、同一列或同一条斜线(包括正负斜线)上,任意的两个白皇后都不在同一行、同一列或同一条斜线(包括正负斜线)上。问总共有多少种放法?n 小于等于 8。
输入格式
输入的第一行为一个整数 n,表示棋盘的大小。
接下来 n 行,每行 n 个 0 或 1 的整数,如果一个整数为 1,表示对应的位置可以放皇后,如果一个整数为 0,表示对应的位置不可以放皇后。
输出格式
输出一个整数,表示总共有多少种放法。
输入1
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
输出1
2
输入2
4
1 0 1 1
1 1 1 1
1 1 1 1
1 1 1 1
输出2
0
题解
这题比普通n皇后问题多了一种皇后,那就深搜先放一种皇后,再放另一种皇后,这样仅在结束情况上做更改就好了。另外这题不能放棋子的地方仅在深搜继续的条件上加一个就行了(注:还要注意两种棋子不能放到同一位置上)
代码:
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int vy[10],vd1[20],vd2[20];//判断列,判断左下右上的对角线,判断右下左上的对角线
int mp[10][10];
void dfs(int x,int p){
//p=1代表放置白皇后,p=2代表放置黑皇后
if(x==n&&p==2){
//如果行到了n并且黑皇后也放置完了
ans++;
return ;
}
if(x==n){
//p=1并且放置完了白皇后
dfs(0,p+1);//放置黑皇后
return;
}
for(int i=0;i<n;i++){
if(mp[x][i]&&vy[i]!=p&&vd1[x+i]!=p&&vd2[i-x+n]!=p){
//地图上不为0并且列上没有放置p皇后,两个对角线也没有放置p皇后
mp[x][i]=0;//将地图该点变为0,表示不能放置任何皇后
int q1=vy[i],q2=vd1[i+x],q3=vd2[i-x+n];//记录原来的值
vy[i]=p,vd1[x+i]=p,vd2[i-x+n]=p;//将列对角线标记为p,表示p皇后放了
dfs(x+1,p);//继续放置下一行
mp[x][i]=1;//回溯
vy[i]=q1,vd1[x+i]=q2,vd2[i-x+n]=q3;
}
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&mp[i][j]);
}
}
dfs(0,1);
cout<<ans<<endl;
return 0;
}
版权声明
本文为[OldLeft]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43833610/article/details/115744262
边栏推荐
- Zhang Jian of Huawei cloud IOT expert group: he has become a senior engineer of Huawei since he was 22. The code is what I want to say to the world
- 光猫超级帐号密码,重置光猫获取超级帐号密码
- Let's talk about passive safety again. I'll teach you to understand the rating report of China Insurance Research Institute collision test
- Research and application of Acrel-5000 building energy consumption monitoring system in Xixian Airport Garden Project
- [HCTF 2018]admin
- 智能照明控制系统在医院的设计与应用
- Acrel-2000型电力监控系统在兴庆坊新兴广场配电所配电回路用电的实时监控和管理
- 那些咸鱼上买来的代码|ssm酒店客房管理系统|买来的源码是否真的可以使用|来自程序员的困惑|玉念聿辉|大丑村吴明辉
- Frequently asked questions about recent BSN development
- Gbase 8s存儲結構簡介及空間管理
猜你喜欢
![Error: permissionerror: [winerror 32] this file is in use by another program and cannot be accessed by the process. Solution of](/img/eb/9031f00e41666941219974ea2e2274.png)
Error: permissionerror: [winerror 32] this file is in use by another program and cannot be accessed by the process. Solution of "+ file path"

2022 penetration job interview (thinking)

Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system

Cai Guoqiang's fireworks NFT debut is as wonderful as fireworks during the day

gin--hello

Is it difficult for girls to learn software testing?

世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
![[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard](/img/cc/bbabc5621b8485d04ffa7d4d5fad2e.png)
[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard

gin--hello

员工公寓楼建设项目电力监控系统的研究与应用
随机推荐
Yyds dry goods counting flag variable rule
C language guessing game and trickery game
[Ethernet switching security] - switch flow control / DHCP snooping / IP source guard
Acrel-3200远程预付费电能管理系统 在福州万宝产业园的应用
Gbase 8s检查点简介
京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。
Is it difficult for girls to learn software testing?
Real time monitoring and management of distribution circuit power consumption of acrel-2000 power monitoring system in xingqingfang Xinxing square distribution substation
Cai Guoqiang's fireworks NFT debut is as wonderful as fireworks during the day
The origin explanation and use example of image pre training model
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
GBase 8s查询处理和优化
Oplg: new generation cloud primary observable best practices
Text justify, orientation, combine text attributes
再谈被动安全 教你看懂中保研碰撞测试的评级报告
App uses the template message from WeChat official account for message push.
Free trial for the first three months! Borui data alarm platform onealert is in progress
[the first contact between Android engineers and smart home products ③] the specific implementation of smartconfig one key distribution network on the hardware side | the specific implementation of es
Redis forgets the password and resets the password
gin--hello