#include"iostream.h"
#include"string.h"
#include"iomanip.h"
#define N 5
#define N 5
int map[N][N];//数组
int direction[8][2]= {{1, 2}, {2, 1}, {1,-2}, {2, -1},
{-1, 2}, {-2, 1}, {-1, -2}, {-2, -1}};//方向
int count= 0; //总计
void next(int x,int y,int step){
int cx;
int cy;
if(step>N*N){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<map[i][j]<<" ";
cout<<endl;
}
count++;
cout<<count<<endl<<endl;
}
else{
for(int d=0;d<8;d++){
cx=x+direction[d][0];
cy=y+direction[d][1];
if( cx>=N || cx<0 || cy>=N || cy<0 )
continue;
if(map[cx][cy] != 0)
continue;
map[cx][cy]=step;
next(cx,cy,step+1);
map[cx][cy]=0;
}
}
}
void createmap(){
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
map[i][j]=0;
}
void main(int argc,char*argv[]){
for(int i=0;i<N;i++)
for(int j=0;j<N;j++){
createmap();
map[i][j]=1;
next(i,j,2);
}
cout<<endl<<endl<<count;
}
2778

被折叠的 条评论
为什么被折叠?



