#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct P
{
int x,y;
int e;
int time;
P()
{
x = 0,y = 0,e = 0,time = 0;
}
};
int n,m;
int map[10][10];
//int is[10][10];
int ma[10][10];
P start,ens;
queue<P> G;
int dist[4][2] = {0,1,0,-1,1,0,-1,0};
bool iss(int a,int b)
{
if(a>=0&&b>=0&&a<n&&b<m&&map[a][b])return true;
return false;
}
int solve()
{
if(!G.empty()) G.pop();
G.push(start);
memset(ma,0,sizeof(ma));
ma[start.x][start.y] = 6;
while(!G.empty())
{
P xi = G.front();
G.pop();
if(xi.x==ens.x&&xi.y==ens.y) return xi.time;
P temp;
if(xi.e>0)
{
for(int i=0;i<4;i++)
{
temp.x = xi.x+dist[i][0];
temp.y = xi.y +dist[i][1];
temp.e = xi.e-1;
temp.time = xi.time + 1;
if(iss(temp.x,temp.y))
{
if(map[temp.x][temp.y] == 4&&temp.e!=0)
temp.e = 6;
if(ma[temp.x][temp.y]<temp.e)
G.push(temp),ma[temp.x][temp.y]=temp.e;
}
}
}
}
return -1;
}
void init()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j = 0;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j] == 2)
start.x = i,start.y = j,start.e = 6;
if(map[i][j] == 3)
ens.x = i,ens.y = j;
}
printf("%d\n",solve());
}
}
int main()
{
init();
return 0;
}
hdu 1072
最新推荐文章于 2020-02-24 16:20:05 发布