hdu1072

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值