2018_1_25_8 Queens Chess Problem_打表

https://vjudge.net/problem/UVA-750

注意格式,行列对应,回溯打表

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<fstream>
using namespace std;

int P[100][9];
int tmp[8];
int n=0;
bool col[8]={0},lleft[15]={0},rright[15]={0};
ofstream out;

void func(int r){
	if(r==8){
		for(int i=0;i<8;i++)
		P[n][i]=tmp[i];
		n++;
		return;
	}
	for(int c=0;c<8;c++){
		int ld=(c-r)+7;
		int rd=c+r;
		if(!col[c]&&!lleft[ld]&&!rright[rd]){
			col[c]=1,lleft[ld]=1,rright[rd]=1;
			tmp[r]=c;
			func(r+1);
			col[c]=0,lleft[ld]=0,rright[rd]=0;
		}
	}
}


int main(){
	func(0);
	int Case;
	scanf("%d",&Case);
	int x,y;
	for(int g=0;g<Case;g++){
		int z=1;
		if(g!=0){
			puts("");
		}
		puts("SOLN       COLUMN");
		puts(" #      1 2 3 4 5 6 7 8");
		puts("");
		scanf("%d%d",&x,&y);
		x--;y--;
		for(int i=0;i<n;i++){
			if(P[i][y]==x){
				printf("%2d      ",z++);
				for(int j=0;j<7;j++)
				printf("%d ",P[i][j]+1);
				printf("%d\n",P[i][7]+1);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值