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;
}