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 ...
随机推荐
- postgresql使用文档之一 初始化数据存储区
17.2. 创建一个数据库集群(Database Cluster) 在你能做任何事情之前,你必须在磁盘上初始化一块存储空间.我们称这为一个数据库集群(database cluster). 一个Data ...
- Python中 os._exit() sys.exit() exit()区别
Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...
- OneToMany与ManyToOne的属性
供自己查阅,嫌低级的勿喷! 1.OneToMany的属性 ①targetEntity 定义关系类的类型,默认是该成员属性对应的类类型,所以通常不需要提供定义. ②mappedBy 定义类之间的双向关系 ...
- Lucene:QueryParser
作为lucene的Query工具,QueryParser却是最重要的一个.简单的说,QueryParser能够根据用户的输入来进行解析,自动构建合适的Query对象.下面简单总结一下它的实现: 目录 ...
- Gridview导出到Excel
#region #导出到Excel protected void Button2_Click(object sender, EventArgs e) { Respons ...
- NuGet 无法连接到远程服务器-解决方法
一. Entity Framework以下简称EF 安装EF4.3的步骤是首先安装VS扩展 NuGet,然后再使用NuGet安装EF程序包 安装完NuGet就可以安装EF了,有两种方式可以安装EF: ...
- iframe页面改动parent页面的隐藏input部件value值,不能触发change事件。
实现一个依据iframe页面返回充值卡类型不同,安排不同的input部件. 点击选择弹出一个iframe.点击充值卡数据行.返回1.充值卡类型.2.充值卡id(用的UUID).3.充值卡号(字符串). ...
- diy 电脑 重装系统
1\组装好电脑 2\硬盘的红灯只亮一会,找不到 硬盘 .bios里 硬盘设置里 IDE修改为 ACHI即可. 3\老毛桃 \ 用桌面的那个分区软件 分区 100G,300G 4\找到U盘 的iso 文 ...
- 使用ssh对服务器进行登录
一.什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会 ...
- ubuntu 配置android开发环境
本文的下载地址都是androiddevtools,下载地址:http://www.androiddevtools.cn/ 一.安装android sdk 解压文件,全部放到/opt/Java/andr ...