题意:给一张地图,给出起点和终点,每移动一步消耗体力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. Java Notes

    1.java是解释型语言.java虚拟机能实现一次编译多次运行. 2.JDK(java software Development kit 软件开发包),JRE(java Runtime Environ ...

  2. poj 1562 Oil Deposits (广搜,简单)

    题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...

  3. hdu 3951 Coin Game 博弈论

    思路: 当n<=k时,先手必胜: 当k=1时,n为奇数先手胜,否则后手胜: 当k>1时,先手操作之后必定形成链,后手操作后形成二条一样的链,之后,先手怎么操作,后手就怎么操作,则后手必胜. ...

  4. MySQL 创建数据库并且指定编码

    GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...

  5. Project Euler 84:Monopoly odds 大富翁几率

    Monopoly odds In the game, Monopoly, the standard board is set up in the following way:             ...

  6. 开发版本控制git

    git init 在git命令行中依次输入 touch readme.txt并回车, git add . 点代表所有, git commit -m "init first"并回车, ...

  7. 关于NGUI制作图集在低内存设备上的注意事项

    正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...

  8. Java多线程5:线程等待与唤醒

    原文:http://www.cnblogs.com/skywang12345/p/3479224.html wait(),notify(), notifyAll()等方法介绍在Object.java中 ...

  9. Hadoop HDFS文件常用操作及注意事项(更新)

    1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...

  10. UINavigationController学习笔记

    http://site.douban.com/129642/widget/notes/5513129/note/187701199/ 1-view controllers的关系:Each custom ...