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的更多相关文章

  1. 递归实现n(经典的8皇后问题)皇后的问题

    问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...

  2. 八皇后算法的另一种实现(c#版本)

    八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...

  3. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. [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 ...

  5. N皇后问题—初级回溯

    N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...

  6. 数据结构0103汉诺塔&八皇后

    主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...

  7. N皇后问题

    题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...

  8. 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 ...

  9. 八皇后问题_Qt_界面程序实现

    //核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...

  10. 两个NOI题目的启迪8皇后和算24

    论出于什么原因和目的,学习C++已经有一个星期左右,从开始就在做NOI的题目,到现在也没有正式的看<Primer C++>,不过还是受益良多,毕竟C++是一种”低级的高级语言“,而且NOI ...

随机推荐

  1. NODE编程(二)--异步编程技术

    在Node世界里流行两种响应逻辑管理方式,回调和事件监听. 回调通常用来定义一次性响应的逻辑.比如对于数据的查询,可以指定一个回调函数来确定如何处理查询结果. 事件监听器,本质上也是一个回调,不同的是 ...

  2. SQL Server2008创建约束图解 转

    转自 http://www.cnblogs.com/longhs/p/3670307.html SQLServer 中有五种约束, Primary Key 约束. Foreign Key 约束. Un ...

  3. iOS-实现验证码倒计时功能(1)

    验证码倒计时按钮的应用是非常普遍的,该Blog就和你一起来写一个IDCountDownButton来实现验证码倒计时的效果.你可以想使用普通的UIButton类型按钮一样,只需要设置其倒计时时长(若未 ...

  4. 远程管理之VNC

    远程管理的基本概念 ①RDP(remote desktop protocol)协议 远程桌面协议,我们常用的windows操作系统就是的远程桌面管理就是基于该协议的,更多有关RDP协议的可以查看百度百 ...

  5. VMware系统运维(一)安装Esxi

    1.下载ISO文件,开始安装,如图1,直接按回车进行安装. 图1 2.按回车键继续安装,如图2所示 图2 3.选择要安装系统的磁盘,按回车继续,如图3所示 图3 4.选择对应的键盘语言,按回车继续,如 ...

  6. myeclipse自带客户端连接mysql数据库

    一:开启mysql服务器 开始——服务 二:打开myeclipse数据库连接视图 找到DB Browser, 三:新建一个连接 如图: 四:填写相关连接参数,并添加jar包驱动 五:然后一路next, ...

  7. [改善Java代码]让工具类不可实例化

    建议42: 让工具类不可实例化 Java项目中使用的工具类非常多,比如JDK自己的工具类java.lang.Math.java.util.Collections等都是我们经常用到的.工具类的方法和属性 ...

  8. [BigData]关于Hadoop学习笔记第四天(PPT总结)(一)

    课程安排 Partitioner编程** 自定义排序编程** Combiner编程** 常见的MapReduce算法** ---------------------------加深拓展-------- ...

  9. [未完成][Mooc]关于Linxu的总结(一)

    视频1:Linux之前有个Minix(这个是一个教授用来教学用的)开源的,不是编译过的,不能通过QQ.exe找到其源码.后来Linus这个家伙搞了一个Linux.服务器领域超过百分之八十.linux是 ...

  10. hdu 3401 单调队列优化动态规划

    思路: 动态方程很容易想到dp[i][j]=max(dp[i][j],dp[i-w-1][j-k]-k*ap[i],dp[i-w-1][j+k]+k*bp[i]): dp[i][j]表示第i天拥有j个 ...