8 Queens Chess Problem

题目:八皇后模型,输出包含了某一点的所有皇后

注意:格式上,不同输入之间空两行。数字的最后没有空格,所以把空格写在前面。还有最后一行输出后,后面没有空行了

#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <ctgmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main()
{
	int xunhuan;
	cin>> xunhuan;
	while(xunhuan--){
		int x,y;
		cin >> x >> y;
		int t = 1;
		int a[8] = {0};
		int b[15] = {0};
		int c[15] = {0};
		int j = 0,i=0;
		int temp = 0;
		int count = 1;
		vector<int> m;
		vector<int>::iterator it;
		vector<int> sum;
		
		cout<<"SOLN       COLUMN"<<endl;
		cout<<" #      1 2 3 4 5 6 7 8"<<endl<<endl;
		while(1){
			for(i = 0; i <= 7; i ++){
				for(j = 0; j <= 7; j ++){
					if((a[j]==0) && (b[i+j]==0) && (c[i-j+7]==0) ){//这一点可以有皇后
						m.push_back(j);
						a[j]=1;
						b[i+j]=1;
						c[i-j+7]=1;
						break;
					}
				}
				//如果走完了还没有位置
				
				if(j == 8){
				Next_One:            while(1){
					if(i >= 0){
						i--;
						temp = m[i];
						//删除原来皇后的记录
						a[temp]=0;
						b[i+temp]=0;
						c[i-temp+7]=0;
						m.pop_back();
						
						for(j = temp +1; j <= 7; j ++){
							if((a[j]==0) && (b[i+j]==0) && (c[i-j+7]==0) ){//这一点可以有皇后
								m.push_back(j);
								a[j]=1;
								b[i+j]=1;
								c[i-j+7]=1;
								break;
							}
						}
						if(j != 8) break;
						
					}
				   }
					
				}
			}
			
			//判断
			if(m[y-1] == x-1){
				printf("%2d",count++);
				cout<<"     ";
				for(it = m.begin();it != m.end();it++){
					cout << " " << (*it)+1; //空格在之前出现
				}
				cout<<endl;

			}
				
			
			t++;
			if(t==93) break;
			goto Next_One;
			
		}
		
		// goto Next_One;
		if(xunhuan)cout<<endl;
 //先判断是不是最后一行了,否则不输出回车
	}
		return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值