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 isn∗m 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 willk ,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(H1−H2))/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.
This mountain is pretty strange that its underside is a rectangle which size is
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
Ming can go to next position
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(T≤10) , indicating the number of testcases.
ThenT testcases follow.
The first line contains three integersn,m,k ,meaning as in the title(1≤n,m≤50,0≤k≤50) .
Then theN × M matrix follows.
In matrix , the integerH 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.
Then
The first line contains three integers
Then the
In matrix , the integer
Then follow two lines,meaning Xiao Ming's coordinate
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)
(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
- HDOJ 5433 Xiao Ming climbing (BFS+三维标记)
- hdu 5433 Xiao Ming climbing bfs
- HDU 5433 Xiao Ming climbing(BFS)
- HDU 5433 Xiao Ming climbing(bfs+记忆化搜索)
- hdu 5433 Xiao Ming climbing (BFS+dp)
- hdu 5433 Xiao Ming climbing(优先队列+BFS)
- HDU 5433 Xiao Ming climbing
- HDU 5433 Xiao Ming climbing
- hduacm 5433 Xiao Ming climbing题解
- hdu5433 Xiao Ming climbing
- HDU 5433(Xiao Ming climbing)(费用最短路:BFS+优先队列)
- dp hdu5433 Xiao Ming climbing
- hdu 5433 Xiao Ming climbing(最短路)
- HDU - bc - Xiao Ming climbing (优先队列)
- hdu5433 Xiao Ming climbing(BestCoder Round #55 ($) )
- hdoj 2579 Dating with girls(2) 【BFS&&三维数组标记】
- HDOJ 2579 Dating with girls(2) (BFS+三维标记好)
- HDOJ-2579-Dating with girls(2) 【BFS 三维标记】
- iOS——KVO与KCO
- ios 数据存储
- 自定义控件其实很简单1/3
- JFreeChart中文乱码解决方案
- EL表达式使用方法(在jsp中使用)
- HDOJ 5433 Xiao Ming climbing (BFS+三维标记)
- 浅谈操作系统对内存的管理
- Java中可变长参数的使用及注意事项
- Atom 更为先进的文本代码编辑器 - 由 Github 打造的下一代编程开发利器
- Python核心数据类型(内置对象)—— 列表(list)
- Linux下面的DISPLAY环境变量----远程连接图形图像的处理
- 【LeetCode】27 Remove Element
- 流程图特点和说明
- C# MVC 中给js传对象的问题