题目描述:
    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。
你的任务是,对于给定的N,求出有多少种合法的放置方法。

 

Input

    共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。

 

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的更多相关文章

  1. 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 ...

  2. [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others)     ...

  3. HDU 2553(N皇后)(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn. ...

  4. hdu 2553 N皇后问题

    回溯. 一个主对角线,副对角线的技巧 //vis[0][i]表示第i列有没有皇后 vis[1][cur+i]表示副对角线 vis[2][cur-i+n]表示主对角线 #include <cstd ...

  5. HDU 2553 (状压) N皇后问题 (2)

    也许大多数做法都是打表,但这里用位运算的思想来解决这个问题,位运算果然强大,Orz 原文地址,感觉讲的很明白了: http://www.cnblogs.com/gj-Acit/archive/2013 ...

  6. hdu 2553 N皇后问题 (经典DFS)

    题目链接:点击链接 思路:用一维数组hang[num] = i,num表示第num行,i表示第i列,计算n = 1~10皇后的不同放置数量,然后打表 #include<stdio.h> # ...

  7. HDU 2553 N皇后问题(详细题解)

    这是一道深搜题目!问题的关键是在剪枝. 下面我们对问题进行分析: 1.一行只能放一个皇后,所以我们一旦确定此处可以放皇后,那么该行就只能放一个皇后,下面的就不要再搜了. 2.每一列只能放一个皇后,所以 ...

  8. HDU 2553 n皇后问题(回溯法)

     DFS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  9. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

随机推荐

  1. Fiddler 4 抓包

    Fiddler 4 Tools –> Fiddler Options HTTPS -> 勾选"CaptureHTTPS CONNECTs",同时勾选"Decr ...

  2. treap 模版

    struct Treap { struct node { node *son[]; int key,siz,wei,cnt; node(int _key,node *f) { son[]=son[]= ...

  3. C#位运算讲解与示例

    首先每一个权限数都是2的N次方数 如:k1=2 ; //添加 k2=4 ; //删除 k3=8; //修改 ... 如此定义功能权限数,当需要组合权限时,就需要对各个所拥有的权限数按位或了. 如: p ...

  4. JAVA 1.6 流程控制语句

    1. 条件运算符(三元表达式),其形式为:type d = a ? b : c; 具体化形式为:int d = 2 < 1 ? 3 : 4;2. 轻量级的文本编辑器:UltraEdit.Edit ...

  5. Sublime Text3 快捷键

    选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...

  6. iOS 消息推送报错NSCocoaErrorDomain Code=3000

    转自: http://www.cnblogs.com/zxykit/p/5207498.html Xcode7推送报错.Error Domain=NSCocoaErrorDomain Code=300 ...

  7. Evolutionary Computing: 5. Evolutionary Strategies(1)

    resource: Evolutionary computing, A.E.Eiben Outline What is Evolution Strategies Introductory Exampl ...

  8. H5离线存储

    如何使用 首先,我们建立一个html文件,类似这样: <!DOCTYPE html> <html lang="en" manifest="manifes ...

  9. Sprint 2

    成员 团队贡献分 许佳仪 22 柯晓君 23 卓宇靖 18 赖文亮 17 浏览书籍 查询书籍(可分别按照图书名和价格进行查询)

  10. 【转】mysql查询结果输出到文件

    转自:http://www.cnblogs.com/emanlee/p/4233602.html mysql查询结果导出/输出/写入到文件 方法一: 直接执行命令: mysql> select ...