题目:八皇后模型,输出包含了某一点的所有皇后
注意:格式上,不同输入之间空两行。数字的最后没有空格,所以把空格写在前面。还有最后一行输出后,后面没有空行了
#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;
}