[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 ...
随机推荐
- Lintcode--004(最小子串覆盖)
给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"",如果有多个 ...
- Android从相册中获取图片以及路径
首先是相册图片的获取: private final String IMAGE_TYPE = "image/*"; private final int IMAGE_CODE = 0; ...
- BZOJ 2527 Meteors
http://www.lydsy.com/JudgeOnline/problem.php?id=2527 思路:整体二分 #include<cstdio> #include<cmat ...
- 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)
使用最新版本的百度地图需要注意的几个地方: 1.libs文件夹下要有android-support-v4.jar.baidumapapi_v2_1_0.jar.locSDK_3.1.jar三个jar包 ...
- Polymorphism & Overloading & Overriding
In Java, a method signature is the method name and the number and type of its parameters. Return typ ...
- 新闻:型牌男装:网上订服装,如何将返修率降到5个点以下 | IT桔子
新闻:型牌男装:网上订服装,如何将返修率降到5个点以下 | IT桔子 型牌男装:网上订服装,如何将返修率降到5个点以下
- 常用的js对象扩展方法
1. 字符串的replaceAll String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!R ...
- python高级编程:有用的设计模式3
# -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#访问者:有助于将算法从数据结构中分离出来"&qu ...
- (转)iOS7人机界面设计规范 - 目录
英文原文出自苹果官方的iOS7设计资源-iOS人机界面设计规范(预发布版本),由C7210自发翻译,并首发于Beforweb.com.如需转载,请注明译者及出处信息. UI设计基础 为iOS7而设计 ...
- springMVC之annotation优化
1.在之前配置的spring配置文件中会有这样的代码: <!-- 方法映射 --> <bean class="org.springframework.web.servle ...