USACO1.5 Checker Challenge(类n皇后问题)
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皇后问题)的更多相关文章
- 『嗨威说』算法设计与分析 - 回溯法思想小结(USACO-cha1-sec1.5 Checker Challenge 八皇后升级版)
本文索引目录: 一.回溯算法的基本思想以及个人理解 二.“子集和”问题的解空间结构和约束函数 三.一道经典回溯法题点拨升华回溯法思想 四.结对编程情况 一.回溯算法的基本思想以及个人理解: 1.1 基 ...
- USACO 6.5 Checker Challenge
Checker Challenge Examine the 6x6 checkerboard below and note that the six checkers are arranged on ...
- TZOJ 3522 Checker Challenge(深搜)
描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...
- Poj 1321 棋盘问题 【回溯、类N皇后】
id=1321" target="_blank">棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- USACO 1.5.4 Checker Challenge跳棋的挑战(回溯法求解N皇后问题+八皇后问题说明)
Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...
- Checker Challenge跳棋的挑战(n皇后问题)
Description 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子. 列号 0 1 2 3 4 5 6 ...
- USACO training course Checker Challenge N皇后 /// oj10125
...就是N皇后 输出前三种可能排序 输出所有可能排序的方法数 vis[0][i]为i点是否已用 vis[1][m+i]为i点副对角线是否已用 m+i 为从左至右第 m+i 条副对角线 vis[1] ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- N皇后问题2
Description Examine the checkerboard below and note that the six checkers are arranged on the board ...
随机推荐
- Java 热部署深入探索
简介 在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能实现方法体的修改热部署,对于整个类的结构修改,仍然需要重启虚拟机,对类重新加载才能完成更新操作.对于某些大型的 ...
- 关于kali安装vmware的坑,linux套路太深。
http://www.linuxidc.com/Linux/2015-08/122240.htm 但是还有些坑 安装gcc5.4.1 apt-get install gcc-5 gcc-5所在目录 / ...
- 二维码开源库zbar、zxing使用心得
首先说明我的测试场景是“识别打印在纸上的二维码”,在扫描结果中寻找二维码并进行识别,而不是直接让摄像头对着二维码扫描. zbar和zxing用的都是自己从github上clone的c++源码/接口编译 ...
- static作用——The static effect
1)在函数体内,一个被声明为静态的变量在这一函数被调用过程中维持其值不变(该变量存放在静态变量区). 2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它 ...
- [CSS3] CSS :target Selector
The :target selector allows us to interact with a fragment identifier, or hash, in our URL from CSS. ...
- iOS:编译错误Undefined symbols for architecture i386: _OBJC_CLASS_$_XXX", referenced from: error
Undefined symbols for architecture i386: _OBJC_CLASS_$_XXX", referenced from: error 这个意思为无法找到名为 ...
- [转] Are You Making a Big Mistake in This Volatile Market?
Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...
- Ubuntu设置环境变量的几种方法
1.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.1 永久的:需要修改配置文件,变量永久生效. 1.2 临时的:使用export命令声明即可,变量在关闭shell时失效 ...
- 从实践谈iOS生命周期
从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...
- HDU 4607 Park Visit(树的直径)
题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...