题目链接:

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]]).

代码:

 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<cmath>
 #include<algorithm>
 using namespace std;

 ;

 double dp[maxn][maxn][maxn];
 bool vis[maxn][maxn][maxn];
 char mat[maxn][maxn];

 int n,m,len;
 int X1,Y1,X2,Y2;

 void init(){
     memset(vis,,sizeof(vis));
     memset(dp,0x7f,sizeof(dp));
 }

 ,,,};
 ,,-,};
 double solve(int x,int y,int k){
     if(vis[x][y][k]) return dp[x][y][k];
     vis[x][y][k]=;
     ;i<;i++){
         int tx=x+dx[i],ty=y+dy[i];
         ||tx>n||ty<||ty>m||k+>len||mat[tx][ty]=='#') continue;
         );
         dp[x][y][k]=min(dp[x][y][k],solve(tx,ty,k+)+add);
     }
     return dp[x][y][k];
 }

 int main(){
     int tc;
     scanf("%d",&tc);
     while(tc--){
         init();
         scanf("%d%d%d",&n,&m,&len);
         ;i<=n;i++) scanf();
         scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
         dp[X1][Y1][len]=; vis[X1][Y1][len]=;
         double ans=0x3f;
         ;
         ;k--){
             double tmp=solve(X2,Y2,k);
             if(ans>tmp){
                 flag=;
                 ans=tmp;
             }
         }
         if(flag) printf("%.2lf\n",ans);
         else printf("No Answer\n");
     }
     ;
 }

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&#39;s Hope lucas定理

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

  6. hdu 4349 Xiao Ming&#39;s Hope 规律

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

  7. HDU 4349 Xiao Ming&#39;s Hope

    有这样一个性质:C(n,m)%p=C(p1,q1)*C(p2,q2).......%p,其中pkpk-1...p1,qkqk-1...q1分别是n,m在p进制下的组成. 就完了. #include&l ...

  8. hdu 4349 Xiao Ming&#39;s Hope lucas

    题目链接 给一个n, 求C(n, 0), C(n, 1), ..........C(n, n)里面有多少个是奇数. 我们考虑lucas定理, C(n, m) %2= C(n%2, m%2)*C(n/2 ...

  9. HDU 4349 Xiao Ming&amp;#39;s Hope

    非常无语的一个题. 反正我后来看题解全然不是一个道上的. 要用什么组合数学的lucas定理. 表示自己就推了前面几个数然后找找规律. C(n, m) 就是 组合n取m: (m!(n-m!)/n!) 假 ...

随机推荐

  1. PHP跨域form提交

    因为安全性因素,直接跨域访问是不被允许的. 1.PHP CURL方式 function curlPost($url,$params) { $postData = ''; foreach($params ...

  2. robotium重签名使用解决办法

    用re-sign重新签名,出现error,提示缺少zipalign 解决方案: 下载zipalign.exe,地址:http://pan.baidu.com/s/1geoHemR 下载后将zipali ...

  3. springMVC:通配符的匹配很全面, 但无法找到元素 &#39;mvc:annotation-driven&#39; 的声明

    转自:http://blog.csdn.net/tolcf/article/details/50845483 报错信息:通配符的匹配很全面, 但无法找到元素 'mvc:annotation-drive ...

  4. iOS-旧项目中手动内存管理(MRC)转ARC

    在ARC之前,iOS内存管理无论对资深级还是菜鸟级开发者来说都是一件很头疼的事.我参 加过几个使用手动内存管理的项目,印象最深刻的是一个地图类应用,由于应用本身就非常耗内存,当时为了解决内存泄露问题, ...

  5. 页面打开自动触发onlick事件

    <script type="text/javascript"> // 两秒后模拟点击 setTimeout(function() { // IE if(document ...

  6. 实战1--应用EL表达式访问JavaBean的属性

    (1)编写index.jsp页面,用来收集用户的注册信息 <%@ page language="java" pageEncoding="GBK"%> ...

  7. linux上svn版本库创建小记

    [新建svn仓库] 先创建一个文件夹mkdir /opt/svn/wechat;   然后创建svn版本库    svnadmin create /opt/svn/wechat;   [创建用户组权限 ...

  8. CF 148A Insomnia cure

    题目链接:传送门 题目大意:就是给四个数,和一个d,问1-d中有多少个数字不是那四个数的倍数; 这道题的d数据很小直接暴力可以过: 暴力代码:时间复杂度O(1): #include<stdio. ...

  9. jQuery 之 .stop() 方法

    总结version 1.7版本前.stop([clearQueue][,jumpToEnd])clearQueue: 布尔值,默认是 false;此值处理是相关的动画队列是否移除,如果为 false ...

  10. 协议系列之HTTP协议

    什么是HTTP\HTTPS HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.HTTP协议用于从WWWserver传输超文本到本地浏览器的传输协议,它能使浏览 ...