《ACM程序设计》例题解析-N皇后问题

N皇后问题

题目大意

经典问题,N个皇后放在N*N的棋盘,互不攻击。

思路

回溯,每行放一个,O(N!)

格子合法的判断

数组

? 列位置 col [N]
? 左右对角线 lr [2*N-1]
? 右左对角线 rl [2*N-1]

y-x+n-1                    

7891011121314
678910111213
56789101112
4567891011
345678910
23456789
12345678
01234567
x+y

01234567
12345678
23456789
345678910
4567891011
56789101112
678910111213
7891011121314

屏蔽位

row

101010
111010
111010

ld

100100
110100
101000
rd

000111
010111
001011


*图片来源于Matrix67的Blog

代码

数组判断格子合法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <assert.h>
typedef __int64 LL;
#define N 14
int col[N],lr[2*N-1],rl[2*N-1];
int cnt;
int dfs(int *a,int k,int n){
	int i;
	if(k==n){
		cnt++;
		/*if(cnt < 10) {
			for(i=0;i<n;i++) printf("%d ",a[i]);
			printf("\n");
		}*/
	}
	else{
		for(i=0;i<n;i++){
			if(!col[i] && !lr[i-k+n-1] && !rl[k+i]){
				a[k] = i;
				col[i] = lr[i-k+n-1] = rl[k+i] = 1;
				dfs(a,k+1,n);
				col[i] = lr[i-k+n-1] = rl[k+i] = 0;
			}
		}
	}
}

int main(){
	int a[N];
	time_t begin,end;
	begin = clock();
	dfs(a,0,14);
	printf("%d\n",cnt);
	end = clock();
	printf("time:%.3f\n ",(end - begin)*1.0/CLOCKS_PER_SEC);
	return 0;
}

利用屏蔽位

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <assert.h>
typedef __int64 LL;

int cnt = 0;

void dfs(int *a,int k,int n,int c,int l,int r){
    int i,mask,t;
    if(k==n){
        cnt++;
        /*if(cnt < 10) {
            for(i=0;i<n;i++) printf("%d ",a[i]);
            printf("\n");
        }*/
    }
    else{
        mask = c | l | r;
        for(i=0;i<n;i++){
            t = 1<<i;
            if(~mask&t){
                a[k] = i;                
                dfs(a,k+1,n,c|t,(l|t)<<1,(r|t)>>1);
            }
        }
    }
    
}

int main(){
    int a[20];
    time_t begin,end;
    begin = clock();
    dfs(a,0,14,0,0,0);
    printf("%d\n",cnt);
    end = clock();
    printf("time:%.3f\n ",(end - begin)*1.0/CLOCKS_PER_SEC); //296
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值