B - B

Time Limit:1000MS     Memory Limit:16000KB     64bit IO Format:%lld & %llu

 

Description

Examine the $6\times 6$ 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 $1$ to $6$:

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 $N$). 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 $N$ ($6\leq N\leq 13$) that is the dimension of the $N\times N$ checkerboard.

Output

The first three lines show the first three solutions found, presented as $N$ 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

题解:雷同于八皇后问题。。只是增加了输出摆放的前三种和摆放办法

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,num,s[],vis[][];;
void search(int cur)
{
int i;
if (cur>n)
{
num++;
if (num<=)
{
for (i=; i<n; i++) printf("%d ",s[i]);
printf("%d\n",s[n]);
}
return;
}
for (i=; i<=n; i++)
{
if(!vis[][i]&&!vis[][cur+i]&&!vis[][cur-i+n])
{
s[cur]=i;
vis[][i]=vis[][cur+i]=vis[][cur-i+n]=;
search(cur+);
vis[][i]=vis[][cur+i]=vis[][cur-i+n]=;
}
}
}
int main()
{
scanf("%d",&n);
memset(s,,sizeof(s));
num=;
search();
printf("%d\n",num);
return ;
}

USACO1.5 Checker Challenge(类n皇后问题)的更多相关文章

  1. 『嗨威说』算法设计与分析 - 回溯法思想小结(USACO-cha1-sec1.5 Checker Challenge 八皇后升级版)

    本文索引目录: 一.回溯算法的基本思想以及个人理解 二.“子集和”问题的解空间结构和约束函数 三.一道经典回溯法题点拨升华回溯法思想 四.结对编程情况 一.回溯算法的基本思想以及个人理解: 1.1 基 ...

  2. USACO 6.5 Checker Challenge

    Checker Challenge Examine the 6x6 checkerboard below and note that the six checkers are arranged on ...

  3. TZOJ 3522 Checker Challenge(深搜)

    描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...

  4. Poj 1321 棋盘问题 【回溯、类N皇后】

    id=1321" target="_blank">棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. USACO 1.5.4 Checker Challenge跳棋的挑战(回溯法求解N皇后问题+八皇后问题说明)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  6. Checker Challenge跳棋的挑战(n皇后问题)

    Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...

  7. USACO training course Checker Challenge N皇后 /// oj10125

    ...就是N皇后 输出前三种可能排序 输出所有可能排序的方法数 vis[0][i]为i点是否已用 vis[1][m+i]为i点副对角线是否已用  m+i 为从左至右第 m+i 条副对角线 vis[1] ...

  8. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  9. N皇后问题2

    Description Examine the  checkerboard below and note that the six checkers are arranged on the board ...

随机推荐

  1. JS JQuery Ajax 跨域 Post Soap webservice

    呵呵 最近做一些HTML5的项目, 对于前段开发, 相信大家会碰到一个常见问题, 那就是Javascript跨域访问的问题. 话不多说 直接重点 当前网站和Webservice部署在同一个domain ...

  2. 如何在COM的IDL文件中include头文件?

    可以使用import语句,如import "x.h"; 则在自动生成的xxx_i.h中将会有include "x.h", 于是x.h就被include到工程中了 ...

  3. eclipse运行内存不足解决办法

    选中所要执行的类,鼠标右键移动到Run As选项,接着选择Run Configurations...选项,在弹出的选项卡中选择Arguments,在VM arguments下面的输入框中输入-Xmx1 ...

  4. windows下计算文件的md和sha值

    在windows下可以使用FCIV命令行工具计算文件的md5和sha值,具体例子如下: FCIV -md5 -sha1 path\filename.ext 例如: FCIV-md5-sha1 c:\w ...

  5. 分布式锁 基于Redis

    分布式锁的实现(基于Redis) 参考:http://www.jb51.net/article/75439.htm http://www.linuxidc.com/Linux/2015-01/1118 ...

  6. 为什么使用spring Struts 等框架开发

    转载自:http://www.cnblogs.com/sharpxiajun/p/3936268.html 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入, ...

  7. PKU 1511 Invitation Cards (SPFA+邻接表)

    题目链接:点击打开链接 题目需要求从原点到所有点的最短距离之和和所有点到原点的最短距离之和,在求所有点到原点最短距离的时候用到了一个技巧:即把图反向,求原点到所有其他点的最短距离,这样用一次SPFA就 ...

  8. HeadFirst设计模式笔记:(六)命令模式 —— 封装调用

    1.概念 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化与不变的因素. 在面向对象的程序 ...

  9. 庖丁解牛FPPopover

    作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di 庖丁解牛FPPopover FPPopover是一个实现Popover控件的开源项目,比标准控 ...

  10. C++通过WIN32 API获取逻辑磁盘详细信息

      众所周知,在微软的操作系统下编写应用程序,最主要的还是通过windows所提供的api函数来实现各种操作的,这些函数通常是可以直接使用的,只要包含windows.h这个头文件. 今天我们主要介绍的 ...