hdu2553 N皇后问题

hdu2553

N皇后问题

//hdu2553 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 11
int ans,n;
struct Map{
	int x;
	int y;
};
Map map[MAX];
void dfs(int cont){
	if(cont==n){
		ans++;
		return;
	}
	if(cont>n){
		return ;
	}
	int j,m;
	for(j=0;j<n;j++){//第cont个皇后的行号为cont 
		for(m=0;m<cont;m++){
			if(map[m].y==j||map[m].x+map[m].y==cont+j||map[m].x-map[m].y==cont-j){
				break;
			}	
	    }
	    if(m<cont)//当前皇后不能放在cont行j列 
		        continue;
		map[cont].x=cont;
	    map[cont].y=j;
	    dfs(cont+1);
	}	
}
int main(){
	int Queen[MAX];
	for(n=1;n<=10;n++){
		ans=0;
		for(int j=0;j<n;j++){//第一个放在第0行的某一列 
			map[0].x=0;
			map[0].y=j;
			int cont=1;
			dfs(cont);
		}
		Queen[n]=ans;
	} 
	while(scanf("%d",&n)!=EOF&&n){
		
		printf("%d\n",Queen[n]);
	}
	return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值