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

来源:互联网 发布:dota2画质优化 编辑:程序博客网 时间:2024/05/17 02:53

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
34 4 521342#232#2222211 13 34 4 721342#232#2222211 13 34 4 502#342#232#222#211 13 3
 

Sample Output
1.030.00No 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
原创粉丝点击