题目大意:八皇后问题,在一个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. ibdata1文件--缩小mysql数据库的ibdata1文件

    摘要 在MySQL数据库中,如果不指定innodb_file_per_table参数,单独存在每个表的数据,MySQL的数据都会存放在ibdata1文件. mysql ibdata1存放数据,索引等, ...

  2. css叠加原则,就近原则

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  3. js选择一个选项 跳出另一个选项 跳出一个输入框

    跳出输入框 <script language="javascript"> function $(obj){return document.getElementById( ...

  4. CentOS下自动登陆root帐户

    1 vi /etc/pam.d/gdm 把 auth required …… root quiet这行注释掉 2 vi /etc/pam.d/gdm-passwd 同上 3 vi /etc/gdm/c ...

  5. div定位

    1.float定位带来的问题<html> <head> <title>div浮动引发的问题</title> </head> <styl ...

  6. XML字符串解析成对象的时候应注意空格

    BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头

  7. HDU 4287 Intelligent IME(字典树)

    在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...

  8. Hadoop集群搭建的密钥配置SSH实现机制的配置(2)

    [hadoop@weekend110 ~]$ ssh-keygen -t rsa 用来生产密钥对 Generating public/private rsa key pair. Enter file ...

  9. PAT (Advanced Level) 1073. Scientific Notation (20)

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

  10. 【hihoCoder 第133周】2-SAT·hihoCoder音乐节

    http://hihocoder.com/contest/hiho133/problem/1 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #inclu ...