Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1.
使用回溯算法解决N皇后问题.
问题描述:
棋盘为N*N的,在该棋盘上放置N个皇后,使得任意两个皇后不在同一行或者同一列或者同一对角线(主对角线和副对角线)。
解题思路:
必定使得棋盘的每一行每一列均存在且只存在一个皇后,所以遍历所有的行,对每一行,遍历所有的列,使用判断条件排除。
直接用visit[][]这个二维数组来判断当前尝试放的位置所在的列(visit[0][column])、对角线1(visit[1][row+column])、对角线2(visit[2][row-column+queen_number])是否已经放了皇后(放了为1,没放为0)。如果没放,那么放上(position[100]用来存储路径,如果不需要打印路径,则不需要该数组);如果放了尝试下一个位置。
注意:BackTrack(row+1)语句执行完以后,表明对于当前的位置已经求得了结果,所以向上一层程序返回的时候,一定将状态改回:“没被访问”。一般的:如果在回溯法中修改了辅助的全局变量,则一定要及时将他们恢复原状。若函数有多个出口,则需要在每个出口的地方恢复被修改的值。
NQueen.cc

#include<iostream>
#include<cstring>  // to use memset
using namespace std;int queen_number, solution_number;
// visit[0][column] = 0 if there is no queen in this column; otherwise = 1;
// visit[1][row + column] and visit[2][row - column + queen_number] are for 2
// kinds of diagonals(/ and \), = 0 if no queen on this line; otherwise = 1.
// row + column & row - column + queen_number can be easily calculated
// by formula: y = k*x + b, which y(row), k(1 or -1), x(column) and b is a constant.
// Note: row - column can be negative, so we add queen_number to guarantee that
// the index is nonnegative.
int visit[3][100];
// position[row] = column: queen in row "row" should be placed in column "column"
// this array is not essential if you don't print solutions.
int position[100];void Output();
void BackTrack(int row);int main()
{while(1){memset(visit, 0, sizeof(visit));  // set all numbers in visit to 0solution_number = 0;cout << "Please input queen_number:\n";cin >> queen_number;BackTrack(0);  // start place queens from [0]rowcout << "Total solutions number is " << solution_number << endl;}
}void BackTrack(int row)
{if(row == queen_number)// we should place queens in [0, queen_number - 1]row, so row equals to queen_number// indicates we have placed all queens, so get a solution and stop backtracking.{solution_number++;Output();}else{// for current row "row", check each column whether can place queen by "visit"// and if can, change according element in "visit" and continue placing queen// in next row "row + 1".for(int column = 0; column < queen_number; column++){if(visit[0][column] == 0 && visit[1][row + column] == 0 &&visit[2][row - column + queen_number] == 0){position[row] = column;visit[0][column] = visit[1][row + column] = visit[2][row - column + queen_number] = 1;BackTrack(row + 1);// after backtrack return, we must retrieve "visit" because we will place queen// in another column for current row by "column++".visit[0][column] = visit[1][row + column] = visit[2][row - column + queen_number] = 0;}}}
}void Output()
{for(int row = 0; row < queen_number; row++){for(int column = 0; column < queen_number; column++){// 1 stand for has queen; otherwise 0cout << ((position[row] == column) ? "1 " : "0 ");}cout << endl;}cout << endl;
}/*
1: 1
2: 0
3: 0
4: 2
5: 10
6: 4
7: 40
8: 92
9: 352
10: 724
*/

Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1.

N-Queen Problem相关推荐

  1. uva 10401 Injured Queen Problem(dp)

    题目链接:10401 - Injured Queen Problem 题目大意:给出一个字符串,要求在n * n(n为字符串的长度)的棋盘上摆放n个受伤的皇后,受伤的皇后只能攻击到同一列和它周围8个格 ...

  2. uva10401Injured Queen Problem(递推)

    题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...

  3. 回溯算法n皇后问题_使用回溯算法的N Queen问题和解决方案

    回溯算法n皇后问题 N-皇后问题 (N - Queen's problem) The n – queen problem is the generalized problem of 8-queens ...

  4. python教程循环语句_Python教程:关于Python 循环语句

    Python 循环语句 本章节将向大家介绍Python的循环语句,程序在一般情况下是按顺序执行的. 编程语言提供了各种控制结构,允许更复杂的执行路径. 循环语句允许我们执行一个语句或语句组多次,下面是 ...

  5. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  6. [C语言]八皇后问题回溯算法

    八皇后问题:在8×8格的国际象棋上摆放八个皇后,任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 将棋盘抽象为4 * 4到20 * 20大小的矩阵 输入样例: 请输入棋盘的大小(4 ...

  7. 【SDUT第11周周赛Problem A】SDUT2576——Queen Collisions

    来源:点击打开链接 由于一些原因,需要在短短的一段时间内速成图论和搜索了=  =,希望能够有一个不错的结果. 这个题是著名八皇后问题的变种,大意就是问在一个棋盘中,照面的皇后有几组(横着竖着斜着都算) ...

  8. zoj 2576 Queen Collisions

    1.http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2576 2.比赛时只想到了暴搜,殊不知有这么 ...

  9. YTU OJ Problem 3013

    YTU OJ Problem 3013 皇后问题(递归) 题目描述 编写一个函数,求解皇后问题:在 n × n 的方格棋盘上,放置 n 个皇后,要求每个皇后不同行.不同列.不同左右对角线. 要求: 1 ...

  10. Openstack : 17、在vmware的centos7虚拟机中通过packstack安装openstack queen版本

    目标: 1研究核心组件nova,neutron,cinder,ironic原理 2安装openstack queen版本 零.环境 1)win10中安装VMware-workstation-full- ...

最新文章

  1. java database connect
  2. 到网站进过的服务器 命令,进入服务器的命令
  3. cmake (2)路径指令
  4. linux安装mysql社区版 rpm_linux系统rpm包安装mysql
  5. JDK 10:FutureTask获取一个toString()
  6. 诺奖文章里面的动图绘制教程来了!!
  7. git clone --depth=1 -b 4.24
  8. python逢7跳过_python实现逢七拍腿小游戏的思路详解
  9. 查看zk状态时报错“Error contacting service. It is probably not running
  10. Web API-定时器
  11. VS中Release模式下生成去掉生成pdb文件
  12. opencv Basic Drawing
  13. lucene查询索引之Query子类查询——(七)
  14. 2022 SpringBoot/SSM的药品售货机平台 H5药品购买商城
  15. Java/JDK下载安装与环境配置(Windows 10 超详细的图文版教程 )
  16. 信用卡欺诈检测建模分析
  17. 【课程笔记】南大软件分析课程—16课时完整版
  18. 获取 jquery 版本号
  19. hightopo学习笔记---入门
  20. HCK哈士奇x可口可乐联名潮酷冰吧,你还没入手吗?

热门文章

  1. VLIW的前世今生:为什么DL加速器都青睐于它
  2. mongodb lbs java_利用mongodb开发lbs应用实践
  3. 白话大数据——大数据算法:白话遗传算法
  4. 不知道图片加文字水印怎么弄?这3个方法自媒体达人必学
  5. 88个word基本使用技巧大全,提升工作效率
  6. visio画箭头、画点线,各种连接头
  7. 【图像处理】PS曲线工具matlab实现 交互自定义灰度映射(Gray Level Transformation)附代码链接
  8. 巴斯大学计算机科学研究生,巴斯大学计算机科学.pdf
  9. python解析mht文件_实现MHT文件格式的解析和内容抽取
  10. 怎样区分线性和非线性_线性与非线性的区别(线性分析、线性模型)