• HDOJ_ACM_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
     

    Code

    View Code
     1 #include <stdio.h>  
     2 #include <math.h>
     3 int q[11];  //q[1] means the coordinate of queue is (1, q[1])
     4 int result[11];//to save the time, so record the previous time
     5 int check(int k) //check if the kth queen is conflict with previous ones
     6 {
     7     int i;
     8     i = 1;
     9     while (i < k)
    10     {
    11         //k - i == abs(q[k] - q[i]) means the current queen is not 45° with other queens
    12         if (q[i] == q[k] || k - i == abs(q[k] - q[i]))
    13             return 0;
    14         i++;
    15     }
    16     return 1;
    17 }
    18 //begin with the first queen, find the position for first queen
    19 //and use DFS to go on to find next queen's position, if u can't find it, u can go back to change the previous queen's position
    20 //untill there is no position for the fist queue to change 
    21 int Nqueens(int n)
    22 {
    23     int count, i, j, k;
    24     count = 0;  //record the count of rank
    25     //begin with first queen and the zero position
    26     k = 1;
    27     q[k] = 0;
    28     while (k > 0)
    29     {
    30         q[k]++;
    31         //when q[k] <= n, u can go on to find q[k] satisfied the condition
    32         while (q[k] <= n && check(k) == 0)
    33             q[k]++;
    34         //if q[k] <= n, which means u can find position for current queen
    35         if (q[k] <= n)
    36         {
    37             //means u find the last queen, so u can record the counr
    38             if (k == n)
    39                 count++;
    40             //if it's not the last queen, u should go on to find the place of next queen
    41             //and for next queen, u should begin with zero.
    42             else
    43             {
    44                 k++;
    45                 q[k] = 0;
    46             }
    47         }
    48         else
    49             //if u can't find the position for current queen, u should go back to modify the previous queen's position
    50             k--;
    51     }
    52     return count;
    53 }
    54 int main()  
    55 {  
    56     int n;
    57     while (scanf("%d", &n) != EOF && n)
    58     {
    59         if (result[n] == 0)
    60             result[n] = Nqueens(n);
    61         printf("%d\n", result[n]);
    62     }
    63     return 0;  
    64 }
    View Code
     1 #include <stdio.h>  
     2 #include <math.h>
     3 #include <stdlib.h>
     4 int q[11];  //q[1] means the coordinate of queue is (1, q[1])
     5 int result[11];//to save the time, so record the previous time
     6 int n;
     7 int check(int k) //check if the kth queen is conflict with previous ones
     8 {
     9     int i;
    10     i = 1;
    11     while (i < k)
    12     {
    13         //k - i == abs(q[k] - q[i]) means the current queen is not 45° with other queens
    14         if (q[i] == q[k] || k - i == abs(q[k] - q[i]))
    15             return 0;
    16         i++;
    17     }
    18     return 1;
    19 }
    20 int count;  //record the count of rank
    21 int flag;
    22 void DFS(int step)
    23 {
    24     int i, j, k;
    25     if (step == n + 1)
    26         count++;
    27     else
    28     {
    29         for (i = 1; i <= n; i++)
    30         {
    31             q[step] = i;
    32             if (check(step) == 0)
    33                 continue;
    34             DFS(step + 1);
    35         }
    36     }
    37 }
    38 int main()  
    39 {  
    40     while (scanf("%d", &n) != EOF && n)
    41     {
    42         //memset();
    43         count = 0;
    44         if (result[n] == 0)
    45         {
    46             DFS(1);
    47             result[n] = count;
    48         }
    49         printf("%d\n", result[n]);
    50     }
    51     return 0;  
    52 }
     


     


    u can find that the first one use least time.

    Recommend
    lcy
     
  • 相关阅读:
    BCP导出导入
    JBehave
    JavaWeb框架的基石
    SWI-Prolog
    面向对象设计三大特性
    android app启动过程(转)
    人,技术与流程(转)
    打破定式,突破屏障,走出自己的创意舒适区(转)
    野生程序员是指仅凭对计算机开发的兴趣进入这个行业,从前端到后台一手包揽,但各方面能力都不精通的人(转)
    Spring MVC异常处理详解 ExceptionHandler good
  • 原文地址:https://www.cnblogs.com/chuanlong/p/3033471.html
Copyright © 2020-2023  润新知