TZOJ 3522 Checker Challenge(深搜)
描述
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(深搜)的更多相关文章
- TZOJ 3305 Hero In Maze II(深搜)
描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开 ...
- USACO 6.5 Checker Challenge
Checker Challenge Examine the 6x6 checkerboard below and note that the six checkers are arranged on ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 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 ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 深搜+DP剪枝 codevs 1047 邮票面值设计
codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
随机推荐
- linux xargs命令一(与find ls等命令组合)(转)
-p 操作具有可交互性,每次执行comand都交互式提示用户选择 -i -i 选项告诉 xargs 可以使用{}代替传递过来的参数, 建议使用-I,其符合POSIX标准 -I 格式: xargs - ...
- POJ 1269 /// 判断两条直线的位置关系
题目大意: t个测试用例 每次给出一对直线的两点 判断直线的相对关系 平行输出NODE 重合输出LINE 相交输出POINT和交点坐标 1.直线平行 两向量叉积为0 2.求两直线ab与cd交点 设直线 ...
- USACO 2009 Open Treasure Cave /// DFS||并查集 oj26215
题目大意: 输入 p,n,t :p为地点数 判断 t 能否回到源点1 接下来n行 每行输入 a b c: a能到达b和c Sample Input 13 6 76 7 82 3 410 11 128 ...
- 我擦,DELPHI写个浏览器竟然这么容易,我只加了3个控件,写了3句代码。
- vc 获取窗口标题GetWindowText
今天在写一个模块,具体功能是想时刻监控用户当前活动窗口,需要获取窗口标题以及其它相关信息,记得API GetWindowText就是用来做这个的,结果试了半天,有的获取成功了有的获取失败了,而且有关汉 ...
- 0819NOIP模拟测试赛后总结
这次挂得很悲伤. T1.T3我都想到正解了…… 结果T1少看了个条件:20%保证ai互不相等.以为100%…… 然后挂到了20分,赛后加了个set不到1分钟就A掉了.. T2把分骗满跑路. T3sb线 ...
- a common method to rotate the image
/* * clockwise rotate * first reverse up to down, then swap the symmetry * 1 2 3 7 8 9 7 4 1 * 4 5 6 ...
- 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)[转]
上篇文章<深入浅出Mybatis系列(一)---Mybatis入门>, 写了一个Demo简单体现了一下Mybatis的流程.本次,将简单介绍一下Mybatis的配置文件: 上次例子中,我们 ...
- 洛谷P1081——开车旅行
传送门:QAQQAQ 题意注意点: 1.是从前往后走,不能回头 2.小A小B轮流开,先小A开,而小A是到第二近的点(这点调试的时候查了好久) 3.若绝对值差相同海拔低的更近,而第一个询问若比值相同是海 ...
- JavaScript性能优化篇js优化
JavaScript性能优化篇js优化 随着Ajax越来越普遍,Ajax引用的规模越来越大,Javascript代码的性能越来越显得重要,我想这就是一个很典型的例子,上面那段代码因为会被频繁使用, ...