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 在 ...
随机推荐
- BW系统之间的InfoProvider数据传输:Export DataSource
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Javascript中的字典和散列
function Dictionary() { var items={}; this.set=function (key,value) { items[key]=value; }; this.remo ...
- Javascript 数组常用操作方法
一.数组 Array 1.创建数组 /* 构造函数 */ var arr1 = new Array(); //创建一个空数组 var arr1 = new Array(5); //创建指定长度数组(数 ...
- mybatis入门_配置文件的配置
一.全局配置文件配置 1.1 properties标签 Properties标签可以用来加载配置文件.例如,我们可以将数据库的连接信息放入到一个配置文件(db.properties中..) 下为db. ...
- J2EE版本
Different versions of JEE: Note: JPE (Java Professional Edition) project announced in May 1998 at Su ...
- ORACLE RAISE
ORACLE 出错信息的输出 偷懒的办法直接在Exception 后使用raise但是错误信息不是很完整使用RAISE_APPLICATION_ERROR(-20999, DBMS_UTILITY.f ...
- javascript实现简单的轮播图片
方法一: <script> var curIndex=0;//时间间隔(单位毫秒),每秒钟显示一张,数组共有5张图片放在Photos文件夹下. var timeInterval=1000; ...
- mac下使用sencha cmd+extjs6
笔者刚接手公司一个项目,后台是使用extjs6做前端,php做api接口,两者通过ajax交互 没办法,不管接手的项目多么的挫逼,都还是要上的,拿人钱财替人消灾嘛 首先是安装sencha cmd ,百 ...
- select distinct
select distinct select distinct 用于返回表中唯一不同的值. 语法 select distinct 列名称 from 表名称 使用 distinct 关键字 Studen ...
- js 同for一样效果 (延迟)每秒循环一次 追加
<script type="text/javascript"> var j = 1; var timeID = null; function ...