这题重要的走过的点可以走第二次,但是我们会发现当走过4时我们可以标记为0,因为回走是没必要
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct T
{
int x,y,time,step;
}q[10024];
int map[12][12],N,M,x,y;
int d[4][2]={ 1,0,0,1,-1,0,0,-1 };
int BFS( )
{
int first=0,end=0;
T t;
t.step=0;t.time=6;
t.x=x;t.y=y;
q[end++]=t;
while( end>first )
{
if( q[first].time>1 )
{
for( int i=0;i<4;i++ )
{
int dx=q[first].x+d[i][0];
int dy=q[first].y+d[i][1];
if( map[dx][dy]==3 )
return q[first].step+1;
if( map[dx][dy]==1 )
{
t.x=dx;t.y=dy;
t.time=q[first].time-1;
t.step=q[first].step+1;
q[end++]=t;
}
if( map[dx][dy]==4 )
{
t.x=dx;t.y=dy;
t.time=6;
map[dx][dy]=0;
t.step=q[first].step+1;
q[end++]=t;
}
}
}
first++;
}
return -1;
}
int main()
{
int T;
scanf( "%d",&T );
while( T-- )
{
memset( map,0,sizeof( map ) );
scanf( "%d%d",&N,&M );
for( int i=1;i<=N; i++ )
{
for( int j=1; j<=M ; j++ )
{
scanf( "%d",&map[i][j] );
if( map[i][j] == 2 )
{
x=i;
y=j;
}
}
}
printf( "%d\n",BFS( ) );
}
return 0;
}
的重复走。