题目链接:

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 ;
}

最新文章

  1. Swif - 可选型
  2. maven-各配置文件详解
  3. Linux下修改系统编码的操作记录
  4. for循坏的穷举与迭代,while、do while循环
  5. PayPal 开发详解(五):创建第一个应用,获取clientId和clientSecret
  6. (转载)OC学习篇之---协议的概念和用法
  7. win7怎么调屏幕自动休眠时间
  8. yzoi1777倒水问题的详细解法
  9. jsp中button传值
  10. date命令使用总结【转载】
  11. C#通用权限管理-程序安全检查,这些你一定要考虑到位
  12. springboot-helloworld
  13. 安装JDK,配置环境变量
  14. 获取linux帮助命令
  15. dev控件 xtraTabbedMdiManager 如何将关闭子窗体改为收回主窗体内
  16. shell脚本学习总结(不断更新中)
  17. 深度学习卷积网络中反卷积/转置卷积的理解 transposed conv/deconv
  18. centos7系统下nginx安装并配置开机自启动操作
  19. PHP中文件操作(2)-- 写文件
  20. .net mvc控制器传递方法到视图

热门文章

  1. Redis高可用详解:持久化技术及方案选择 (推荐)--转载自编程迷思博客www.cnblogs.com/kismetv/p/8654978.html
  2. C语言链栈
  3. C#中的抽象方法,虚方法,接口之间的对比
  4. Git最常用的命令 总结
  5. 关于使用READ TABLE语句
  6. Oracle Data Provider for .NET Support for Microsoft .NET Core
  7. 20155306 实验三 敏捷开发与XP实践
  8. BZOJ1010_玩具装箱toy_KEY
  9. 回顾RAC安装过程中对ASM的处理
  10. 【BZOJ4698】[SDOI2008]Sandy的卡片