N皇后问题

N皇后问题

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

input

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

output

共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。

Sample Input

1
8
5
0

Sample Output

1
92
10

参考的博客 白马游侠
霜雪千年
递归方法(超时了 哭唧唧 )

#include<stdio.h>
#include<string.h>
#include<math.h>
//递归方法,容易T;
int a[15];//若a[2]=3,表示放在了第三行第三列 
int N,sum = 0;
bool judge(int row,int col)//row行,col列 
{
	for (int i=0; i<row; i++)
    {
        if (a[i]== col || (abs(a[i] - col) == row - i))
        {	//如果同行同列或者在对角线 
            return false;
        }
    }
    return true;
}
void dfs(int row)
{	
    for (int col=0; col<N;col++)
    {
        if (judge(row, col))//如果不同行同列 
        {	
            a[row] = col;
            //printf("row=%d col=%d\n",row,col);
            if (row<N -1)
            {
                dfs(row + 1);
            }
            else
            {
                sum++; 
            }
            
        }
    }
    return; 
}
int main(){
	while(scanf("%d",&N)&&N)
	{	memset(a,0,sizeof(0));
		sum=0;
		dfs(0);
		printf("%d\n",sum);
	}
	return 0;
 } 

AC的代码

#include<stdio.h>
#include<math.h>

int queen[15],num[15],sum=0,n,N;
bool judge(int row)//row行,col列 
{      
    for(int i=0;i<row;i++)
	{ 
        if ((queen[i]==queen[row]) || (abs(queen[i]-queen[row])==abs(row-i)))
            return false;
    }
    return true;
}
void place(int row)
{
    for(int col=0;col<n;++col)
	{ 
        queen[row]=col;
        if(judge(row))//如果放置成功
		{
            if(row==n-1) sum++;
            else place(row+1);
        }
    }
}
int main()
{	for(n=1;n<11;++n){
        sum=0;
        place(0);
        num[n]=sum;
    }
    while(scanf("%d",&N)&& N)
        {
        	printf("%d\n",num[N]);
		}
	return 0;
}

非递归方法

#include<stdio.h>
#include<stack>
#include<math.h>
using namespace std;

int N,sum=0;
int a[15]={0};
struct queen 
{
    int row;
    int col;
}now,node;
bool judge(queen node)
{
	for (int i=0;i<node.row;i++)
    {
        if (a[i] == node.col||(abs(a[i] - node.col) == node.row - i))
        {
            return false;
        }
    }
    return true;
}
void dfs()
{
	stack <queen>st;
	now.row = 0;
    now.col = 0;
    st.push(now);
    while(st.size()>=1)
    {
        node=st.top();
        while (node.col < N&& !judge(node))
        {
            node.col++;
        }
        if (node.col<N)
        {
            if (node.row < N-1)
            {
                a[node.row] = node.col;
                st.pop();
                st.push(node);
                //进入这一层的下一个节点 
                node.row++;
                node.col = 0;
                st.push(node);
            }
            else//完成一次摆放 
            {
                
                a[node.row]=node.col;
                sum++;              
                //进入当前层的下一个结点
                node.col++;
                st.pop();
                st.push(node);
            }
        }
        else
        {
            st.pop();           
            if (st.size() == 0)
            {
                return;
            }
            //回溯 
            node=st.top();
            node.col++;
            st.pop();
            st.push(node);
        }
    }   
}
int main()
{
	while(scanf("%d",&N)&&N)
	{
		sum=0;
		dfs();
		printf("%d\n",sum);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值