N皇后问题


转自:http://www.cnblogs.com/chuanlong/archive/2013/04/21/3033471.html

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
 

方法一:递归  容易理解,但效率低点,也能Accepted

<span style="font-size:14px;">#include <stdio.h>  
#include <math.h>
#include <stdlib.h>
int q[11];  //q[1] means the coordinate of queue is (1, q[1])
int result[11];//to save the time, so record the previous time
int n;
int check(int k) //check if the kth queen is conflict with previous ones
{
    int i;
    i = 1;
    while (i < k)
    {
        //k - i == abs(q[k] - q[i]) means the current queen is not 45° with other queens
        if (q[i] == q[k] || k - i == abs(q[k] - q[i]))
            return 0;
        i++;
    }
    return 1;
}
int count;  //record the count of rank
int flag;
void DFS(int step)
{
    int i, j, k;
    if (step == n + 1)
        count++; <span style="color:#3333ff;">   //需要打印具体的放置方式时,可以在这里打印,q[i]表示第i个皇后放在第i行第q[i]列</span>
    else
    {
        for (i = 1; i <= n; i++)
        {
            q[step] = i;
            if (check(step) == 0)
                continue;
            DFS(step + 1);
        }
    }
}
int main()  
{  
    while (scanf("%d", &n) != EOF && n)
    {
        //memset();
        count = 0;
        if (result[n] == 0)
        {
            DFS(1);
            result[n] = count;
        }
        printf("%d\n", result[n]);
    }
    return 0;  
}</span>

方法二:运行效率更快,但不好理解

<span style="font-size:14px;">#include <stdio.h>  
#include <math.h>
int q[11];  //q[1] means the coordinate of queue is (1, q[1])
int result[11];//to save the time, so record the previous time
int check(int k) //check if the kth queen is conflict with previous ones
{
    int i;
    i = 1;
    while (i < k)
    {
        //k - i == abs(q[k] - q[i]) means the current queen is not 45° with other queens
        if (q[i] == q[k] || k - i == abs(q[k] - q[i]))
            return 0;
        i++;
    }
    return 1;
}
//begin with the first queen, find the position for first queen
//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
//untill there is no position for the fist queue to change 
int Nqueens(int n)
{
    int count, i, j, k;
    count = 0;  //record the count of rank
    //begin with first queen and the zero position
    k = 1;
    q[k] = 0;
    while (k > 0)
    {
        q[k]++;
        //when q[k] <= n, u can go on to find q[k] satisfied the condition
        while (q[k] <= n && check(k) == 0)
            q[k]++;
        //if q[k] <= n, which means u can find position for current queen
        if (q[k] <= n)
        {
            //means u find the last queen, so u can record the counr
            if (k == n)
                count++;
            //if it's not the last queen, u should go on to find the place of next queen
            //and for next queen, u should begin with zero.
            else
            {
                k++;
                q[k] = 0;
            }
        }
        else
            //if u can't find the position for current queen, u should go back to modify the previous queen's position
            k--;
    }
    return count;
}
int main()  
{  
    int n;
    while (scanf("%d", &n) != EOF && n)
    {
        if (result[n] == 0)
            result[n] = Nqueens(n);
        printf("%d\n", result[n]);
    }
    return 0;  
}</span>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值