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

来源:互联网 发布:亦庄亚马逊云计算 编辑:程序博客网 时间:2024/05/17 02:20

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
34 4 521342#232#2222211 13 34 4 721342#232#2222211 13 34 4 502#342#232#222#211 13 3
 

Sample Output
1.030.00No 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;elsereturn 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");elseprintf("No Answer\n");continue;}ans=INF;bfs(bx,by);//printf("%lf\n",ans);if(ans<INF)printf("%.2lf\n",ans);elseprintf("No Answer\n");}return 0;}


0 0