题目大意:八皇后问题,在一个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的更多相关文章

  1. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  2. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. UVa 11389 - The Bus Driver Problem 难度:0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  4. uva 100 The 3n + 1 problem (RMQ)

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

  5. UVA 10245 - The Closest Pair Problem

    Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...

  6. UVa 1640 (计数) The Counting Problem

    题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...

  7. 【UVA 1380】 A Scheduling Problem (树形DP)

    A Scheduling Problem   Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...

  8. 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 ...

  9. UVA 100 - The 3n+1 problem (3n+1 问题)

    100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...

随机推荐

  1. HDU 3552 I can do it!

    脑洞题.http://blog.csdn.net/dgq8211/article/details/7748078 #include<cstdio> #include<cstring& ...

  2. python 从windows上传文件到linux脚本

    import paramiko import datetime import os hostname = '192.168.112.132' username = 'root' password = ...

  3. java反射简解

    1.首先一个问题,什么是类,类是不是对象? 我们总是说我们new一个对象出来 那么我们还没有new的时候,建造的那个java类是不是对象呢? 是 它是java.lang.Class的对象 对于反射我们 ...

  4. 支付宝集成+网站支付+APP支付+手机网站支付

    网站支付宝 1.申请签约后获得相应的pid:208***开头和key 这里说明下pc网站支付采用md5加密所以这里只需要提供pid和key不需要上传公钥. 2.下载即时到账demo http://do ...

  5. 使用iptraf,ifstat查看网络流量

    1. 使用 iptraf iptraf是一个实时查看网络流量的文本屏幕界面工具. 有些系统不一定安装了, 如果是 RHEL,那么就去找安装盘中的 iptraf*.rpm 包安装: 如果是 CentOS ...

  6. 安卓开发之探秘蓝牙隐藏API(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3793172.html 上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API.用过Andr ...

  7. JavaScript:有符号整数与无符号整数相互转化

    确实巧妙:原文http://blog.csdn.net/kandyer/article/details/8241937 <script language="JavaScript&quo ...

  8. 【转】SSL/TLS/WTLS协议原理

    1 SSL(Secure Socket Layer)是netscape公司设计的主要用于web的安全传输协议.这种协议在WEB上获得了广泛的应用.2 IETF(www.ietf.org )将SSL作了 ...

  9. 关于MySql中自增长id设置初始值

    在MySQL数据库,设置了id为自增长类型,但由于程序还处于开发阶段,很容易的会产生错误数据,然后就直接删除了,可是id的自增长值没有跟着减少,这里提示: 如果表中数据没有用,直接删除了数据,自动增长 ...

  10. 让你的MyEclipse具有jquery自动提示

    想让你的MyEclipse支持Jquery的自动提示更简单一些,照下图完成即可:      照上面图示已经完成了Jquery自动提示的配置,此时spket已经有两种AJAX库的自动提示,通过右边的De ...