当前位置:网站首页>C语言 N皇后问题
C语言 N皇后问题
2022-07-29 05:08:00 【YiHeboy】
C语言 N皇后问题
介绍:什么是N皇后问题
- 国际象棋中,皇后是一个很强力的单位,可以朝着以皇后棋子本身位置为中心,横向和纵向以及 斜率为1和-1的方向上移动,N 皇后问题是指在 n * n 的棋盘上要摆 n 个皇后,并且使得任何两个 皇后不同行,不同列也不在同一条斜线上。
这个问题我用的是回溯的方法:
以4x4的棋盘为例:
我们先在第一行取第一列的位置,此位置没有与其他皇后行走范围冲突,因此可以放
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | ||
| 3 | q | q | ||
| 4 | q | q |
第二行同样从第一列开始取,当取到第一列,冲突,第二列,冲突,第三列不冲突,放置第二枚皇后
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | Q | q |
| 3 | q | q | q | q |
| 4 | q | q | q |
第三行重复第二行的操作,但是发现任何列都会冲突,因此不能放,我们回到第二行,将皇后向下一列放置
再来到第三行此时第二列没有冲突,则可以放
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | Q | q | q | q |
| 2 | q | q | q | Q |
| 3 | q | Q | q | q |
| 4 | q | q | q | q |
我们发现第四行又没有可选择的了。第一次重试失败,回溯上一级,无可用列,再回溯上一级,无可用列,最后返回第一行。
第二次重试
我们重新回到第一步,这说明我们之前第一行选择第一列是无解的,所以我们第一行不应该选择第一列,我们再来选择第二列来试试,以此类推
| 1 | 2 | 3 | 4 | |
|---|---|---|---|---|
| 1 | q | Q | q | q |
| 2 | q | q | ||
| 3 | q | q | ||
| 4 | q |
代码实现如
#include <bits/stdc++.h>//偷懒用了C++的万能头,其实和C没区别,.C文件编译不通过就换C的头
using namespace std;
bool attack(int *chess, int col, int row)
{
for(int i = 0; i < row; ++i){
if(chess[i] == col){
//纵向冲突
return true;
}
if(col - chess[i] == row - i || chess[i] - col == row - i){
//斜线冲突
return true;
}
}
return false;
}
int nqueen()
{
int queens = 0;
cin>>queens;
int col = 0, cnt = 0, row = 0;
int chess[9] = {
-1};
//题目要求是最多9个皇后,而且网站的编译器不允许数组长度为变量。
while(!(col == queens && row == 0)){
if(!attack(chess,col,row)){
//判断无冲突
chess[row++] = col;
if(row == queens){
//已将所有皇后防止完成
++cnt;
col = chess[--row];//回溯上一行寻找新解
++col;
while(col == queens){
//上一行已经是最后一个的情况则再向上回溯
if(queens == 1){
col = queens;
row = 0;
break;
}
chess[row--] = -1;
col = chess[row];
col++;
}
}
else{
col = 0;
}
}
else{
++col;//有冲突则下一列
while(col == queens && row != 0){
//全列都有冲突则回溯
chess[row--] = -1;
col = chess[row];
col++;
}
}
}
return cnt;
}
int main()
{
int cnt = 0;
cnt = nqueen();
cout<<cnt;
return 0;
}
以上!
希望能给你一点帮助。
感谢
边栏推荐
- Xiaobai high salary shortcut Qt development game Snake
- Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
- CryEngine3 调试Shader方法
- 2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
- OCCT学习003-----MFC单文档工程
- 321,京东言犀×NLPCC 2022挑战赛开赛!
- 小白高薪捷径-Qt开发游戏—贪吃蛇
- APP常用跨端技术栈深入分析
- 三层项目的架构分析及构造方法的参数名称注入
- 什么是_GLIBCXX_VISIBILITY(default)
猜你喜欢

京东云分布式链路追踪在金融场景的最佳实践
![[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start](/img/6e/6b4deeedbfd9d6baa651019f3dabfa.jpg)
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start

Getting started with arfoundation tutorial 10- plane detection and placement

阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂

6.2 function-parameters

How rimworld uploads creative workshops through steamcmd

C语言求字符串的长度

CMU15-213 Malloc Lab实验记录

The latest tank battle 2022 full development notes-1

2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
随机推荐
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
C语言求字符串的长度
Arfoundation starts from zero 9-ar anchor
Modification of annotation based three-tier project and the way of adding package scanning
最新坦克大战2022-全程开发笔记-1
TMUX essays
CMU15-213 Malloc Lab实验记录
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
7.1-default-arguments
Vs code的安装步骤及环境配置
Teardown 解除时间限制的方法
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
QT learning: qdropevent drag event
Adb常用命令列表
C language handwritten qq-ai version
7.3-function-templates
千人规模互联网公司研发效能成功之路
ARFoundation入门教程10-平面检测和放置
京东云金秋上云特惠进行中!扫码参与活动
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球