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
 

转载于:https://www.cnblogs.com/chuanlong/archive/2013/04/21/3033471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值