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

这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(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. poj1305:概念水题

    了解一下毕达哥拉斯三元组概念= = 暴力求出所有的本源三元组即可 代码: #include <iostream> #include <stdio.h> #include< ...

  2. App架构设计经验谈:接口的设计

    App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...

  3. 关于cvAbs的那些事

    void cvAbs(const  CvArr* src, const   CvArr*    dst); cvAbs :计算数组中所有的元素的绝对值 // cvAbs函数的使用.cpp : 定义控制 ...

  4. Oracle11gRAC安装

    安装Oracle RAC 一.硬件环境 ①用虚拟机搭建两台机器,操作系统都为: [root@node1 ~]# cat /etc/issue Red Hat Enterprise Linux Serv ...

  5. js获取url传递参数(转的,原作不详)

    这里是一个获取URL带QUESTRING参数的JAVASCRIPT客户端解决方案,相当于asp的request.querystring,PHP的$_GET 函数: <Script languag ...

  6. [原创作品]html css改变浏览器选择文字的背景和颜色

    又很久没有'剥壳'了,最近在为一家公司做一个生产管理解决方案.所以都很忙.今天的话题很简单,就做一个很简单的网页特效.今天偶然浏览到一个网站,他们在选择文字时,样子不是蓝背景和白色字体那么单调,感觉这 ...

  7. [Redux] Extracting Container Components -- VisibleTodoList

    Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...

  8. 关于C语言中的inline

    在c中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间是有限的 ...

  9. winscp连接vmware ubuntu

    winscp连接vmware ubuntu,因为第一次安装在虚拟机上的ubuntu是没有ssh服务的,不能在windows上通过工具进行连接,所以需要以下步骤进行ssh配置     1:检查ssh状态 ...

  10. ORACLE SQL 组函数【weber出品必属精品】

    组函数:对一组数据进行加工,每组数据返回一个值 常用的组函数:count()  avg()  max()   min()  sum()   count()函数  1. count(*) :返回总共的行 ...