HDU 5433 Xiao Ming climbing dp
Xiao Ming climbing
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=629&pid=1002
Description
小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪:
这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)(x,y)和一个高度HH.
为了逃离这座山,小明必须找到大魔王,并消灭它以消除诅咒.
小明一开始有一个斗志值kk,如果斗志为0则无法与大魔王战斗,也就意味着失败.
小明每一步都能从他现在的位置走到他的(N,E,S,W)(N,E,S,W)四个位置中的一个,会消耗(abs(H_1-H_2))/k(abs(H1−H2))/k的体力,然后消耗一点斗志。
大魔王很强大,为了留下尽可能多的体力对付大魔王,小明需要找到一条消耗体力最少的路径.
你能帮助小明算出最少需要消耗的体力吗.
Input
第一行输入一个整数T( 1 \leq T \leq 10 )T(1≤T≤10)
接下来有TT行TT组数据,每组数据有三个整数n,m,kn,m,k含义如题(1 \leq n,m \leq 50, 0 \leq k \leq 50)(1≤n,m≤50,0≤k≤50)
接下来有nn行,每行mm个字符,如果是数字则表示(i,j)(i,j)的高度H(0 \leq H \leq 9)H(0≤H≤9),'#'表示障碍
最后两行分别输入小明的坐标(x_1,y_1)(x1,y1)和大魔王的坐标(x_2,y_2)(x2,y2),小明和魔王都不在障碍上。
Output
每组数据对应输出满足要求的体力(保留两位小数)。
如果无法逃离,则输出"No \ AnswerNo Answer"
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
HINT
题意
题解:
dp咯,三维dp
dp[i][j][k]表示我现在在i,j位置体力值为k的最小花费是多少
用类似spfa一样转移就好了
代码:
//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 500001
#define mod 1001
#define eps 1e-7
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* string s[];
int n,m;
int k;
double dp[][][];;
struct node
{
int x,y,z;
};
int inq[][][];
int dx[]={,,-,};
int dy[]={,,,-};
int main()
{
int t=read();
while(t--)
{
memset(dp,,sizeof(dp));
memset(inq,,sizeof(inq));
n=read(),m=read();
scanf("%d",&k);
for(int i=;i<n;i++)
cin>>s[i]; int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
if(x1==x2&&y1==y2)
{
if(k==)
printf("No Answer\n");
else
printf("0.00\n");
continue;
}
x1--,y1--,x2--,y2--;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
dp[i][j][k]=inf;
}
}
}
queue<node> Q;
node ttt;
ttt.x=x1,ttt.y=y1,ttt.z=k;
Q.push(ttt);
node now;
now.x = x1,now.y = y1,now.z = k;
dp[now.x][now.y][now.z]=;
inq[now.x][now.y][now.z]=;
while(!Q.empty())
{
now = Q.front();
Q.pop();
inq[now.x][now.y][now.z]=;
for(int i=;i<;i++)
{
node next = now;
next.x+=dx[i];
next.y+=dy[i];
next.z--;
if(next.z==)
continue;
if(next.x<||next.x>=n)
continue;
if(next.y<||next.y>=m)
continue;
if(s[next.x][next.y]=='#')
continue;
double num1 = (int)(s[next.x][next.y]-'');
double num2 = (int)(s[now.x][now.y]-'');
double ss = fabs(num1-num2)/now.z;
if(dp[now.x][now.y][now.z]+ss<dp[next.x][next.y][next.z])
{
dp[next.x][next.y][next.z]=dp[now.x][now.y][now.z]+ss;
if(!inq[next.x][next.y][next.z])
{
inq[next.x][next.y][next.z]=;
Q.push(next);
}
}
}
}
double ans = inf;
for(int i=;i<;i++)
ans = min(ans,dp[x2][y2][i]);
if(ans == inf)
printf("No Answer\n");
else
printf("%.2lf\n",ans);
}
}
HDU 5433 Xiao Ming climbing dp的更多相关文章
- HDU 5433 Xiao Ming climbing 动态规划
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5433 Xiao Ming climbing Time Limit: 2000/1000 MS (Ja ...
- 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 ...
随机推荐
- 大四实习准备2_java异常处理_android控件练习
2015-4-24 Java 异常处理 可以有多个catch;ArrayIndexOutOfBoundsException类是Exception类的子类RuntimeException类的一个间接子类 ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求
具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Setti ...
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的 ...
- acdream 瑶瑶带你玩激光坦克 (模拟)
瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) Submi ...
- acdream 小晴天老师系列——竖式乘法(简单穷举)
小晴天老师系列——竖式乘法 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- (转)solr排序OOM解决方法
转自 http://topcat.iteye.com/blog/1293650 问题 lucene使用排序时会将被排序字段全部加入内存再进行排序,当多次使用不同字段进行排序时会造成OOM问题 解决方案 ...
- [转] python程序的调试方法
qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...
- STM32 串口功能 库函数 详解和DMA 串口高级运用(转载)
数据传输时要从支持那些相关的标准?传输的速度?什么时候开始?什么时候结束?传输的内容?怎样防止通信出错?数据量大的时候怎么弄?硬件怎么连接出发,当然对于stm32还要熟悉库函数的功能 具起来rs232 ...
- rtems总结
rtems 历史背景及现状 常用的api 和参数介绍 rtems_interrupt_enable rtems_interrupt_is_in_progress rtems_cache_flush_r ...