当前位置:网站首页>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;
}
以上!
希望能给你一点帮助。
感谢
边栏推荐
- 【[第一次写博客]Uda课程中的P控制器实现说明】
- 6.2 function-parameters
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
- Soft link & hard link
- SM整合原来这么简单,步骤清晰(详细)
- Adb常用命令列表
- VirtualBox has expanded the capacity of virtual hard disk (without modifying the original data)
- 什么是_GLIBCXX_VISIBILITY(default)
- The latest tank battle 2022 full development notes-1
猜你喜欢

Qt版的贪食蛇游戏项目

js(forEach)出现return无法结束函数的解决方法

6.3 references

预约中,2022京东云产业融合新品发布会线上开启
![[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

C语言宏#define命令练习

Getting started with arfoundation tutorial 10- plane detection and placement

Live broadcast Preview: integration of JD cloud Devops and jfrog product library

最新坦克大战2022-全程开发笔记-2

C语言数组入门到精通(数组精讲)
随机推荐
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
京东云金秋上云特惠进行中!扫码参与活动
京东云分布式链路追踪在金融场景的最佳实践
200 多家 ISV 入驻!阿里云计算巢发布一周年
Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
Unity3d - the object is too far away to see
MySQL的详细安装使用教程(保姆式安装图文讲解)
Qt版的贪食蛇游戏项目
Learn the first program of database
vs2019编译cryengine失败问题处理
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
MFC集成qt验证及问题处理
ARFoundation从零开始9-AR锚点(AR Anchor)
C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
Visual Basic .Net 如何获取命令参数
ARFoundation从零开始8-Geospatial API(地理空间)开发
Rolabelimg to data format data
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
osg进阶-序
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com