N皇后问题-Hdu 2553
Input
Output
Sample Input
1
8
5
0
Sample Output
1
92
10 代码1如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n;
int Nqueue[] = {,,,,,,,,,,,,,,};
while(scanf("%d",&n)==&&n)
{
printf("%d\n", Nqueue[n]);
}
return ;
}
代码2如下:
//此方法简单,但是会超时,不过可以用来模拟程序的运行(模拟),然后打表 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int C[], tot, n;
int pos; void dfs(int cur)
{
int i, j;
//pos++;
if(cur == n)//cur代表行,当cur等于n时,可行解数加1
tot++;
else
for(i = ; i < n; i++ )//
{
int flag = ;
C[cur] = i;
for(j = ; j < cur; j++ )
if(C[cur]==C[j]||cur-C[cur]==j-C[j]||cur+C[cur]==j+C[j])//分别为同列,同主对角线,同副对角线
{
flag = ;
break;
}
if(flag)
dfs(cur+);
}
} int main()
{
while(scanf("%d", &n)==&&n)
{
tot = , pos = ;
dfs();
printf("%d\n", tot);
//printf("%d\n", pos);
}
return ;
} // n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //可行解的个数 1 0 0 2 10 4 40 92 352 724 2680 14200 73712 365596
N皇后问题-Hdu 2553的更多相关文章
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2553(N皇后)(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn. ...
- hdu 2553 N皇后问题
回溯. 一个主对角线,副对角线的技巧 //vis[0][i]表示第i列有没有皇后 vis[1][cur+i]表示副对角线 vis[2][cur-i+n]表示主对角线 #include <cstd ...
- HDU 2553 (状压) N皇后问题 (2)
也许大多数做法都是打表,但这里用位运算的思想来解决这个问题,位运算果然强大,Orz 原文地址,感觉讲的很明白了: http://www.cnblogs.com/gj-Acit/archive/2013 ...
- hdu 2553 N皇后问题 (经典DFS)
题目链接:点击链接 思路:用一维数组hang[num] = i,num表示第num行,i表示第i列,计算n = 1~10皇后的不同放置数量,然后打表 #include<stdio.h> # ...
- HDU 2553 N皇后问题(详细题解)
这是一道深搜题目!问题的关键是在剪枝. 下面我们对问题进行分析: 1.一行只能放一个皇后,所以我们一旦确定此处可以放皇后,那么该行就只能放一个皇后,下面的就不要再搜了. 2.每一列只能放一个皇后,所以 ...
- HDU 2553 n皇后问题(回溯法)
DFS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
随机推荐
- Java开发中经典的小实例-(冒泡法)
public class Test25 { public static void main(String[] args) { // 冒泡法 int[] array = ...
- consul 模板配置内容的参数说明
datacenters {{datacenters}} 数据中心 file {{file "/path/to/local/file"}} 读取本地文件的内容.如果不可读的话,会报错 ...
- hdu-5525 Product(费马小定理)
题目来源:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=644&pid=1003 前面用奇偶性约掉2, ...
- laravel 实现上传 excel
//导入电话号码 public function postTel(){ set_time_limit(0); ini_set('memory_limit','1024M'); $params = In ...
- git入门网站
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git入门教程:对商业的.开源的. ...
- UGUI text image 等加Shadow OutLine等
Text,文本控件,同NGUI中的Label.支持动态字库.大小调节.富文本(基本的html标签格式)等等.描边.阴影等需要Effect组件支持.Add Component-->UI--> ...
- RxAndroid/java小记
Rxandroid 作为一个在设计模式中能把MVP发挥的淋漓尽致的框架不去学习感觉真的对不起自己,然后也学点新东西吧,响应式编程,MVP观察者模式,然后使用RxAndroid使我们自己的代码更加简洁 ...
- Jekyll x Liquid 控制文章列表只显示特定类别的Post
使用Liquid按照Category或者Tag过滤Post List 文章首发于szhshp的第三边境研究所(szhshp.org), 转载请注明 前段时间画了一些漫画,考虑把漫画相关的Post放到另 ...
- 磁盘文件系统Fat、Fat32、NTFS、exFAT的优缺点
我们在Windows系统里格式化磁盘的时候,文件系统的选项里可以看到有“FAT”.“FAT32”.“NTFS”等选项,在对U盘或其他移动存储设备 格式化的时候还会出现“exFAT”选项,那么这四种磁盘 ...
- DOM hash
前段时间做的一个H5专题,用到了hash解决问题,特意记录一下.DOM hash的详细内容可以点击链接查看. hash就是uri中#及后面的部分,例如:www.google.com.hk#123的#1 ...