HDu 5433 Xiao Ming climbing (BFS)
题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪:
这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H.
为了逃离这座山,小明必须找到大魔王,并消灭它以消除诅咒.
小明一开始有一个斗志值k,如果斗志为0则无法与大魔王战斗,也就意味着失败.
小明每一步都能从他现在的位置走到他的(N,E,S,W)(N,E,S,W)(N,E,S,W)四个位置中的一个,会消耗(abs(H1−H2))/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)的更多相关文章
- 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 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 4349 Xiao Ming's Hope 找规律
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...
- hdu5433 Xiao Ming climbing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 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) ...
随机推荐
- HDU---4417Super Mario 树状数组 离线操作
题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...
- 【剑指offer】面试题40:数组中只出现一次的数字
题目: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路: 因为只有两个只出现一次的数字,所以所有数字进行异或之后得到值res一定不是0.这样,res ...
- javascript笔记3之数据类型
/* var box = 250; //十进制整型 alert(box); var box = 070; //八进制,按照十进制输出是56 alert(box); var box = 0x1f; // ...
- poj 2100 Graveyard Design(尺取法)
Description King George has recently decided that he would like to have a new design for the royal g ...
- ThinkPHP3.2.3中三大自动中的缺陷问题
我们在使用Thinkphp3.2.3框架时在对数据表进行模型化后就可以使用自动完成功能. 自动完成可以帮助我们更简便的完成对表单内容对数据表(集合)的填充,自动完成是基于: 当实例化数据库user后, ...
- 【转载】cocos2d-x2.2.3和android的平台环境
这两天试图按照教程来学习写游戏移植到的横版过关Android在.在网上找了很多教程,但版本号变化.所使用的工具有细微的差别.所以,现在我们还没有准备好,阅读后,下面的文章.最后能够顺利您的手机上跑起来 ...
- 实现简单的django上传文件
本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...
- MySql 事务+异常处理+异常抛出
-- 测试用表 -- innodb 支持事务 CREATE TABLE `tb_test` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, P ...
- EF中执行原生sql与使用Local获取本地数据
使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据) using (var context = new BloggingContext()) { / ...
- jquery悬停tab2
<style> *{ margin:0; padding:0;} body { font:12px/19px Arial, Helvetica, sans-serif; color:#66 ...