#include<iostream>
#define N 5
using namespace std;
//回溯法实现N皇后问题
void place(int* x,int n){
for(int i=1;i<N;++i){
int ifPlace=1;
for(int j=1;j<N;++j)
if(x[j]==i||((n-j==i-x[j]||n-j==x[j]-i)&&x[j]!=0)){
ifPlace=0;
break;
}
if(ifPlace){
x[n]=i;
if(n==N-1){
cout<<"找到一个解:"<<endl;
for(int i=1;i<N;++i)cout<<"第"<<i<<"行,第"<<x[i]<<"列"<<endl;
return;
}else place(x,n+1);
}
}
x[n-1]=0;
}
int main(){
int x[N];
for(int i=0;i<N;++i)x[i]=0;
place(x,1);
return 0;
}