hdu2425

/*
分析:
    简单广搜。
    坑啊~,每组后面不用输出那个空行的。。。


    接近半个月没怎么刷题了,惭愧呀~~~


                                  2012-08-16 12:03
*/








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


int r,c;
int base[4];
int map[25][25];
int flag[25][25];
int x_s,y_s;
int x_e,y_e;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};


struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
};


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


	memset(flag,0,sizeof(flag));


	now.x=x_s;
	now.y=y_s;
	now.step=0;
	flag[now.x][now.y]=1;
	q.push(now);


	while(!q.empty())
	{
		now=q.top();
		q.pop();


		if(now.x==x_e && now.y==y_e)	return now.step;


		for(i=0;i<4;i++)
		{
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];


			if(judge(next.x,next.y))	continue;


			next.step=now.step+base[map[next.x][next.y]];
			flag[next.x][next.y]=1;
			q.push(next);
		}
	}
	return -1;
}
int main()
{
	int Case=1;
	int i,l;
	char str[25];
	while(scanf("%d%d",&r,&c)!=-1)
	{
		scanf("%d%d%d",&base[1],&base[2],&base[3]);
		for(i=0;i<r;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='#')		map[i][l]=1;
				else if(str[l]=='.')map[i][l]=2;
				else if(str[l]=='T')map[i][l]=3;
				else if(str[l]=='@')map[i][l]=-1;
			}
		}


		scanf("%d%d%d%d",&x_s,&y_s,&x_e,&y_e);


		printf("Case %d: %d\n",Case++,BFS());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值