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

这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(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. openstack组件手动部署整合

    preface:当你完全且正确的配置好整个OpenStack ENV 你将能看到的和体验到的!!! 我们先来看看简单效果吧,祝君能在这条路上走的更远,更好;

  2. window.open和window.close的使用详解

    有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢?   我们知道window.open可以新 ...

  3. javascript笔记7之对象数组

    /* var box = new Array(); //声明一个数组,空数组 alert(typeof box); //数组属于object类型 var box = new Array('李炎恢', ...

  4. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  5. html5 750 REM JS换算方法

    在安卓手机低版本浏览器,如果进页面快速执行的话会出现计算宽度不正确的情况,解决方法是放在onload方法里面执行,但这种解决方式在一些高版本浏览器中会出现页面闪动,所以使用判断浏览器版本的方式来解决, ...

  6. OpenMp 基本

      OpenMp是由OpenMP Architecture Review Board牵头提出的,并已被广泛接受的,用于共享内存并行系统的多线程程序设计的一套指导性的编译处理方案(Compiler Di ...

  7. Android Notification使用及取消

    //发送通知 NotificationManager manger = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE ...

  8. javascript之求最值

    求最值: var selections = $("#deliveryGridSalesOrGoods").datagrid('getRows'); var costPrice = ...

  9. AVT Vimba与OpenCV环境配置

    近来,由于项目需求,需要使用AVT的一款相机采集图像并进行相应的算法处理.环境的配置过程较为复杂,特此记录,以做备忘.也给有需要的小伙伴们一些key point的分享. 搭建环境:Windows7 + ...

  10. Windows命令行(DOS命令)教程-3(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_2.html

    五.常用命令 DOS命令总共大约有一百个(包括文本编辑.查杀病毒.配置文件.批处理等),我们这里详细介绍二十个常用的DOS命令. 先介绍一下通配符的概念. 通配符*和? *表示一个字符串 ?只代表一个 ...