hdu 1026(BFS)

#include <iostream>
#include <queue>
using namespace std;
const int MAX = 99999999;
int n,m;
struct st
{
	char cc;
	int num,x,y,prex,prey;
}map[105][105];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
void outPut()
{
	int x=0,y=0,a,b,num=1,i;
	if(map[0][0].num!= MAX)
	{
		printf("It takes %d seconds to reach the target position, let me show you the way.\n",map[0][0].num);
		while(x!= n-1| y!=m-1)
		{
			a=map[x][y].prex; 
			b=map[x][y].prey;
			if(map[x][y].cc!='.')
				for(i=0;i<map[x][y].cc - '0';i++)
					printf("%ds:FIGHT AT (%d,%d)\n",num++,x,y);
			printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,a,b);
			x=a; y=b;
		}
		if(map[x][y].cc!='.')
		{
			for(i=0;i<map[x][y].cc - '0';i++)
				printf("%ds:FIGHT AT (%d,%d)\n",num++,x,y);
		}
		printf("FINISH\n");
	}
	else
	{
		printf("God please help our poor hero.\nFINISH\n");
	}
}

void bfs()
{
	int temp;
	struct st fir;
	queue<st> qu;
	map[n-1][m-1].num=0;
	if(map[n-1][m-1].cc>='1'&&map[n-1][m-1].cc<='9')
		map[n-1][m-1].num=map[n-1][m-1].cc - '0';
	qu.push(map[n-1][m-1]);
	while(!qu.empty())
	{
		fir=qu.front();
		for(int i=0;i<4;i++)
		{
			int tx=fir.x+dir[i][0],ty=fir.y+dir[i][1];
			if(tx<0||ty<0||tx>=n||ty>=m||map[tx][ty].cc=='X') continue;
			if(map[tx][ty].cc!='.')       
				temp=fir.num + map[tx][ty].cc-'0';
			else  
				temp=fir.num;  
			if(temp+1<map[tx][ty].num)
			{
				map[tx][ty].num=temp+1;
				map[tx][ty].prex=fir.x;
				map[tx][ty].prey=fir.y;
				qu.push(map[tx][ty]);                    
			}
		}
		qu.pop();
	}
	outPut();
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		getchar();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf("%c",&map[i][j].cc);
				map[i][j].num = MAX;
				map[i][j].x=i;
				map[i][j].y=j;
				map[i][j].prey=map[i][j].prex=-1;
			}
			getchar();
		}
		bfs();
	}
}
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值