HDU-1026

//带路径的BFS
//记录着父亲,以及入队的要求就好了
#include"stdio.h"
#include"iostream"
#include"queue"
using namespace std;
#define MAX 9999999
char str[200][200];
int dis[10001];
int n,m;
int parent[10001];
int fx(int v)
{
	if(v%m!=0)
		return v/m;
	else
		return v/m-1;
}
int fy(int v)
{
	if(v%m!=0)
		return v%m-1;
	else
		return m-1;
}
int fv(int x,int y)
{
	return x*m+y+1;
}
void bfs()
{
	int v;
	int i,j;
	int lp;
	queue<int> q;
	int vx,vy;
	for(i=0;i<10001;i++)
		dis[i]=9999999;
	dis[1]=0;
	q.push(1);
	while(q.empty()==0)
	{
		v=q.front();
		q.pop();
		vx=fx(v);
		vy=fy(v);
		if(vx-1>=0 && str[vx-1][vy]!='X')
		{
			
			if(str[vx-1][vy]>='1' && str[vx-1][vy]<='9'  )
			{
				if(dis[fv(vx-1,vy)]>dis[v]+(str[vx-1][vy]-'0')+1)
				{
					q.push(fv(vx-1,vy));
					dis[fv(vx-1,vy)]=dis[v]+(str[vx-1][vy]-'0')+1;
					parent[fv(vx-1,vy)]=v;
				}
			}
			else
			{
				if(dis[fv(vx-1,vy)]>dis[v]+1)
				{
					q.push(fv(vx-1,vy));
					dis[fv(vx-1,vy)]=dis[v]+1;
					parent[fv(vx-1,vy)]=v;
				}
			}
		}
		if(vx+1<n && str[vx+1][vy]!='X')
		{
			
			if(str[vx+1][vy]>='1' && str[vx+1][vy]<='9')
			{
				if(dis[fv(vx+1,vy)]>dis[v]+(str[vx+1][vy]-'0')+1)
				{
					dis[fv(vx+1,vy)]=dis[v]+(str[vx+1][vy]-'0')+1;
					parent[fv(vx+1,vy)]=v;
					q.push(fv(vx+1,vy));
				}
			}
			else
			{
				if(dis[fv(vx+1,vy)]>dis[v]+1)
				{
					dis[fv(vx+1,vy)]=dis[v]+1;
					parent[fv(vx+1,vy)]=v;
					q.push(fv(vx+1,vy));
				}
			}
			
		}
		if(vy-1>=0 && str[vx][vy-1]!='X')
		{
			lp=fv(vx,vy-1);
			if(str[vx][vy-1]>='1' && str[vx][vy-1]<='9')
			{
				if(dis[lp]>dis[v]+(str[vx][vy-1]-'0')+1)
				{
					dis[lp]=dis[v]+(str[vx][vy-1]-'0')+1;
					parent[lp]=v;
					q.push(lp);
				}
			}
			else
			{
				if(dis[lp]>dis[v]+1)
				{
					dis[lp]=dis[v]+1;
					parent[lp]=v;
					q.push(lp);
				}
			}
		}
		if(vy+1<m && str[vx][vy+1]!='X')
		{
			lp=fv(vx,vy+1);
			if(str[vx][vy+1]>='1' && str[vx][vy+1]<='9')
			{
				if(dis[lp]>dis[v]+(str[vx][vy+1]-'0')+1)
				{
					dis[lp]=dis[v]+(str[vx][vy+1]-'0')+1;
					parent[lp]=v;
					q.push(lp);
				}
			}
			else
			{
				if(dis[lp]>dis[v]+1)
				{
					dis[lp]=dis[v]+1;
					parent[lp]=v;
					q.push(lp);
				}
			}
			
		}
	}
}
int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(parent,-1,sizeof(parent));
		for(i=0;i<n;i++)
			cin>>str[i];
		bfs();
		if(dis[fv(n-1,m-1)]>=MAX)
		{
			printf("God please help our poor hero.\n");
		}
		else{
		printf("It takes %d seconds to reach the target position, let me show you the way.\n",dis[fv(n-1,m-1)]);
		
		int x;
		x=fv(n-1,m-1);
		int luo[1002];
		int num=0;
		while(x!=1)
		{
			luo[num]=x;
			num++;
			x=parent[x];
		}
		luo[num]=1;
		int t=1;
		int l,p;
		l=fx(luo[num]);
		p=fy(luo[num]);
		int z1,z2;
		for(i=num-1;i>=0;i--)
		{
			z1=fx(luo[i]);
			z2=fy(luo[i]);
			printf("%ds:",t);
			t++;
			printf("(%d,%d)->(%d,%d)\n",l,p,z1,z2);
			if(str[z1][z2]>='0' && str[z1][z2]<='9')
			{
				for(j=1;j<=(str[z1][z2]-'0');j++)
				{
					printf("%ds:",t);
					t++;
					printf("FIGHT AT (%d,%d)\n",z1,z2);
				}
			}
			l=z1;
			p=z2;
		}
		}
		printf("FINISH\n");
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值