题目大意:八皇后问题,在一个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. robot_framewok自动化测试

    robot_framewok自动化测试 http://wenku.baidu.com/view/691abcaa4b73f242336c5fec.html 接口自动化测试框架设计 http://wen ...

  2. JSP标准标签库(JSTL)--JSTL简介与安装

    对于MVC设计模式来讲,我们一直强调,在一个JSP钟scriptlet代码越少越好,但是只靠以前的概念很难实现,因为标签的开发特别麻烦,所以为了简化标签,也为了让标签更具备一些通用性,所以一般在开发中 ...

  3. CF 476 div2 C

    http://www.codeforces.com/contest/476/problem/C   C. Dreamoon and Sums time limit per test 1.5 secon ...

  4. java中把list列表转为arrayList以及arraylist数组截取的简单方法

    java中把list列表转为arrayList以及arraylist数组截取的简单方法 package xiaobai; import java.util.ArrayList; import java ...

  5. Android安全讲座第九层(二) 内存dump

    近来android上越来越多的应用对自身的保护机制加强了重视,主要表现在几个方面. 1 dex加壳 2 so加壳 3 dex藏在so中,在适当的时候释放. 这是技术上一个进步,并且还有一些专业的公司提 ...

  6. PAT (Advanced Level) 1054. The Dominant Color (20)

    简单题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #inc ...

  7. 【knockoutjs】 Computed VS Pure Computed 区别

    Pure Computed只有当有其他subscriber的时候才会有这个对象,所对应的DOM对象 不激活的时候不存在,这样可以防止内存泄露,在Component等场景下不用担心dispose的问题. ...

  8. ADO。net学习笔记

    来源于网络 1.       SqlConnection(DBConnection)  建立程序与数据库的链接 链接字符串有两种形式: //使用Windows验证  SSPI(安全支持提供程序接口) ...

  9. $(srctree) is not clean, please run 'make mrproper'

    在使用make menuconfig重新配置后,再编译kernel时常会遇到这样的问题: Using /home/likewise-open/BJS/lisa.liu/code/sprdroid4.0 ...

  10. __bridge,__bridge_transfer和__bridge_retained的使用和区别【转载】

    __bridge,__bridge_transfer和__bridge_retained的使用和区别[转载] Core Foundation 框架Core Foundation框架 (CoreFoun ...