HDOJ 5433 Xiao Ming climbing (BFS+三维标记)

Xiao Ming climbing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1139    Accepted Submission(s): 305


Problem Description
Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can hardly escape.

This mountain is pretty strange that its underside is a rectangle which size is nm and every little part has a special coordinate (x,y) and a height H .

In order to escape from this mountain,Ming needs to find out the devil and beat it to clean up the curse.

At the biginning Xiao Ming has a fighting will k ,if it turned to 0 Xiao Ming won't be able to fight with the devil,that means failure.

Ming can go to next position (N,E,S,W) from his current position that time every step, (abs(H1H2))/k 's physical power is spent,and then it cost 1 point of will.

Because of the devil's strong,Ming has to find a way cost least physical power to defeat the devil.

Can you help Xiao Ming to calculate the least physical power he need to consume.
 

Input
The first line of the input is a single integer T(T10) , indicating the number of testcases.

Then T testcases follow.

The first line contains three integers n,m,k ,meaning as in the title (1n,m50,0k50) .

Then the N × M matrix follows.

In matrix , the integer H meaning the height of (i,j) ,and '#' meaning barrier (Xiao Ming can't come to this) .

Then follow two lines,meaning Xiao Ming's coordinate (x1,y1) and the devil's coordinate (x2,y2) ,coordinates is not a barrier.
 

Output
For each testcase print a line ,if Xiao Ming can beat devil print the least physical power he need to consume,or output " NoAnswer " otherwise.

(The result should be rounded to 2 decimal places)
 

Sample Input
  
  
3 4 4 5 2134 2#23 2#22 2221 1 1 3 3 4 4 7 2134 2#23 2#22 2221 1 1 3 3 4 4 50 2#34 2#23 2#22 2#21 1 1 3 3
 

Sample Output
  
  
1.03 0.00 No Answer 题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪:这座山的底面是矩形的, 而且矩形的每一小块都有一个特定的坐标(x,y)(x,y)和一个高度HHH.为了逃离这座山,小明必须找到大魔王,并消 灭它以消除诅咒.小明一开始有一个斗志值kkk,如果斗志为0则无法与大魔王战斗,也就意味着失败.小明每一步都能 从他现在的位置走到他的(N,E,S,W)四个位置中的一个,会消耗(abs(H1−H2))/k(abs(H_1-H_2))/k的体力,然 后消耗一点斗志。大魔王很强大,为了留下尽可能多的体力对付大魔王,小明需要找到一条消耗体力最少的路径.你能 帮助小明算出最少需要消耗的体力吗. 思路:刚开始看错了,不是每次都是除以k,而是除以当前拥有的斗志值,即a.hp。用v[x][y][k]表示走到(x,y) 位置的最小体力消耗值,如果此状态出现过或者又一次走到了这一步且消耗值小于此值,那么此值变化,因为每一步 到达时的斗志值不一样,所以不要忘了v的初始化,因为不是只是寻找能否到达,而是在能到达的基础上要求最小消 耗体力值,所以要比较。。。 坑点1:小明坐标和魔王坐标有可能在同一个点。 坑点2:k的值可能为0。 ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 10001
#define LL long long
#define INF 0x7fffffff*1.0
#define mem(x) memset(x,0,sizeof(x))
using namespace std;
int n,m,k,ex,ey;
double ans;
int map[55][55];
double v[55][55][55];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s
{
	int x;
	int y;
	double num;
	int hp;
};
int fab(int a)
{
	if(a<0)
	return -a;
	else
	return a;
}
int check(s aa)
{
	if(aa.x<0||aa.x>=n||aa.y<0||aa.y>=m||map[aa.x][aa.y]==-1)
	return 1;
	return 0;
}
void bfs(int xx,int yy)
{
	//printf("%d %d\n",xx,yy);
	int i;
	queue<s>q;
	s a,b;
	a.x=xx;a.y=yy;a.num=0;a.hp=k;
	v[xx][yy][k]=0;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		//printf("a.x=%d a.y=%d\n",a.x,a.y);
		b.hp=a.hp-1;
		if(b.hp<=0)
		continue;
		for(i=0;i<4;i++)
		{
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
			//printf("%d %d\n",b.x,b.y);
			if(check(b))
			continue;
			//printf("sec %d %d\n",b.x,b.y);
			double kk=1.0*fab(map[a.x][a.y]-map[b.x][b.y])/a.hp;
			//printf("kk=%lf\n",kk);
			b.num=a.num+kk;
			//printf("b.num=%lf\n",b.num);
			//printf("v[b.x][b.x][b.hp]=%lf\n",v[b.x][b.y][b.hp]);
			if(v[b.x][b.y][b.hp]<0||v[b.x][b.y][b.hp]>b.num)
			{
				//printf("thi %d %d\n",b.x,b.y);
			    v[b.x][b.y][b.hp]=b.num;
			    //printf("ans=%lf\n",ans);
			    //printf("b.x=%d b.y=%d\n",b.x,b.y);
			    if(b.x==ex&&b.y==ey)
			    ans=min(ans,b.num);
				q.push(b);
			}
		}
	}
}
int main()
{
	int t,i,j,q;
	int bx,by;
	char ss[1000];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i<n;i++)
		{
		    scanf("%s",ss);
		    for(j=0;j<m;j++)
		    {
		    	if(ss[j]=='#')
		    	map[i][j]=-1;
		    	else
		    	map[i][j]=ss[j]-'0';
		    	for(q=0;q<=52;q++)
		    	v[i][j][q]=-1.0;
			}
	    }
		scanf("%d%d",&bx,&by);
		scanf("%d%d",&ex,&ey);
		bx--;by--;ex--;ey--;
		if(bx==ex&&by==ey)
		{
			if(k>0)
			printf("0.00\n");
			else
			printf("No Answer\n");
			continue;
		}
		ans=INF;
		bfs(bx,by);
		//printf("%lf\n",ans);
		if(ans<INF)
		printf("%.2lf\n",ans);
		else
		printf("No Answer\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值