HDU 5433 Xiao Ming climbing 动态规划
题目链接:
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
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.
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.
(The result should be rounded to 2 decimal places)
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
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 动态规划的更多相关文章
- HDU 5433 Xiao Ming climbing dp
Xiao Ming climbing Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/ ...
- 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 ...
- HDU 5433 Xiao Ming climbing
题意:给一张地图,给出起点和终点,每移动一步消耗体力abs(h1 - h2) / k的体力,k为当前斗志,然后消耗1斗志,要求到终点时斗志大于0,最少消耗多少体力. 解法:bfs.可以直接bfs,用d ...
- HDu 5433 Xiao Ming climbing (BFS)
题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪: 这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H. 为了逃离这座山,小明必须找到大 ...
- 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/ ...
- HDU 4349 Xiao Ming's Hope lucas定理
Xiao Ming's Hope Time Limit:1000MS Memory Limit:32768KB Description Xiao Ming likes counting nu ...
- hdu 4349 Xiao Ming's Hope 规律
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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) ...
- hdu5433 Xiao Ming climbing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
随机推荐
- SQL逻辑查询处理的步骤序号
(8)SELECT (9) DISTINCT <select_list> (1)FROM <left_table> (3)<join_type>JOIN<ri ...
- MYSQL 入门全套
MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅 ...
- 配置redis一直启动
1. 进入 DOS窗口 2. 在进入redis的安装目录 3. 输入:redis-server --service-install redis.windows.conf --loglevel verb ...
- 从码云把之前的代码git push 回IDEA 对IDEA里的文件进行简单操作
前情提要:我的IDEA里的项目之前已经和码云连接成功可以上传.但我直接在电脑文件夹里对文件进行重命名.剪切.粘贴等操作之后IDEA对操作后的文件不识别,无奈之下我将码云上之前的代码推回重新新建了项目. ...
- 20155328 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告
20155328 <Java程序设计>实验一(Java开发环境的熟悉) 实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发: 打开windows ...
- 20155332 实验二 Java面向对象程序设计
目录 一.单元测试和TDD 任务一:实现百分制成绩转成"优.良.中.及格.不及格"五级制成绩的功能 任务二:以TDD的方式研究学习StringBuffer 二.面向对象三要素:封装 ...
- 关于正则表达式"\b"
今天刚刚开始看正则表达式就遇到一个十分头疼的问题,原文是这样的: “不幸的是,很多单词里包含hi这两个连续的字符,比如him,history,high等等.用hi来查找的话,这里边的hi也会被找出来. ...
- C#数据流
C#编程中数据流的使用一直不很熟练,没有一个系统的认识,但是它的重要性显然不言而喻.System.IO下的Stream类是所有数据流的基类,当我们对数据进行逐字节操作时,首先需要将数据转换为数据流.C ...
- ARKit-1
1.1-AR技术简介 增强现实技术(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像.视频.3D模型的技术,这种技术的目标是在屏幕上把虚拟世界套在 ...
- day 12 列表字典 补充
1.列表list的遍历 ##### while遍历 需要len(list) list = [11,22,33,44,55] len_list = len(list) i = 0 while i< ...