【HDOJ】2416 Treasure of the Chimp Island

时间:2023-03-09 13:07:10
【HDOJ】2416 Treasure of the Chimp Island

bfs()。题目的数据乱码。应该如下:

*****#*********
*.......$...*
*..***.......*
*....*****..*
*....******37A
*****......*
*.....******..*
***CA********** *****
*$**
*.**
***#* --
 #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; #define isUpper(ch) (ch>='A' && ch<='Z')
#define isLower(ch) (ch>='a' && ch<='z')
#define isDigit(ch) (ch>='0' && ch<='9')
#define INF 0x7f7f7f7f
#define MAXN 105 typedef struct node_t {
int x, y, z, t;
node_t() {}
node_t(int xx,int yy, int zz, int tt) {
x = xx; y = yy; z = zz; t = tt;
}
friend bool operator < (const node_t a, const node_t b) {
return a.t > b.t;
}
} node_t; int visit[MAXN][MAXN][];
char map[MAXN][MAXN];
int m, n;
int dir[][] = {-,,,,,-,,}; bool check(int x, int y) {
return x<||x>=m||y<||y>=n;
} int bfs() {
int x, y, z, t;
int i, j;
priority_queue<node_t> Q; memset(visit, 0x7f, sizeof(visit));
for (i=; i<m; ++i) {
for (j=; j<n; ++j) {
if (map[i][j]=='#') {
Q.push(node_t(i, j, , ));
visit[i][j][] = ;
} else if (isUpper(map[i][j])) {
Q.push(node_t(i, j, map[i][j]-'A'+, ));
visit[i][j][map[i][j]-'A'+] = ;
}
}
} while (!Q.empty()) {
node_t nd = Q.top(); if (map[nd.x][nd.y] == '$')
return nd.t; Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y) || map[x][y]=='*')
continue;
if (map[x][y] == '.') {
if (nd.t < visit[x][y][nd.z]) {
visit[x][y][nd.z] = nd.t;
Q.push(node_t(x, y, nd.z, nd.t));
}
} else if (map[x][y] == '$') {
Q.push(node_t(x, y, nd.z, nd.t));
} else if (isDigit(map[x][y])) {
if (nd.z > && nd.t < visit[x][y][nd.z-]) {
visit[x][y][nd.z-] = nd.t;
Q.push(node_t(x, y, nd.z-, nd.t));
}
t = nd.t+map[x][y]-'';
if (t < visit[x][y][nd.z]) {
visit[x][y][nd.z] = t;
Q.push(node_t(x, y, nd.z, t));
}
}
}
} return -;
} int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif int i = , tmp; while (gets(map[i++]) != NULL) {
if (map[][] == '-')
break;
while (gets(map[i])!=NULL && strlen(map[i])!=)
++i;
m = i;
n = strlen(map[]);
tmp = bfs();
if (tmp == -)
printf("IMPOSSIBLE\n");
else
printf("%d\n", tmp);
i = ;
} return ;
}