hdu 2553:N皇后问题(DFS遍历,水题)
N皇后问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6905 Accepted Submission(s): 3128
你的任务是,对于给定的N,求出有多少种合法的放置方法。
#include <iostream>
using namespace std; int n,sum;
int ans[],sel[]; int Abs(int n) //求绝对值
{
return n>?n:-n;
} void f(int h) //确定第i行的皇后位置
{
if(h>n){ //成功,计数+1
++sum;
return ;
}
int i,x,y; //(x,y)假定要放置的位置
x = h; //确定纵坐标
for(y=;y<=n;y++){ //确定横坐标
//检测竖直方向,横着的方向就不用检测了,因为是一行行来的
for(i=;i<x;i++)
if(y==sel[i])
break;
if(i<x) //失败
continue;
//检测斜着的方向
for(i=;i<x;i++)
if(Abs(sel[i]-y)==x-i)
break;
if(i<x) //失败
continue; sel[x] = y; //通过检测,存储当前位置的横坐标
f(h+);
}
} int main()
{
for(n=;n<=;n++){ //打表
sum = ;
f();
ans[n] = sum;
}
while(cin>>n){
if(n==) break;
cout<<ans[n]<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 2553:N皇后问题(DFS遍历,水题)的更多相关文章
- HDU 2553(N皇后)(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn. ...
- hdu 2553 N皇后问题 (DFS)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- HDOJ/HDU 1256 画8(绞下思维~水题)
Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...
- hdu 1164:Eddy's research I(水题,数学题,筛法)
Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU ACM 1073 Online Judge ->字符串水题
分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...
随机推荐
- 如何下载flash离线安装包
如何下载flash离线安装包 CreateTime--2018年4月14日16:02:13 Author:Marydon 1.下载地址 UpdateTime--2018年5月13日16点55分 p ...
- 重新安装Drupal?
因个人需要需要重新安装Drupal.如何操纵呢?Drupal是在_drupal_bootstrap_database()函数里面检查是否已经安装过的.检查的依据是有没有$GLOBALS['databa ...
- 【laravel54】composer install与composer update的区别
1.基础概念: 我们需要明白laravel项目里面有2个配置文件,composer.json和composer.lock文件,前者是下载的依赖包配置文件,后者是锁定的包版本信息. 使用之前,需要cd ...
- mosquitto ---SSL/TLS 单向认证+双向认证
生成证书 # * Redistributions in binary form must reproduce the above copyright # notice, this list of ...
- python 多线程 示例
import threading import Queue q = Queue.Queue() from test import * def worker1(x, y): #假设耗时 执行完毕 大于三 ...
- obj 格式注意事项
用Adreno Profiler分析图形效果的实现过程时,需要将特效涉及到的模型导出,以便进行多角度的详细查看,结果发现Adreno Profiler导出模型的功能有bug,总是报错并生成一个残缺的. ...
- Atitit.软件GUI按钮与仪表盘(01)--报警系统--
Atitit.软件GUI按钮与仪表盘(01)--报警系统-- 1. 温度报警区(鲁大师,360taskman) 1 2. os-区-----cpu_mem_io资源占用监测 1 3. Vm区 1 4. ...
- location 将跟目录下某个文件夹指向2级目录
例如: /caffespressos/指向/web01/caffe/ [root@web01 default]# tree web01/ web01/ └── caffe └── index.html ...
- 【C/C++语言】int 在计算机内部的存储
int在32位计算机中占4个字节,主要是想弄清楚这4个字节的在内存中存放的顺序. #include <iostream> using namespace std; typedef stru ...
- Mac命令行启动MySQL
#mysql 启动 mysql.server start #mysql停止 mysql.server stop #mysql重启 mysql.server restart