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 在 ...
随机推荐
- React组件库
图表组件库:Recharts(React和D3构建的图表库) UI组件库:react-bootstrap
- UAT SIT QAS DEV PET
UAT: User Acceptance Testing 用户验收测试SIT: System Integration Testing 系统集成测试PET: Performance Evaluation ...
- 如何离线创建Ionic/cordova项目
在创建ionic项目的时候,需要联网下载许多东西,由于墙的缘故,我们在创建.添加android平台.编译过程中,失败的可能性非常高,为解决这个问题,我创建了一个空的Ionic1 tab项目,并完成了上 ...
- 16个基本颜色关键字 Basic color keywords
16个基本颜色关键字 Basic color keywords Color Color Name HEX RGB black #000000 0,0,0 silver #C0C0C0 192, ...
- SwitchyOmega 在线调试
1,chrome 安装 Proxy SwitchyOmega 扩展程序 2,新建情景模式,输入模式名称"例如:new proxy1",选择"请选择情景模式的类型:代理服务 ...
- 在阿里云配置URL的REWRITE模式
今天买了阿里云的虚拟主机,然后把我的项目上传. 网站首页能够访问,但是点开任何页面,都是提示No input file specified,猜测应该是URL重写没有生效,我在浏览器地址把index.p ...
- OneSQL安装
下载 www.onexsoft.com -> 下载 -> OneSQL 安装 yum install onesql-<version>.rpm 配置 进入安装目录 cd /us ...
- MP3光标移动
#include<iostream> #include<string.h> using namespace std; int main() { int N; cin>&g ...
- VMware8.0虚拟机中安装Ubuntu12.04使用NAT设置连接网络
之前一直尝试使用“桥接”的方法,但是一打开虚拟机,本机windows就断网.最后不得不换种方法,还好尝试了很多遍终于使用NAT设置成功的联网了. 说明:本机windows连接的是无线网. 1.检查自己 ...
- 批量转换编码 (gbk -> utf8)
使用 Notepad++ 批量的转换文件编码:Mass convert a project to UTF-8 using Notepad++ 步骤如下: 1.一般 Noptepad++ 安装完后已经自 ...