【HDOJ】2822 Dogs

时间:2021-06-23 15:06:44

bfs.

 /* 2822 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 1005
#define INF 0xffffff typedef struct node_t {
int d, x, y;
node_t() {}
node_t(int xx, int yy, int dd) {
x = xx; y = yy; d = dd;
}
friend bool operator <(node_t a, node_t b) {
return a.d > b.d;
}
} node_t; int visit[MAXN][MAXN];
char map[MAXN][MAXN];
int n, m;
int bx, by, ex, ey;
int dir[][] = {
-,,,,,,,-
}; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} int bfs() {
int x = bx, y = by, d = ;
int i, j, k;
priority_queue<node_t> Q;
node_t nd; Q.push(node_t(x, y, d));
visit[x][y] = ; while (!Q.empty()) {
nd = Q.top();
if (nd.x==ex && nd.y==ey)
return nd.d;
Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y))
continue;
if (map[x][y] == 'X')
d = nd.d;
else
d = nd.d + ;
if (d < visit[x][y]) {
visit[x][y] = d;
Q.push(node_t(x, y, d));
}
}
} return ;
} int main() {
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j)
visit[i][j] = INF;
}
scanf("%d%d%d%d",&bx,&by,&ex,&ey);
--bx; --by; --ex; --ey;
k = bfs();
printf("%d\n", k);
} return ;
}