N皇后问题2
Description
Examine the 

checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)
1 2 3 4 5 6
-------------------------
1 | | O | | | | |
-------------------------
2 | | | | O | | |
-------------------------
3 | | | | | | O |
-------------------------
4 | O | | | | | |
-------------------------
5 | | | O | | | |
-------------------------
6 | | | | | O | |
-------------------------
The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from
to
:
ROW 1 2 3 4 5 6
COLUMN 2 4 6 1 3 5
This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of
). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.
Input
A single line that contains a single integer
(




) that is the dimension of the 

checkerboard.
Output
The first three lines show the first three solutions found, presented as
numbers with a single space between them. The fourth line shows the total number of solutions found.
Sample Input
6
Sample Output
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
题意:N皇后问题,需要输出前三种排放和可以排放的方式的数量
解题思路:只需要记数输出三次,其他的和N皇后一样......(这里用二维数组来判断是否同列同斜线...防超时)
代码如下:
#include <stdio.h>
int n,t=,vis[][],c[];
void dfs(int cur)
{
if(cur==n+)
{
t++;
if(t<)
{
for(int i=; i<=n-; i++)
printf("%d ",c[i]);
printf("%d\n",c[n]);
}
}
else
{
for(int i=; i<=n; i++)
{
if(!vis[][i]&&!vis[][cur+i]&&!vis[][cur-i+n]) //判断是否和前面的皇后冲突
{
c[cur]=i;
vis[][i]=vis[][cur+i]=vis[][cur-i+n]=;
dfs(cur+);
vis[][i]=vis[][cur+i]=vis[][cur-i+n]=;
}
}
}
}
int main()
{
scanf("%d",&n);
dfs();
printf("%d\n",t);
return ;
}
N皇后问题2的更多相关文章
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...
- 八皇后算法的另一种实现(c#版本)
八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- N皇后问题—初级回溯
N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...
- 数据结构0103汉诺塔&八皇后
主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...
- N皇后问题
题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...
- LeetCode:N-Queens I II(n皇后问题)
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- 八皇后问题_Qt_界面程序实现
//核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...
- 两个NOI题目的启迪8皇后和算24
论出于什么原因和目的,学习C++已经有一个星期左右,从开始就在做NOI的题目,到现在也没有正式的看<Primer C++>,不过还是受益良多,毕竟C++是一种”低级的高级语言“,而且NOI ...
随机推荐
- Java(Android)编程思想笔记01:多态性的理解
1. 多态的定义: 指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同的行为方式. (发送消息就是函数调用) 2. 多态的理解 多态是面向对象的重要特性,简 ...
- Android(java)学习笔记75:匿名内部类的方式实现多线程程序
二话不说,首先利用代码体现出来,给大家直观的感觉: package cn.itcast_11; /* 4 * 匿名内部类的格式: 5 * new 类名或者接口名() { 6 * 重写方法; 7 * } ...
- 信号量和PV操作写出Bakery算法的同步程序
面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...
- 常用html演示模板
--学习测试新的css,js,经常用到test.html,在这里放一个: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
- linux下获取ip
如果打开虚拟机 没有ip置灰显示了 lo 可以使用dhclient自动获取ip 如果想开机就自动获取ip: vim /etc/rc.d/rc.local 在这里插入dhclient命令
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- MapReduce的方式进行HBase向HDFS导入和导出
附录代码: HBase---->HDFS import java.io.IOException; import org.apache.hadoop.conf.Configuration; imp ...
- Laravel 中查询 where 记录
Laravel 的 Eloquent 使用 Between $query->whereBetween('age',[$from,$to]) 这是生成 And between ... and .. ...
- jquery 60秒倒计时(方法二)
<script type="text/javascript">var wait=60;document.getElementById("btn"). ...
- this指针在不同情况下的指代
说不同情况了吧,首先要分有几种情况使用this,然后再说分别指代什么 1)如果是一般标签下函数调用,this指代全局对象,也就是window对象或者document对象 2)如果在嵌套函数中被嵌套的 ...