hdu 1072

OJ

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


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值