题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪:

这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H.

为了逃离这座山,小明必须找到大魔王,并消灭它以消除诅咒.

小明一开始有一个斗志值k,如果斗志为0则无法与大魔王战斗,也就意味着失败.

小明每一步都能从他现在的位置走到他的(N,E,S,W)(N,E,S,W)(N,E,S,W)四个位置中的一个,会消耗(abs(H​1​​−H​2​​))/k的体力,每走一步消耗一点斗志。

大魔王很强大,为了留下尽可能多的体力对付大魔王,小明需要找到一条消耗体力最少的路径.

你能帮助小明算出最少需要消耗的体力吗.

思路 : BFS

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define pii pair<int,int>
#define INF 0x7fffffff
struct node{
int x;
int y;
int k;
double cost;
};
double g[55][55],ans;
int sx,sy,ex,ey,n,m,k,flag;
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
char ch[55][55];
queue<node> Q; void BFS(){
int i,x,y;
double temp;
node t ;
t.x = sx;
t.y = sy;
t.k = k;
t.cost = 0;
g[sx][sy] = 0;
Q.push(t);
while(!Q.empty()){
t = Q.front();
Q.pop();
if(t.x == ex && t.y == ey && t.k>=1){
flag = 1;
ans = ans < t.cost ? ans : t.cost;
continue;
}
if(t.k<=1)
continue;
for(i=0;i<4;i++){
x = t.x + dir[i][0];
y = t.y + dir[i][1];
if(x<=0 || x>n || y<=0 || y>m)
continue;
if(ch[x][y] == '#')
continue;
temp = t.cost + fabs((ch[x][y] - ch[t.x][t.y])*1.0/(t.k));
if(temp < g[x][y]){
Q.push((node){x,y,t.k-1,temp});
g[x][y] = temp;
}
}
}
} int main(){
int i,j,T;
cin >> T;
while(T--){
ans = INF;
flag = 0;
while(!Q.empty())
Q.pop();
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
g[i][j] = INF;
for(i=1;i<=n;i++)
scanf("%s",ch[i]+1);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
if(k <= 0 ){
printf("No Answer\n");
continue;
}
BFS();
if(ans < INF)
printf("%.2lf\n",ans);
else
printf("No Answer\n");
}
return 0;
}

HDu 5433 Xiao Ming climbing (BFS)的更多相关文章

  1. HDU 5433 Xiao Ming climbing 动态规划

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5433 Xiao Ming climbing Time Limit: 2000/1000 MS (Ja ...

  2. HDU 5433 Xiao Ming climbing dp

    Xiao Ming climbing Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/ ...

  3. 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 ...

  4. HDU 5433 Xiao Ming climbing

    题意:给一张地图,给出起点和终点,每移动一步消耗体力abs(h1 - h2) / k的体力,k为当前斗志,然后消耗1斗志,要求到终点时斗志大于0,最少消耗多少体力. 解法:bfs.可以直接bfs,用d ...

  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. hdu5433 Xiao Ming climbing

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  7. HDU 4349 Xiao Ming's Hope lucas定理

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

  8. hdu 4349 Xiao Ming's Hope 规律

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

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

随机推荐

  1. SecureCRT 安装及初始化配置

    安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...

  2. Django之验证码 + session 认证

    验证码 + session认证 目录结构 . └── project ├── app01 │   ├── admin.py │   ├── apps.py │   ├── __init__.py │  ...

  3. javascript实现的手风琴折叠菜单效果

    演示地址:http://codepen.io/anon/pen/pJERMq 实现效果: HTML代码: <!DOCTYPE html> <html lang="en&qu ...

  4. 初学者学Java设计模式(一)------单例设计模式

    单例设计模式 单例设计模式是指一个类只会生成一个对象,优点是他可以确保所有对象都访问唯一实例. 具体实现代码如下: public class A { public static void main(S ...

  5. android_launcher的源码详细分析

    转载请注明出处:http://blog.csdn.net/fzh0803/archive/2011/03/26/6279995.aspx 去年做了launcher相关的工作,看了很长时间.很多人都在修 ...

  6. Mac_配置adb环境变量

    1.打开终端Terminal. 2.进入HOME目录:命令行输入 echo(这之间有一个空格) $HOME 3.创建.bash_profile文件:命令行输入 touch(这之间有一个空格).bash ...

  7. gitosis随记

    0.创建git用户 useradd -m git passwd git 1.安装脚本工具(gitosis依赖python) apt-get install python-setuptools 2.gi ...

  8. String.equals()

    名称                                               说明 String.Equals(Obejecct)            确定String实例是否指 ...

  9. hdu 5671 矩阵变换

    Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  10. Java中的Clone机制(浅层复制)

    浅层复制代码: import java.util.*; class Int{ private int i; public Int(int ii){i = ii;} public void increm ...