hdu1026

/*
分析:
    1次ac,0MS,哦耶~
    用的广搜,数据应该挺弱的,本来都想好要优化了,然后再试试
弄到0MS,可是牟想到- -,木优化也0MS了。
    所以,直接打表记录路径吧。

                                           2012-07-19
*/






#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;

struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
};
int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
int map[111][111];
int flag[111][111];
int blood[111][111];
int n,m;

int judge(int x,int y)
{
	if(x<0 || x>=n || y<0 || y>=m)	return 1;
	if(map[x][y]==-1)				return 1;
	return 0;
}
int BFS()
{
	priority_queue<node>q;
	node cur,next;
	int i;

	cur.x=0;
	cur.y=0;
	cur.step=0;
	map[0][0]=-1;

	q.push(cur);
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		if(cur.x==n-1 && cur.y==m-1)	return cur.step;
		for(i=0;i<4;i++)
		{
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];

			if(judge(next.x,next.y))	continue;
			next.step=cur.step+1+map[next.x][next.y];
			flag[next.x][next.y]=i+1;
			map[next.x][next.y]=-1;
			q.push(next);
		}
	}
	return -1;
}
int temp;
void P(int x,int y)
{
	int next_x,next_y;
	if(flag[x][y]==0)	return ;
	next_x=x-dir[flag[x][y]-1][0];
	next_y=y-dir[flag[x][y]-1][1];
	P(next_x,next_y);
	printf("%ds:(%d,%d)->(%d,%d)\n",temp++,next_x,next_y,x,y);

	while(blood[x][y]--)	printf("%ds:FIGHT AT (%d,%d)\n",temp++,x,y);
}

int main()
{
	char str[111];
	int i,l;
	int ans;

	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(map,0,sizeof(map));
		memset(flag,0,sizeof(flag));
		memset(blood,0,sizeof(blood));
		for(i=0;i<n;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='.')		map[i][l]=0;
				else if(str[l]=='X')map[i][l]=-1;
				else				map[i][l]=blood[i][l]=str[l]-'0';
			}
		}

		ans=BFS();
		if(ans==-1)	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",ans);

			temp=1;
			P(n-1,m-1);
		}
		printf("FINISH\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值