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. 从大学开始学C++到现在的一些感悟

    Since I choose this road, I will not regret it.     --Mereyct 端午过后的第二天,闲着没事,更新一下博客. 写这个博客的原因是,看到了群里有 ...

  2. selenium grid 测试资料

    像风一样自由的4篇博客: http://blog.csdn.net/five3/article/details/9671287 http://blog.csdn.net/five3/article/d ...

  3. zznu 1068: 进制转换

    进制应该属于程序员的看家本事了,也是大家水平告别菜鸟的一个转折,所以进制转换题目是很有意义的, 这个题目是最简单的把二进制数化简成十进制,因为输入有可能有31位,所以无法使用int或者long lon ...

  4. 怎么用copy关键字

    出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙,目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 用途: NSString.NSArray.NS ...

  5. [置顶] [混迹IT职场系列]一、转正的那些事儿

    讲起转正,是每个IT人进入职场后要面对的第一关,只有越过这第一关卡才能更加顺利玩弄职场或被职场玩弄或互相玩弄. 很多人觉得转正只需自身努力即可,譬如有句话叫做 “只要功夫深,铁针磨成棒”.其实不然,职 ...

  6. 缓存淘汰算法 (http://flychao88.iteye.com/blog/1977653)

    1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. ...

  7. oracle 32位和64位的问题

  8. php如何同时连接多个数据库

    下面是一个函数能够保证连接多个数据库的下不同的表的函数,可以收藏一下,比较实用,测试过是有用的. function mysql_oper($oper,$db,$table,$where='1',$li ...

  9. Java 获取今天之前的七天 的日期

    package test; import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; pub ...

  10. PNG文件格式具体解释

      PNG文件结构分析(上:了解PNG文件存储格式) 前言 我们都知道,在进行J2ME的手机应用程序开发的时候,在图片的使用上,我们能够使用PNG格式的图片(甚至于在有的手机上,我们仅仅能够使用PNG ...