zoukankan      html  css  js  c++  java
  • HDU 2553 N皇后问题

                   N皇后问题


     

    Problem Description
    在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<cstdio>
     2 using namespace std;
     3 
     4 int c[11];
     5 int cnt;
     6 
     7 void dfs(int cur,int n)
     8 {
     9     if(cur==n)
    10     {
    11         cnt++;
    12         return;
    13     } 
    14     for(int i=0;i<n;i++)
    15     {
    16         int ok=1;
    17         c[cur]=i;
    18         for(int j=0;j<cur;j++)
    19         {
    20             if(c[j]==c[cur]||cur-c[cur]==j-c[j]||cur+c[cur]==j+c[j])
    21             {
    22                 ok=0;
    23                 break;
    24             }
    25         }
    26         if(ok)
    27         dfs(cur+1,n);
    28     }
    29 }
    30 
    31 int main()
    32 {
    33     int n;
    34     int a[11];
    35     for(int i=1;i<=10;i++)
    36     {
    37         cnt=0;
    38         dfs(0,i); 
    39         a[i]=cnt;
    40     }
    41     while(scanf("%d",&n)&&n)
    42     {
    43         printf("%d
    ",a[n]);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    uva 1605 building for UN ——yhx
    uva 120 stacks of flapjacks ——yhx
    uva133-S.B.S.
    Uva10082 WERTYU -S.B.S.
    Quicksum-S.B.S.
    NOIP2014提高组 DAY1 -SilverN
    NOIP2013普及组 -SilverN
    uva 1354 Mobile Computing ——yhx
    UVa 11292 Dragon of Loowater
    UVa 839 Not so Mobile
  • 原文地址:https://www.cnblogs.com/homura/p/4699650.html
Copyright © 2011-2022 走看看