描述

Examine the 6x6 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.)

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

输入

A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.

输出

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.

样例输入

6

样例输出

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

题意

N*N的棋盘填棋,要求每两个棋子不在同一行同一列同一斜,按字典序输出前3种,再输出总数。

题解

经典爆搜,跟八皇后有点类似,由于13比较慢,可以把表存下来。

代码

 #include<bits/stdc++.h>
using namespace std; int n,h[],l[],out,sum;
int biao[]={,,,,,,,};
bool check(int p,int j)
{
for(int i=p-;i>=;i--)
if(abs(j-h[i])==p-i)
return ;
return ;
}
void dfs(int p)
{
if(out>)return;
if(p==n+)
{
sum++;
if(++out<=)
{
printf("%d",h[]);
for(int i=;i<p;i++)
printf(" %d",h[i]);
printf("\n");
}
return;
}
for(int i=;i<=n;i++)
{
if(!l[i]&&check(p,i))
{
l[i]=;
h[p]=i;
dfs(p+);
h[p]=;
l[i]=;
}
}
}
int main()
{
scanf("%d",&n);
dfs();
printf("%d",biao[n-]);
return ;
}
/*
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
1 3 5 7 2 4 6
1 4 7 3 6 2 5
1 5 2 6 3 7 4
40
1 5 8 6 3 7 2 4
1 6 8 3 7 4 2 5
1 7 4 6 8 2 5 3
92
1 3 6 8 2 4 9 7 5
1 3 7 2 8 5 9 4 6
1 3 8 6 9 2 5 7 4
352
1 3 6 8 10 5 9 2 4 7
1 3 6 9 7 10 4 2 5 8
1 3 6 9 7 10 4 2 8 5
724
1 3 5 7 9 11 2 4 6 8 10
1 3 6 9 2 8 11 4 7 5 10
1 3 7 9 4 2 10 6 11 5 8
2680
1 3 5 8 10 12 6 11 2 7 9 4
1 3 5 10 8 11 2 12 6 9 7 4
1 3 5 10 8 11 2 12 7 9 4 6
14200
1 3 5 2 9 12 10 13 4 6 8 11 7
1 3 5 7 9 11 13 2 4 6 8 10 12
1 3 5 7 12 10 13 6 4 2 8 11 9
73712
*/

TZOJ 3522 Checker Challenge(深搜)的更多相关文章

  1. TZOJ 3305 Hero In Maze II(深搜)

    描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...

  2. USACO 6.5 Checker Challenge

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

  3. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  4. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  5. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  6. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  7. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  8. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  9. 深搜+DP剪枝 codevs 1047 邮票面值设计

    codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description ...

随机推荐

  1. 4_8.springboot2.x嵌入式servlet容器启动原理解析

    问题描述: 什么时候创建嵌入式的Servlet容器工厂? 什么时候获取嵌入式的Servlet容器并启动Tomcat? *获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行ru ...

  2. Python自学--part1

    概要 Python介绍 Python安装 Hello World程序 变量 字符编码 用户输入 pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式while 循环 表达 ...

  3. 关于ueditor 文本框

    遇到一个问题,需要将从ueditor中的获得的带格式的文本,从数据库中取出,在放回到 ueditor中去,但是 文本中\n总是截断字符串,出现 这种情况,后面的字符就不能算到里面去了,程序就报错了. ...

  4. Mybatis Resultmap 简化之超级父类

    我们在写 mybatis多表关联查询的时候 ,要配置  resultmap ,实在太麻烦.而这个超级父类 可以省去我们查询多表时的map public class SuperPojo extends ...

  5. (转)线程池 ExecutorService 详细介绍以及注意点区别

    线程池 ExecutorService 相信java开发都用到,这里做个简单笔记 一 Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池 ...

  6. Python全栈开发:协程代码实例

    协程代码1 #!/usr/bin/env python # -*- coding;utf-8 -*- # 导入协程模块 """ 协程工作原理 ""&q ...

  7. STL vector容器需要警惕的一些坑

    从迭代器中取值切记需要判断是否为空 例如: vector<int> vtTest; vtTest.clear(); if (vtTest.empty()){ ; } ]; 如果没有忘了判断 ...

  8. 洛谷P1081——开车旅行

    传送门:QAQQAQ 题意注意点: 1.是从前往后走,不能回头 2.小A小B轮流开,先小A开,而小A是到第二近的点(这点调试的时候查了好久) 3.若绝对值差相同海拔低的更近,而第一个询问若比值相同是海 ...

  9. kafka分析

    目录 1,kafka简介 2, Kafka Server 2.1,kafka中zookeeper的作用 2.2, Broker 2.2.1,Broker高性能设计 2.2.2,Broker选举机制 2 ...

  10. Python学习day04 - Python基础(2)数据类型基础

    <!doctype html>day04 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...