#include<iostream>
#include<queue>
#include<string>
using namespace std;
typedef struct point
{
int x,y;
int step;
int time;
}point;
int n,m;
int mins;
int map[10][10];
int t[10][10];
int sx,sy,ex,ey;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
void bfs()
{
mins=0;
queue<point> Q;
point start;
start.x=sx;
start.y=sy;
start.step=0;
start.time=6;
t[sx][sy]=6;
Q.push(start);
while(!Q.empty())
{
point now=Q.front();
Q.pop();
if(now.x==ex&&now.y==ey)
{
if(now.time>0)
{mins=now.step;
return;
}
else continue;
}
if(map[now.x][now.y]==4)
{
now.time=6;
t[now.x][now.y]=6;
}
int i;
for(i=0;i<4;i++)
{
point temp;
temp.x=now.x+dir[i][0];
temp.y=now.y+dir[i][1];
temp.step=now.step+1;
temp.time=now.time-1;
if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m
&&now.time>0&&temp.time>t[temp.x][temp.y]&&map[temp.x][temp.y]!=0)
{
t[temp.x][temp.y]=temp.time;
Q.push(temp);
}
}
}
}
int main()
{
int k;
cin>>k;
while(k--)
{
cin>>n>>m;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
t[i][j]=0;
cin>>map[i][j];
if(map[i][j]==2){sx=i;sy=j;}
else if(map[i][j]==3){ex=i;ey=j;}
}
}
bfs();
if(mins==0)cout<<-1<<endl;
else cout<<mins<<endl;
}
return 0;
}
hdu1072
最新推荐文章于 2020-05-25 22:18:25 发布