[OpenJudge] 百练2754 八皇后
八皇后
- Description
- 会下国际象棋的人都很清楚:皇后可以在横、竖、斜线上不限步数地吃掉其他棋子。如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题。
对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数。已经知道8皇后问题一共有92组解(即92个不同的皇后串)。
给出一个数b,要求输出第b个串。串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小。 - Input
- 第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数b(1 <= b <= 92)
- Output
- 输出有n行,每行输出对应一个输入。输出应是一个正整数,是对应于b的皇后串。
- Sample Input
-
2
1
92 - Sample Output
-
15863724
84136275 题解:回溯法的应用。注意判断对角线之前是否存在皇后的方法。 题目地址:http://bailian.openjudge.cn/practice/2754/ 代码:#include<stdio.h>
#include<string.h>
#include<stdbool.h> int i,j,n,m,num,
a[],b[][]; bool f[][]; int
pre()
{
memset(f,,sizeof(f));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
num=;
return ;
} void
dfs(int x)
{
int i;
if(x==)
{
num++;
for(i=;i<=;i++)
b[num][i]=a[i];
} for(i=;i<=;i++)
if (f[][i]&&f[][x+i]&&f[][x-i+])
{
a[x]=i;
f[][i]=f[][x+i]=f[][x-i+]=;
dfs(x+);
f[][i]=f[][x+i]=f[][x-i+]=;
}
} int
main()
{
int cas,i;
scanf("%d",&cas);
pre();
dfs();
while(cas--)
{
scanf("%d",&n);
for(i=;i<=;i++)
printf("%d",b[n][i]);
printf("\n");
}
return ;
}
[OpenJudge] 百练2754 八皇后的更多相关文章
- [poj百练]2754:八皇后 回溯
描述 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后 ...
- OpenJudge 2754 八皇后
1.链接地址: http://bailian.openjudge.cn/practice/2754 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 会下国际象棋的人都很清楚: ...
- Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...
- Poj OpenJudge 百练 1860 Currency Exchang
1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...
- Poj OpenJudge 百练 2602 Superlong sums
1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- Poj OpenJudge 百练 2632 Crashing Robots
1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...
- Poj OpenJudge 百练 Bailian 1008 Maya Calendar
1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Ca ...
随机推荐
- LeetCode_Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 由于 UNION ALL Chinese_PRC_CI_AS”之间的排序规则冲突,值的排序规则未经解析
由于不同的表之间的排序规则不一样,在归并集合的 时候会出现排序问题. 只要在查询的列后面 声明结果列的排序规则保持一致即可: SELECT b0.[CardCode] collate SQL_Lat ...
- VC的话有必要认真听,但却不用急着照办
本文来自著名风险投资人 Fred Wilson 的博客 AVC,他在 2016 年 8 月 23 日的这篇文章<Understanding VCs>里用简单的语言揭秘了 VC(风险投资人) ...
- Display number of replies in disscussion board
how to display number of replies in disscussion board I have a require about display the replies' nu ...
- 手动同步chrome浏览器
chrome浏览器每次设置好的标签在重新开机后都会变回设置前的状态,崩溃,每次设置好后还是手动同步一下吧. 1. 点击 工具(右上角的三个点)-->设置 2. 点击 高级同步设置 3. 点击 使 ...
- Javascript: 截取字符串多出来并用省略号[...]显示
/背景知识/ substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数描述 start 必需.一个非负的整数 ...
- 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- highcharts的使用
步骤: 1. 去highcharts官网下载最新版本 2. 在.aspx页面添加引用 例: <link href="../JS/highcharts/css/highslide.css ...
- "ORA-00942: 表或视图不存在 "的原因和解决方法[转]
采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或视图不存在 ”,很是郁闷,这个问题以前出现过,当初解决了,但因好久没有使用,这次竟然 ...
- IOS 代码管理工具
代码管理工具国内主要用的是SVN 国外主要是Git