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 * * [问 ...
随机推荐
- R语言——基本绘图函数
通过一个综合的例子测试绘图函数 学习的内容是tigerfish老师的教程. 第一节:基本知识 用seq函数产生100位学生的学号. > num = seq(,) > num [] [] [ ...
- C++:string类的使用
类 <string> std::string String类的定义 , 其也是个模板类 typedef basic_string<char> string; String cl ...
- 判断浏览器增加标签 encodeURIComponent
var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; var lx; (s = ua.match(/msie ([\d.]+ ...
- 转 sqlplus执行sql报错:ORA-01756:
1.sqlplus执行sql报错:ORA-01756: quoted string not properly terminated 分类: 技术 在SQLPLUS中执行SQL文件时 ...
- Struts+Spring+Hibernate的Web应用执行过程
struts1和spring有两种整合的方法 一种是action和spring bean映射:一种是将action交给spring初始化 第一种方式:访问.do的URL->tomcat接收到r ...
- 防止aspx木马的IIS SPY变态功能
防止aspx木马的IIS SPY变态功能 如果服务器支持aspx语言,而且被上传了aspx木马,利用木马里面的IIS SPY 功能,可以读出IIS里面的所有用户的密码,包括用IIS做FTP的,也能读出 ...
- 上传数据插件 Easy Populate 遇到问题 ?langer=remove
批量上传,出现如下错误提示: ADD NEW PRODUCT FAILED! – Model: no200 – SQL error. Check Easy Populate error log in ...
- Zencart 500错误查找和解决方法
有时在一些虚拟主机下(如HostMonster,BlueHost),Zencart网站会莫名奇妙输出空白页面,查看HTTP头,其实可以看到是500错误.至于500错误的出现原因,一般是由于服务器脚本或 ...
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
web.xml中的加载顺序为:listener > filter > servlet > spring. 其中filter的执行顺序是filter-mapping在web.xml中出 ...
- Java之Property类使用
http://blog.csdn.net/hopestar2/article/details/6372883 在项目中经常用到各种配置文件,有.properties的,也有.xml格式的 都可以通过j ...