UVa 750 - 8 Queens Chess Problem
题目大意:八皇后问题,在一个8*8的棋盘上,放置8个皇后,使得任意两个皇后不在同一行上、不在同一列上、不在同一条对角线上,不过这道题预先给定了一个位置放置一个皇后,让你输出所有可能的答案。
经典的回溯问题,具体可参考《算法竞赛入门经典》7.4.1,不过这道题对输出的要求说的挺模糊的,要特别注意输出的格式。开始不知道,就WA了一次...
#include <cstdio>
#include <cstring>
#define N 8 bool vis[][*N];
int r, c, kase;
int ans[N]; void search(int cur)
{
if (cur == N)
{
printf("%2d ", ++kase);
for (int i = ; i < N; i++)
printf("%d%s", ans[i]+, (i==N-) ? "\n" : " ");
return;
}
if (cur == c) search(cur+);
else
{
for (int i = ; i < N; i++)
if (!vis[][i] && !vis[][cur-i+N] && !vis[][cur+i])
{
ans[cur] = i;
vis[][i] = vis[][cur-i+N] = vis[][cur+i] = ;
search(cur+);
vis[][i] = vis[][cur-i+N] = vis[][cur+i] = ;
}
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &r, &c);
r--;
c--;
memset(vis, , sizeof(vis));
vis[][r] = ;
vis[][c-r+N] = ;
vis[][c+r] = ;
ans[c] = r;
kase = ;
printf("SOLN COLUMN\n");
printf(" # 1 2 3 4 5 6 7 8\n\n");
search();
if (T) printf("\n");
}
return ;
}
为了提高时间,可以预先打表,貌似只有92种方案,然后从中选择就可以了。
UVa 750 - 8 Queens Chess Problem的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa 11389 - The Bus Driver Problem 难度:0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- uva 100 The 3n + 1 problem (RMQ)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- UVa 1640 (计数) The Counting Problem
题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...
- 【UVA 1380】 A Scheduling Problem (树形DP)
A Scheduling Problem Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...
- UVA 11389 The Bus Driver Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82842#problem/D In a city there are n bus ...
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
随机推荐
- 介绍Python程序员常用的IDE和其它开发工具
概述 “工欲善其事,必先利其器”,如果说编程是程序员的手艺,那么IDE就是程序员的吃饭家伙了. IDE 的全称是Integration Development Environment(集成开发环境), ...
- g1gc
http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All http://blog.csdn.net/renfufe ...
- iOS 一招搞定去掉字符串开始的0,尤其是针对时间格式化
// 2.利用整型自动去掉开头的0, 不要循环和判断prefix以及截取字符串- (NSString *)pp_formatDateWithArrYMDToMD;{ NSInteger mont ...
- C# 经典入门12章-System.Collections.Generic命名空间
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAAsCAIAAAAl09PEAAAgAElEQVR4nOx95Vscyd7285cMPrg7Aw ...
- 关于Ubuntu下apt的一些用法及和yum的比较
Fedora和Red Hat有yum安装软件,Ubuntu有apt工具. apt简单的来说,就是给Ubuntu安装软件的一种命令方式. 一.apt的相关文件 /etc/apt/sources.list ...
- Java中的字符串分割 .
转自 http://blog.csdn.net/yuwenhao0518/article/details/7161059 http://longkm.blog.163.com/blog/static/ ...
- java中基本数据类型和C语言中基本数据类型转换
java中 1 short = 2 byte 1 char = 2 byte 1 int = 4 byte 1 long = 8 byte C语言中 typedef unsigned char ...
- Android音频系统之AudioPolicyService
地址:http://blog.csdn.net/edmond999/article/details/18599327 1.1 AudioPolicy Service 在AudioFlinger小节,我 ...
- PAT (Advanced Level) 1015. Reversible Primes (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- Java网络通信——XML和JSON
XML(Extensible Markup Language) 定义:一种可扩展的标记性语言 XML有丰富的编码工具,比如Dom4j.JDom等. JSON(JavaScript Object Not ...