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

解法:bfs。可以直接bfs,用dp维护最小值……也可以用优先队列优化……但是不能找到终点后就直接输出,因为从不同方向到达终点的消耗不同,终点的前一个状态不一定比这一状态更优……一开始并没有意识到这一点……wa了一篇……后来加了dp维护……但其实在将点弹出的之后再更新vis也可以……以前因为更新vis的问题T过……留下了阴影……

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
char maze[55][55];
int sx, sy, ex, ey;
int n, m, k;
struct node
{
int x, y, k;
double tili;
bool operator < (const node &tmp) const
{
return tili > tmp.tili;
}
node(int x, int y, int k, double tili) : x(x), y(y), k(k), tili(tili) {}
node() {}
};
bool vis[55][55][55];
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
double dp[55][55][55];
double bfs()
{
for(int i = 0; i < 55; i++)
for(int j = 0; j < 55; j++)
for(int k = 0; k < 55; k++)
dp[i][j][k] = 1e8;
memset(vis, 0, sizeof vis);
priority_queue <node> q;
q.push(node(sx, sy, k, 0.0));
vis[sx][sy][k] = 1;
while(!q.empty())
{
node tmp = q.top();
q.pop();
if(tmp.k == 0) continue;
if(tmp.x == ex && tmp.y == ey) return tmp.tili;
for(int i = 0; i < 4; i++)
{
int tx = tmp.x + dir[i][0], ty = tmp.y + dir[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
if(maze[tx][ty] == '#') continue;
if(dp[tx][ty][tmp.k - 1] > (double)abs(maze[tx][ty] - maze[tmp.x][tmp.y]) / (double)tmp.k + tmp.tili)
{
dp[tx][ty][tmp.k - 1] = (double)abs(maze[tx][ty] - maze[tmp.x][tmp.y]) / (double)tmp.k + tmp.tili;
q.push(node(tx, ty, tmp.k - 1, (double)abs(maze[tx][ty] - maze[tmp.x][tmp.y]) / (double)tmp.k + tmp.tili));
}
}
}
return -1.0;
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < n; i++)
scanf("%s", maze[i]);
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
sx--;
sy--;
ex--;
ey--;
double ans = bfs();
if(ans < 0) puts("No Answer");
else printf("%.2f\n", fabs(ans));
}
}
return 0;
}

  

HDU 5433 Xiao Ming climbing的更多相关文章

  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 (BFS)

    题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪: 这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H. 为了逃离这座山,小明必须找到大 ...

  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. HDU 4349 Xiao Ming's Hope lucas定理

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

  7. hdu 4349 Xiao Ming's Hope 规律

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

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

  9. hdu5433 Xiao Ming climbing

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

随机推荐

  1. lua语言入门之Sublime Text设置lua的Build System

    转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...

  2. Fragment (一)

      1,简介 Fragement(碎片)允许将Activity拆分成多个完全独立封装的可重用组件,每个组件有它自己的生命周期和UI布局,由此可见,Fragement依赖于Activity,它的生命周期 ...

  3. TopCoder SRM 633div1

    250pts   PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...

  4. lintcode 中等题:find the missing number 寻找缺失的数

    题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...

  5. AO总结10:MapControl控件

    MapControl对应ArcMap中的数据视图,它封装了Map对象,并提供了额外的属性.方法.事件用于: 1 管理控件的外观.显示属性和地图属性 2 添加并管理控件中的数据层 3 装载Map文档控件 ...

  6. WP布局之Pivot和Panorama

    一.Pivot控件(枢轴控件) Pivot主要用于管理应用中的视图或者页面,此控件在WP中几乎处处可见,不管是短信的左右滑动,还是QQ的左右滑动都是此控件的功劳. 就是图片中的控件,是不是很熟悉呢. ...

  7. 增加oracle数据库最大连接数

    这几天碰到系统不能登陆的情况,初步判断可能是数据库连接满了,(后来检查不是这个原因),做了一次增加数据库最大连接数操作.操作步骤如下 操作系统:Red Hat Enterprise Linux Ser ...

  8. java开发之多线程需要学习和理解的东西

    40个Java多线程问题总结 http://www.codeceo.com/article/40-java-thread-problems.html

  9. [LeetCode#247] Strobogrammatic Number II

    Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...

  10. hashmap的遍历

    import java.util.HashMap;import java.util.Iterator; public class hash { /** * @param args */ public ...