hdu 5433 Xiao Ming climbing

题意:一张图,给出起点和终点,给一个fighting值,每走一步fighting消耗1,并且消耗(abs(H1−H2))/fighting 的 physical power。求起点到终点消耗最小的physical pow。

解:题目上有一句"At the biginning Xiao Ming has a fighting will k,if it turned to 0 Xiao Ming won‘t be able to fight with the devil,that means failure.",原来到终点的时候fighting值可以为0,昨天这里错了一晚上。

思路:优先队列或者普通bfs都可以,这里用优先队列实现。

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long uLL;

const int N = 55;
const double PI = acos(-1.0);
const double eps = 1e-6;
const double INF = 1e9+7;

int n, m, k;
double csm_ft;

char mp[N][N];

struct pt {
    int x, y;
    pt(){}
    pt(int _x, int _y):x(_x), y(_y) {}
    pt operator + (const pt& add) {
        return pt(this->x + add.x, this->y + add.y);
    }
    bool operator == (const pt& eq) {
        return (this->x == eq.x && this->y == eq.y);
    }
};

struct node {
public:
    pt p;
    int ft;
    double csm;
    node(){}
    node(pt _p, int _ft, double _csm):p(_p), ft(_ft), csm(_csm) {}
    bool operator < (const node& cmp) const {
        //return ft < cmp.ft;
        return csm > cmp.csm;
    }
};

priority_queue <node> q;

pt bg, ed;

double vis[N][N][N];

pt dir[4] = {pt(1, 0), pt(-1, 0), pt(0, 1), pt(0, -1)};

bool in_bound(pt p){
    return (p.x >= 0 && p.x < n && p.y >= 0 && p.y < m);
}

double bfs(int k) {
    while(!q.empty())   q.pop();

    q.push(node(bg, k, 0));
    vis[bg.x][bg.y][k] = 0.0f;

    pt _tp;
    double s_csm, ans = INF;
    node t;

    while(!q.empty()){
        t = q.top();
        q.pop();

        if(t.ft <= 0)   continue;

        if(t.p == ed) {
            ans = min(ans, t.csm);
            continue;
        }
        for(auto d : dir) {
            _tp = t.p + d;
            if(!in_bound(_tp) || mp[_tp.x][_tp.y] == ‘#‘)  continue;

            s_csm = abs(mp[_tp.x][_tp.y] - mp[t.p.x][t.p.y]) * 1. / t.ft;
            //vis[_tp.x][_tp.y] = min(vis[_tp.x][_tp.y], t.csm);
            if(t.csm + s_csm >= vis[_tp.x][_tp.y][t.ft - 1])  continue;

            vis[_tp.x][_tp.y][t.ft - 1] = t.csm + s_csm;
            q.push(node(_tp, t.ft - 1, t.csm + s_csm));
            //printf("%d %d %d %f\n", _tp.x, _tp.y, t.ft - 1, t.csm + s_csm);
        }
    }

    return ans;
}

int main(){
    //freopen("data.in", "r", stdin);
    int T;
    cin >> T;
    while(T--){
        cin >> n >> m >> k;
        for(int i = 0; i < n; ++i) {
            scanf("%s", mp[i]);
            for(int j = 0; j < m; ++j) {
                for(int l = 0; l <= k; ++l) {
                    vis[i][j][l] = INF;
                }
            }
        }
        scanf("%d%d", &bg.x, &bg.y);
        scanf("%d%d", &ed.x, &ed.y);
        bg.x--;
        bg.y--;
        ed.x--;
        ed.y--;
        double ans = bfs(k);
        if(ans >= INF) puts("No Answer");
        else    printf("%.2f\n", ans);
    }
    return 0;
}

时间: 2024-12-24 22:16:30

hdu 5433 Xiao Ming climbing的相关文章

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 hardly escape. This mountain is pretty strange that its underside is a rectangle which size is n∗m and every little part has a special coordinate(x,y)a

hdu 5433 Xiao Ming climbing(用dp代替搜索)

题意:有n*m的矩阵,人的初始力量值为k,矩阵上有数字表示高度,相邻点高度差的绝对值除k,为力量消耗量,求给定起点终点消耗力量的最小值: 思路:dp[k][x][y]表示第k步走到(x,y)时的最小力量消耗,建立递推: #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define inf 0x3f3f3f3f int t,

hdu 5433 Xiao Ming climbing (BFS)

DFS超时,当时头脑混乱不会改成BFS.过了两天,思路清晰,一次AC,真爽. 思路:用dp[x][y][p]表示走到(x,y)且剩余斗志为p的最少体力,bfs的过程中,只有满足边界条件并且下一步算出来的值要小于dp[next.x][next.y][next.p]时才把这个点放入队列. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include &

HDU 4349 Xiao Ming&#39;s Hope (Lucas)

题意:给定一个 n,问你在 C(n, 0) - C(n , n) 中有多少个奇数. 析:Lucas定理,C(b[i], a[i]),只要不为0,那么就是奇数,然后b[i],是固定的,也就是说a[i] 只有 b[i]+1种情况.最后乘起来就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

HDU 4349 Xiao Ming&#39;s Hope(数学题,Lucas定理)

解题思路: 深入理解lucas定理. Lucas定理:把n写成p进制a[n]a[n-1]a[n-2]...a[0],把m写成p进制b[n]b[n-1]b[n-2]...b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1],b[n-1])*C(a[n-2],b[-2])*....*C(a[0],b[0])模p同余. 这题p为2,所以a[i]和b[i]为0或者1.又因为C(1,0), C(1,1), 即当n等于1时,结果才等于1. 所以写出n的二进制,m从0遍历到n,每一次遍历把m写

HDU 4349 Xiao Ming&#39;s Hope

很无语的一个题. 反正我后来看题解完全不是一个道上的. 要用什么组合数学的lucas定理. 表示自己就推了前面几个数然后找找规律. C(n, m) 就是 组合n取m: (m!(n-m!)/n!) 如果n==11 : C(11,0):C(11,1):C(11,2):C(11,3):C(11,4):C(11,5): 分别为 (1/1); (1 / 11) ; (11*10 / 2*1)  ;   (11*10*9 / 3*2*1); (11*10*9*8 / 4*3*2*1) ;  (11*10*9

hdu 4349 Xiao Ming&#39;s Hope 规律

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to

HDU 4349 Xiao Ming&#39;s Hope lucas定理

Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2