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 在 ...
随机推荐
- Filter
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有 web资源:例如Jsp, Servlet, 静 ...
- servlet(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- selenium之js
通过调用js代码来网页的滚动条: 出现以上错误的原因是 driver.set_window_size(600,600) 放置的位置不对
- 各种SKYPE网页代码,SKYPE在线代码
各种SKYPE网页代码,SKYPE在线代码 <a href="skype:账户?chat" target="_blank">开始 Skype 文字聊 ...
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
以下是上述协议的简单介绍:BSD开源协议BSD开源协议是一个给于使用者很大自由的协议.基本上使用者可以”为所欲为”,可以自由的使用,修改源代码,也可以将修改后的代码作为开源或者专有软件再发布. 但”为 ...
- iOS 圆的放大动画效果
第一步:创建一个View,将这个View添加到当前的控制器 如: CGFloat timeW = self.view.bounds.size.width; timeAnimation * timean ...
- iOS - Phone 电话
1.调用电话 1.1 拨打系统电话 调用系统自带的打电话程序,要跳转到打电话程序,打完电话自动跳转回来. 在 iOS9.0 + 系统隐私控制里禁止查询设备中已安装的 App,所以在 iOS9.0 + ...
- linux node安装
安装node0.10.24版本,升级了两个版本/usr/local/src/node/0.10.24/usr/local/n/versions/node/4.4.7/usr/local/n/versi ...
- Linux系统移植(1) ------搭建交叉编译环境
本人的开发环境是ubuntu12.05的64版本,运行在11.00的虚拟机上.首先说明为什么需要搭建交叉编译环境.我们知道,我们的开发一般在PC机上,是基于X86架构的,而我们的开发板却是基于ARM架 ...
- Calibre - book library management application
http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...