题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5433

Xiao Ming climbing

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

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 n∗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 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(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.

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

Then T testcases follow.

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

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

题解:

  看网上都是bfs的解法,这里来一发动态规划。

  设dp[i][j][k]代表小明走到(i,j)时还剩k个单位的fighting will的状态;

  令(i',j') 表示(i,j)上下左右的某一点,那么易得转移方程:

    dp[i][j][k]=min(dp[i][j][k],dp[i'][j'][k+1]+abs(H[i][j]-H[i'][j'])/(k+1))

  由于状态转移的顺序比较复杂,所有可以用记忆化搜索的方式来求解。

  最终ans=min(dp[x2][y2][1],......,dp[x2][y2][k]]).

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. const int maxn=;
  9.  
  10. double dp[maxn][maxn][maxn];
  11. bool vis[maxn][maxn][maxn];
  12. char mat[maxn][maxn];
  13.  
  14. int n,m,len;
  15. int X1,Y1,X2,Y2;
  16.  
  17. void init(){
  18. memset(vis,,sizeof(vis));
  19. memset(dp,0x7f,sizeof(dp));
  20. }
  21.  
  22. const int dx[]={-,,,};
  23. const int dy[]={,,-,};
  24. double solve(int x,int y,int k){
  25. if(vis[x][y][k]) return dp[x][y][k];
  26. vis[x][y][k]=;
  27. for(int i=;i<;i++){
  28. int tx=x+dx[i],ty=y+dy[i];
  29. if(tx<||tx>n||ty<||ty>m||k+>len||mat[tx][ty]=='#') continue;
  30. double add=fabs((mat[x][y]-mat[tx][ty])*1.0)/(k+);
  31. dp[x][y][k]=min(dp[x][y][k],solve(tx,ty,k+)+add);
  32. }
  33. return dp[x][y][k];
  34. }
  35.  
  36. int main(){
  37. int tc;
  38. scanf("%d",&tc);
  39. while(tc--){
  40. init();
  41. scanf("%d%d%d",&n,&m,&len);
  42. for(int i=;i<=n;i++) scanf("%s",mat[i]+);
  43. scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
  44. dp[X1][Y1][len]=; vis[X1][Y1][len]=;
  45. double ans=0x3f;
  46. int flag=;
  47. for(int k=len;k>=;k--){
  48. double tmp=solve(X2,Y2,k);
  49. if(ans>tmp){
  50. flag=;
  51. ans=tmp;
  52. }
  53. }
  54. if(flag) printf("%.2lf\n",ans);
  55. else printf("No Answer\n");
  56. }
  57. return ;
  58. }

HDU 5433 Xiao Ming climbing 动态规划的更多相关文章

  1. HDU 5433 Xiao Ming climbing dp

    Xiao Ming climbing Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/ ...

  2. hdu 5433 Xiao Ming climbing(bfs+三维标记)

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

  3. HDU 5433 Xiao Ming climbing

    题意:给一张地图,给出起点和终点,每移动一步消耗体力abs(h1 - h2) / k的体力,k为当前斗志,然后消耗1斗志,要求到终点时斗志大于0,最少消耗多少体力. 解法:bfs.可以直接bfs,用d ...

  4. HDu 5433 Xiao Ming climbing (BFS)

    题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪: 这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H. 为了逃离这座山,小明必须找到大 ...

  5. HDU 4349 Xiao Ming's Hope 找规律

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...

  6. HDU 4349 Xiao Ming's Hope lucas定理

    Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB  Description Xiao Ming likes counting nu ...

  7. hdu 4349 Xiao Ming's Hope 规律

    Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 4349——Xiao Ming's Hope——————【Lucas定理】

    Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu5433 Xiao Ming climbing

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

随机推荐

  1. ASP.net 加载不了字体Failed to load resource: the server responded with a status of 404 (Not Found)

    在bootstrap下加载不了字体内容.出现下列错误. 1.打开IIS找到部署的网站,点击MIME类型,把.woff和.woff2两个类型分别添加到新类型中,重启网站即可.  

  2. laychat聊天功能

    windows版本:1.直接下载laychat聊天室压缩包,并解压到PHPstudy本地PHP环境中去:2.进入E:\PHPTutorial\WWW\laychat-master\vendor\Wor ...

  3. ruby安装devkit

    双击下载文件,指定解压路径,路径中不能有空格.如C:\DevKit,这个路径就是<DEVKIT_INSTALL_DIR>. > cd <DEVKIT_INSTALL_DIR&g ...

  4. golang基础--strcut结构体

    结构体struct类似python语言中的类class,结构体类的元素可以是一个变量,或者函数或者其它的类型,好比python的属性和方法. // struct结构体,类似python语言中的clas ...

  5. Hibernate学习笔记四

    1 整合log4j(了解) l slf4j 核心jar  : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. l 整合导入jar包 log4j 核心包 ...

  6. websocket简单入门

    今天说起及时通信的时候,突然被问到时用推的方式,还是定时接受的方式,由于之前页面都是用传统的ajax处理,可能对ajax的定时获取根深蒂固了,所以一时之间没有相同怎么会出现推的方式呢?当被提及webs ...

  7. HDFS(0.20.2)运营中急救方案

    这段时间折腾的都是hadoop和lucene,总结了hadoop在运营过程中出现问题时的解决方案,请大家指教! HDFS(0.20.2)运营中急救方案 1           Namenode 挂掉( ...

  8. 20155215 2016-2017-2《Java程序设计》课程总结

    20155215 2016-2017-2<Java程序设计>课程总结 一.(按顺序)每周作业链接汇总 预备作业1: 对于JAVA课程本身的期望和理解.以及期望的师生关系是什么样的,自己印象 ...

  9. 20155234 实验三 敏捷开发与XP实践

    20155234 实验三 敏捷开发与XP实践 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验步骤 (一)敏捷开发与XP 敏捷开发(Agile Development)是一种以人为核心.迭 ...

  10. 20155316 2016-2017-2 《Java程序设计》第2周学习总结

    教材学习内容总结 学习主要内容:基本类型介绍及流程控制简介 关键点:关键记住JAVA的大体框架,可以类比C语言结合着记.相较于C不同且值得关注的主要信息有: 基本类型的不同:byte.boolean. ...