好久没有做题了,唉,一晃大学就只有一年了。考研还是保研还是工作,好难确定
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char dest[5][5]={{'1','1','1','1','1'},{'0','1','1','1','1'},{'0','0',' ','1','1'},{'0','0','0','0','1'},{'0','0','0','0','0'}};
char board[5][5];
int dx[8]={-1,1,-2,2,-2,2,-1,1};
int dy[8]={-2,-2,-1,-1,1,1,2,2};
int min;
void getPosition(int *x,int *y)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(board[i][j]==' ')
{
*x=i;*y=j;
return ;
}
}
}
}
int Check(int x,int y,int i,int way)//检查第i个方向的马是否能跳
{
if(x+dx[i]>=0&&x+dx[i]<5&&y+dy[i]>=0&&y+dy[i]<5&&(way+i)!=7)
{
return 1;
}
return 0;
}
int DFS(int x,int y,int step,int way)
{
int i;
int nx,ny;
char tmp;
if(step>=min)return 0;
if(memcmp(board,dest,sizeof(board))==0)
{
if(step<min)min=step;
return 0;
}
for(i=0;i<8;i++)
{
if(Check(x,y,i,way))
{
nx=x+dx[i];ny=y+dy[i];
tmp=board[x][y];
board[x][y]=board[nx][ny];
board[nx][ny]=tmp;
DFS(nx,ny,step+1,i);
tmp=board[nx][ny];
board[nx][ny]=board[x][y];
board[x][y]=tmp;
}
}
}
int main()
{
int N,x,y,step;
int i,j,k;
scanf("%d",&N);
getchar();
for(i=0;i<N;i++)
{
min=11;
for(j=0;j<5;j++)
{
gets(board[j]);
}
getPosition(&x,&y);
DFS(x,y,0,8);
if(min>10)printf("Unsolvable in less than 11 move(s).\n");
else printf("Solvable in %d move(s).\n",min);
}
return 0;
}