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 isn∗m 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 willk ,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(H1−H2))/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.
This mountain is pretty strange that its underside is a rectangle which size is
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
Ming can go to next position
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(T≤10) , indicating the number of testcases.
ThenT testcases follow.
The first line contains three integersn,m,k ,meaning as in the title(1≤n,m≤50,0≤k≤50) .
Then theN × M matrix follows.
In matrix , the integerH 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.
Then
The first line contains three integers
Then the
In matrix , the integer
Then follow two lines,meaning Xiao Ming's coordinate
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)
(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
- HDU - bc - Xiao Ming climbing (优先队列)
- hdu 5433 Xiao Ming climbing(优先队列+BFS)
- HDU 5433(Xiao Ming climbing)(费用最短路:BFS+优先队列)
- HDU 5433 Xiao Ming climbing
- HDU 5433 Xiao Ming climbing
- hdu 5433 Xiao Ming climbing bfs
- HDU 5433 Xiao Ming climbing(BFS)
- hdu5433 Xiao Ming climbing
- hdu 5433 Xiao Ming climbing(最短路)
- HDU 5433 Xiao Ming climbing(bfs+记忆化搜索)
- hdu 5433 Xiao Ming climbing (BFS+dp)
- dp hdu5433 Xiao Ming climbing
- hduacm 5433 Xiao Ming climbing题解
- Xiao Ming's Hope HDU
- hdu5433 Xiao Ming climbing(BestCoder Round #55 ($) )
- HDOJ 5433 Xiao Ming climbing (BFS+三维标记)
- HDU 4349--Xiao Ming's Hope
- hdu 4349 Xiao Ming's Hope
- 笔记本手动关闭显示器(附测试代码)
- 支付宝接口开发总结(php)
- MySql 里的IFNULL、NULLIF和ISNULL用法
- 用python和shell检测centos版本
- AIX系统下RAC故障之一--CRS服务启动失败(coredump)
- HDU - bc - Xiao Ming climbing (优先队列)
- 利用functools模块防止被修饰器修改属性
- Android 中的 Service 全面总结
- C#必知的基础
- block的实质
- Ubuntu安装jdk报错:-bash /usr/.../java:No such file or directroy
- ubuntu14.04 安装matlab2014a
- Qt里textbrowser的小应用(textbrowser输出变量的值)
- mysql问题