题目链接:

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; const int maxn=; 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));
} const int dx[]={-,,,};
const int dy[]={,,-,};
double solve(int x,int y,int k){
if(vis[x][y][k]) return dp[x][y][k];
vis[x][y][k]=;
for(int i=;i<;i++){
int tx=x+dx[i],ty=y+dy[i];
if(tx<||tx>n||ty<||ty>m||k+>len||mat[tx][ty]=='#') continue;
double add=fabs((mat[x][y]-mat[tx][ty])*1.0)/(k+);
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);
for(int i=;i<=n;i++) scanf("%s",mat[i]+);
scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
dp[X1][Y1][len]=; vis[X1][Y1][len]=;
double ans=0x3f;
int flag=;
for(int k=len;k>=;k--){
double tmp=solve(X2,Y2,k);
if(ans>tmp){
flag=;
ans=tmp;
}
}
if(flag) printf("%.2lf\n",ans);
else printf("No Answer\n");
}
return ;
}

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. 在mac上使用tar.gz安装mysql

    官方: download: https://dev.mysql.com/downloads/mysql/ mysql参考文档:https://dev.mysql.com/doc/ 环境: macOS ...

  2. java通过get或post方式传到PHP的某控制器的某方法下

    [java]package test4;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStr ...

  3. android4.4 重启的开机不播放开机铃声,按power键的开机播放开机铃声

    平台:A33Android4.4Linux3.4 功能描述:实现重启的开机不播放开机铃声,按power键的开机播放开机铃声 一,无论关机还是重启都会经过rebootOrShutdown这个方法,在方法 ...

  4. LSTM网络应用于DGA域名识别--文献翻译--更新中

    原文献名称 Predicting Domain Generation Algorithms with Long Short-Term Memory Networks原文作者 Jonathan Wood ...

  5. Java NIO (1)

    Java NIO (1) 看了下java核心技术这本书 关于nio的部分介绍比较少,而且如果自己写服务器的话nio用的还是比较多,整理一下nio的资料 java中nio主要是三个组件 Buffers ...

  6. GNU Radio在SDR领域的应用

    1 Software Defined Radio 软件无线电(Software Defined Radio,SDR)是一种实现无线通信的新概念和体制.其中以往只能在硬件中实现的组件(例如混频器,滤波器 ...

  7. 学习Java的必要知识点记录

    在java中什么是类和对象 在还没有学习java类和对象的时候,基本上都是解决的一些比较简单的小程序,仅仅也就是几十行代码而已,如果要开发的是一个很大的程序,需要用到几万行甚至是几十万行代码的量,如果 ...

  8. 20155302 2016-2017-2 《Java程序设计》第3周学习总结

    20155302 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 两个基本的标准类:java.util.Scanner与java.math.BigDecima ...

  9. 20145207 java第二周学习总结

    教材学习内容总结 这部分可能要扒一些课本而上的东西了.在第三章中,知道了Java可区分为基本类型和类类型两大类型系统,其中类类型也称为参考类型.在这一周主要学习了类类型. 对象(Object):存在的 ...

  10. 洛谷 P1941 飞扬的小鸟

    洛谷 P1941 飞扬的小鸟 原题链接 首先吐槽几句 noip都快到了,我还不刷起联赛大水题! 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节 ...