描述

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. C# EF去除重复列DistinctBy

    在网上看了LinQ有DistinctBy方法,实际在用的时候并没有找到,后来参照了该网站才发现写的是拓展方法 https://blog.csdn.net/c1113072394/article/det ...

  2. java笔试之尼科彻斯定理

    验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和. 例如: 1^3=1 2^3=3+5 3^3=7+9+11 4^3=13+15+17+19 这题也可以用数学公式推理,首项m*(m ...

  3. Leetcode187. Repeated DNA Sequences重复的DNA序列

    所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...

  4. [sonata admin] argument "$code" of method

    2. CREATING AN ADMIN 按照这里,在 AppBundle中的Controller创建了 CategoryAdmin 类,当运行 php bin/console server:star ...

  5. div contenteditable 重新编辑时focus光标定位到前面问题解决

    <div class="editdiv" id="edit" contenteditable="true">这是添加文字< ...

  6. thinkphp 视图定义

    视图定义 视图通常是指数据库的视图,视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的 ...

  7. 新书《iOS应用逆向工程:分析与实战》

    前无古人!小白福音!国内第一本iOS应用逆向工程类图书<iOS应用逆向工程:分析与实战>就要空降啦~! 你是否曾因应用上线的第一天即遭破解而无奈苦恼,想要加以防范,却又束手无策? 你是否曾 ...

  8. java中自己对页面跳转问题的一些经验

    在eclipse中,如果你要在jsp页面跳转到servlet页面中,可以用action=“/根文件名/servlet文件名” 的方式跳转. 例如我创建了一个web application名字是test ...

  9. Python GUI文本编辑器

    使用Python编写一个简单的文本编辑器,需要展示一个用户界面,功能包括打开.保存文本文件. 使用tkinter库来编写GUI. #简单GUI文本编辑器 from tkinter import * f ...

  10. springboot自己实现mysql主从数据切换机制

    在很多公司都是实现了数据的读写分离,所谓的读写分离,就是写的时候从主库 ,然后从库就会从主库中复制过去,这样就会形成了数据的读写分离,然而在很多场景是适用的,那么我们怎么做呢,可以利用aop 加注解的 ...