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 ...
随机推荐
- 批量更新mysql表数据
1.批量更新表中某个字段,如果表比较大,每条记录都执行一条update,1s执行10条数据,10万条数据就要1W秒,3个多小时. 2.可以用case when 来实现,模板如下 UPDATE cate ...
- Spring 切面优先级(5)
之前我们提过的应用场景,一个原始对象可能会需要插入多个切面,如果我们按前几篇博客文章介绍的方法完成切面及其通知的注解声明,那么它的执行顺序是怎么样的呢? 本文将介绍AspectJ的切面如何划分优先级 ...
- 使用CEfSharp之旅(3)下载文件 弹出保存框 IDownloadHandler
原文:使用CEfSharp之旅(3)下载文件 弹出保存框 IDownloadHandler 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群 ...
- 在 U-BOOT 对 Nand Flash 的支持
1.1 U-BOOT 对从 Nand Flash 启动的支持 1.1.1 从 Nand Flash 启动 U-BOOT 的基本原理 1. 前 4K 的问题 如果 S3C2410 被配置成从 ...
- Nand Flash 控制器中的硬件 ECC 介绍
ECC 产生方法 ECC 是用于对存储器之间传送数据正确进行校验的一种算法,分硬件 ECC 和软件 ECC 算法两种,在 S3C2410 的 Nand Flash 控制器中实现了由硬件电路(ECC 生 ...
- Redis单机和集群配置(版本在5.0后)
摘抄并用于自己后查 单机版的配置: 1. 下载redis压缩包,然后解压缩文件(tar xzf): 2. 进入解压后的redis文件目录,编译redis源文件(make,没有c环境要gcc): 3. ...
- JS获取url参数,修改url参数
function getURL(){ var args = {}; var query = location.search.substring(1); //获得了当前链接的中?号后的参数 var pa ...
- Python全栈开发:初识Python
Pythton简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...
- springboot中activeMQ消息队列的引入与使用(发送短信)
1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...
- LoadRunner中字符串的操作
LoadRunner中字符串的操作 LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); ...