九度oj 题目1140:八皇后
- 题目描述:
-
会下国际象棋的人都很清楚:皇后可以在横、竖、斜线上不限步数地吃掉其他棋子。如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题。
对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数。已经知道8皇后问题一共有92组解(即92个不同的皇后串)。
给出一个数b,要求输出第b个串。串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小。
- 输入:
-
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数b(1 <= b <= 92)
- 输出:
-
输出有n行,每行输出对应一个输入。输出应是一个正整数,是对应于b的皇后串。
- 样例输入:
-
2
1
92
- 样例输出:
-
15863724
84136275#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAX 10
#define inf 1000000009
int mat[MAX][MAX];
int temp[MAX];
int ans[][MAX];
int cnt = ; bool isOk(int x, int y) {
int a = , b = ;
for(int i = ; i < ; i++) {
a = a + mat[x][i];
b = b + mat[i][y];
if(a >= || b >= ) {
return false;
}
}
for(int i = x - , j = y-; i >= && j >= ; i--, j--) {
if(mat[i][j] == ) {
return false;
}
}
for(int i = x - , j = y+; i >= && j < ; i--, j++) {
if(mat[i][j] == ) {
return false;
}
}
return true;
} void dfs(int n) {
if(n == ) {
cnt++;
for(int i = ; i < ; i++) {
ans[cnt][i] = temp[i];
}
return;
}
for(int i = ; i < ; i++) {
if(isOk(n,i)) {
mat[n][i] = ;
temp[n] = i+;
dfs(n+);
mat[n][i] = ;
}
}
} int main(int argc, char const *argv[])
{
int n;
//freopen("input.txt","r",stdin);
memset(mat, , sizeof(mat));
dfs();
while(scanf("%d",&n) != EOF) {
while(n--) {
int m;
scanf("%d",&m);
for(int i = ; i < ; i++) {
printf("%d",ans[m][i]);
}
puts("");
} }
return ;
}其实,判断是否可以放置的代码还可以利用temp,使其更简洁
#include <cstdio>
#include <string>
#include <cstring>
#define MAX 10
int mat[MAX][MAX];
int temp[MAX];
int ans[][MAX];
int cnt = ; bool isOk(int x, int y) {
for(int i = ; i < x; i++) {
if(temp[i]- == y) {
return false;
}
if(temp[i]-+ i == (x+y) || temp[i] - - i == (y-x)) {
return false;
}
}
return true;
} void dfs(int n) {
if(n == ) {
cnt++;
for(int i = ; i < ; i++) {
ans[cnt][i] = temp[i];
}
return;
}
for(int i = ; i < ; i++) {
if(isOk(n,i)) {
mat[n][i] = ;
temp[n] = i+;
dfs(n+);
mat[n][i] = ;
}
}
} int main(int argc, char const *argv[])
{
int n,m;
memset(mat, , sizeof(mat));
dfs();
while(scanf("%d",&n) != EOF) {
while(n--) {
scanf("%d",&m);
for(int i = ; i < ; i++) {
printf("%d",ans[m][i]);
}
puts("");
}
}
return ;
}
九度oj 题目1140:八皇后的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- 使用POI创建word表格-在表格单元格中创建子表格
要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...
- Linux SSH无密码login
一:ssh原理图为: 1.就是为了让两个linux机器之间使用ssh不需要用户名和密码.采用了数字签名RSA或者DSA来完成这个操作 2.模型分析 假设 A (192.168.20.59)为客户机器, ...
- es的插件 ik分词器的安装和使用
今天折腾了一天,在es 5.5.0 上安装ik.一直通过官方给定的命令没用安装成功,决定通过手工是形式进行安装.https://github.com/medcl/elasticsearch-analy ...
- python实现微信打飞机游戏(by crossin)
# -*- coding: utf-8 -*- import pygame from sys import exit import random pygame.init() screen = pyga ...
- make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)
跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...
- EF+linq的增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- 如何写好一个vue组件,老夫的一年经验全在这了【转】 v-bind="$attrs" 和 v-on="$listeners"
如何写好一个vue组件,老夫的一年经验全在这了 一个适用性良好的组件,一种是可配置项很多,另一种就是容易覆写,从而扩展功能 Vue 组件的 API 来自三部分——prop.事件和插槽: prop 允许 ...
- java web.xml被文件加载过程及加载顺序小结
web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...
- CPP-基础:关于内存分配
1:c中的malloc和c++中的new有什么区别 (1)new.delete 是操作符,可以重载,只能在C++中使用.(2)malloc.free是函数,可以覆盖,C.C++中都可以使用.(3)ne ...
- Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs
http://codeforces.com/contest/476/problem/A A. Dreamoon and Stairs time limit per test 1 second memo ...