当前位置:网站首页>二维数组实现八皇后问题
二维数组实现八皇后问题
2022-08-09 14:49:00 【云海谷天】
之前关八皇后的问题全部使用的是一维数组进行实现(http://www.cnblogs.com/SeaSky0606/p/4604955.html),现改一种数据存储方式,按照8x8的二维棋盘存储皇后。基本逻辑不变,可参见如下代码:#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define N 8
int q[N][N],cnt=0;
void dfs(int x);
void placeon(int x,int y);
void takeout(int x,int y);
int canplace(int x,int y);
void ps();
int main(){
dfs(0);
printf("There are %d schema(s) in total!",cnt);
return 0;
}
void dfs(int x){
if(x==N){
cnt++;
ps();
}else{
for(int i=0;i<N;i++) if(canplace(x,i)){
placeon(x,i);
dfs(x+1);
takeout(x,i);
}
}
}
void placeon(int x,int y){
q[x][y]=1;
}
void takeout(int x,int y){
q[x][y]=0;
}
//key
int canplace(int x,int y){
for(int i=1;i<=x;i++){
if((y+i<N &&(q[x-i][y+i]==1)) ||(y-i>=0 && (q[x-i][y-i]==1)) || q[x-i][y]==1){
return 0;
}
}
return 1;
}
void ps(){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(q[i][j]==1)printf("A");
else
printf(".");
}
printf("\n");
}
printf("\n");
}边栏推荐
- 6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
- spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
- Zero Time Technology | Nomad cross-chain bridge theft of 180 million US dollars incident analysis
- 外贸软件如何提升进出口公司业绩 实现降本增效
- SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
- 半自动爬虫
- 概率论基础知识整理 | 随机向量
- 下班后用微信工作发病是否属于工伤?法院这样判
- len()的使用
- 代码随想录笔记_动态规划_377组合总和IV
猜你喜欢

Simulink simulation pid control servo system

代码随想录笔记_动态规划_377组合总和IV

DSPE-PEG-Hydrazide,DSPE-PEG-HZ,磷脂-聚乙二醇-酰肼MW:1000

After reading the "Redis In-depth Notes" compiled by Tencent bosses in 90 days, I worshipped on the spot.

基于FPGA的FIR滤波器的实现(2)—采用kaiserord & fir2 & firpm函数设计

Unity Obi插件修改到支持URP

测试工程师,看不上年薪20w,原因居然是...

物联网技术概论:1~7章汇总(西安交通大学)
![[Serilog] Simple .NET logging with fully structured events](/img/10/3a1f58946129246e42e2f7b3ca3f9b.png)
[Serilog] Simple .NET logging with fully structured events

Regular expression combat: the latest Douban top250 crawler super detailed tutorial
随机推荐
暴雨天,看天翼云如何“快准稳”防涝
6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
Zero Time Technology | Nomad cross-chain bridge theft of 180 million US dollars incident analysis
Refuse to "reinvent the wheel", Baidu EasyDL lets you play with AI custom development
【LeetCode】1898. 可移除字符的最大数目
Hudi Spark-Shell 实战
论文笔记CIRS
兆骑科创创新创业大赛平台,人才引进,项目路演
概率论基础知识整理 | 随机向量
Selenium - 如何用xpath快速定位路径?
Use Baidu EasyDL to realize intelligent identification of health code/travel code in 30 minutes
第五讲 测试技术与用例设计
Simulink simulation pid control servo system
【磁场建模项目2020-02-Lilin】采集板硬件规范
基于微信云开发的幼儿园招生报名小程序
【OpenGL】三、OpenGL总结:OpenGL坐标系
Unity Obi插件修改到支持URP
After reading the "Redis In-depth Notes" compiled by Tencent bosses in 90 days, I worshipped on the spot.
极限挑战,如何做到分钟级搭建环境?
下班后用微信工作发病是否属于工伤?法院这样判