【HDOJ】2425 Hiking Trip

优先级队列+BFS。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 25 typedef struct node_st {
int x, y, t;
node_st() {}
node_st(int xx, int yy, int tt) {
x = xx; y = yy; t = tt;
}
friend bool operator < (node_st a, node_st b) {
return a.t > b.t;
}
} node_st; char map[MAXN][MAXN];
int visit[MAXN][MAXN];
int dir[][] = {-,,,,,-,,};
int n, m, vp, vs, vt;
int sx, sy, ex, ey; bool outRange(int x, int y) {
if (x< || x>=n || y< || y>=m)
return true;
return false;
} int bfs() {
int i, x, y, t;
priority_queue<node_st> que;
node_st node; if (map[ex][ey] == '@')
return -; memset(visit, , sizeof(visit));
map[sx][sy] = '@';
que.push(node_st(sx, sy, )); while (!que.empty()) {
node = que.top();
if (node.x==ex && node.y==ey)
return node.t;
que.pop();
for (i=; i<; ++i) {
x = node.x + dir[i][];
y = node.y + dir[i][];
if (outRange(x, y) || map[x][y]=='@')
continue;
if (map[x][y] == 'T')
t = node.t + vt;
if (map[x][y] == '.')
t = node.t + vs;
if (map[x][y] == '#')
t = node.t + vp;
if (visit[x][y]== || visit[x][y]>t) {
que.push(node_st(x, y, t));
visit[x][y] = t;
}
}
} return -;
} int main() {
int i, t=; while (scanf("%d %d", &n, &m) != EOF) {
scanf("%d%d%d", &vp, &vs, &vt);
for (i=; i<n; ++i)
scanf("%s", map[i]);
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
i = bfs();
printf("Case %d: %d\n", ++t, i);
} return ;
}
上一篇:Linux popen/pclose


下一篇:redis-windows免安装版本安装多个redies