HDU - bc - Xiao Ming climbing (优先队列)

Xiao Ming climbing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 853    Accepted Submission(s): 226


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  nm  and every little part has a special coordinate (x,y) and a height  H .

In order to escape from this mountain,Ming needs to find out the devil and beat it to clean up the curse.

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.

Ming can go to next position (N,E,S,W) from his current position that time every step, (abs(H1H2))/k  's physical power is spent,and then it cost  1  point of will.

Because of the devil's strong,Ming has to find a way cost least physical power to defeat the devil.

Can you help Xiao Ming to calculate the least physical power he need to consume.
 

Input
The first line of the input is a single integer  T(T10) , indicating the number of testcases. 

Then  T  testcases follow.

The first line contains three integers  n,m,k  ,meaning as in the title (1n,m50,0k50) .

Then the  N  ×  M  matrix follows.

In matrix , the integer  H  meaning the height of  (i,j) ,and '#' meaning barrier (Xiao Ming can't come to this) .

Then follow two lines,meaning Xiao Ming's coordinate (x1,y1)  and the devil's coordinate (x2,y2) ,coordinates is not a barrier.
 

Output
For each testcase print a line ,if Xiao Ming can beat devil print the least physical power he need to consume,or output " NoAnswer " otherwise.

(The result should be rounded to 2 decimal places)
 

Sample Input
  
  
3 4 4 5 2134 2#23 2#22 2221 1 1 3 3 4 4 7 2134 2#23 2#22 2221 1 1 3 3 4 4 50 2#34 2#23 2#22 2#21 1 1 3 3
 

Sample Output
  
  
1.03 0.00 No Answer
不知道为什么换一个队列方式竟然直接超时,希望哪位大神知道为什么
超时代码:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
    int d = p < q ? 1 : -1;
    while(p != q) {
        cout << *p;
        p += d;
        if(p != q) cout << Gap;
    }
    cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 1e5;
const int MAXN = 50 + 5;
int T, n, m, k;
int sx, sy, ex, ey;
char maps[MAXN][MAXN];
bool success;
bool vis[MAXN][MAXN][MAXN];
double step;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

struct point {
    int k, x, y;
    double t;
    point(int x, int y, double t, int d) : x(x), y(y), t(t), k(d) {}
    bool operator < (const point & a) const {
        return t > a.t;
    }
};

void BFS() {
    success = false;
    memset(vis, false, sizeof(vis));
    priority_queue<point> que;
    que.push(point(sx, sy, 0, k));
    if(k <= 0) return;
    while(!que.empty()) {
        point e = que.top();
        que.pop();
        vis[e.x][e.y][e.k] = true;
        if(e.x == ex && e.y == ey) {
            success = true;
            step = e.t;
            return;
        }
        for(int i = 0; i < 4; i ++) {
            int nx = e.x + dx[i];
            int ny = e.y + dy[i];
            if(e.k - 1 <= 0) continue;
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || maps[nx][ny] == '#' || vis[nx][ny][e.k - 1]) continue;
            que.push(point(nx, ny, e.t + 1.0 * abs(maps[nx][ny] - maps[e.x][e.y]) / e.k , e.k - 1));
        }
    }
}

int main() {
    scanf("%d", &T);
    while(T --) {
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < n; i ++) {
            scanf("%s", maps[i]);
        }
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        sx --;
        sy --;
        ex --;
        ey --;
        BFS();
        if(success) {
            printf("%.2lf\n", step);
        } else {
            printf("No Answer\n");
        }
    }
    return 0;
}


AC代码:
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
    int d = p < q ? 1 : -1;
    while(p != q) {
        cout << *p;
        p += d;
        if(p != q) cout << Gap;
    }
    cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 1e5;
const int MAXN = 50 + 5;
int T, n, m, k;
int sx, sy, ex, ey;
char maps[MAXN][MAXN];
bool success;
bool vis[MAXN][MAXN][MAXN];
double step;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

struct point {
    int k, x, y;
    double t;
    point(int x, int y, double t, int d) : x(x), y(y), t(t), k(d) {}
    bool operator < (const point & a) const {
        return t > a.t;
    }
};

void BFS() {
    success = false;
    memset(vis, false, sizeof(vis));
    priority_queue<point> que;
    que.push(point(sx, sy, 0, k));
    if(k <= 0) return;
    while(!que.empty()) {
        point e = que.top();
        que.pop();
        if(vis[e.x][e.y][e.k]) continue;
        vis[e.x][e.y][e.k] = true;
        if(e.x == ex && e.y == ey) {
            success = true;
            step = e.t;
            return;
        }
        for(int i = 0; i < 4; i ++) {
            int nx = e.x + dx[i];
            int ny = e.y + dy[i];
            if(e.k - 1 <= 0) continue;
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || maps[nx][ny] == '#') continue;
            que.push(point(nx, ny, e.t + 1.0 * abs(maps[nx][ny] - maps[e.x][e.y]) / e.k , e.k - 1));
        }
    }
}

int main() {
    scanf("%d", &T);
    while(T --) {
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < n; i ++) {
            scanf("%s", maps[i]);
        }
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        sx --;
        sy --;
        ex --;
        ey --;
        BFS();
        if(success) {
            printf("%.2lf\n", step);
        } else {
            printf("No Answer\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值