4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; const int maxn = 205; const int INF = 0x3f3f3f3f; int n,m,sx,sy,ex,ey; char maze[maxn][maxn]; int vis[maxn][maxn]; int time[maxn][maxn]; int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}}; struct Node { int x,y,t; }pre,cur; void bfs(int x,int y) { memset(vis,0,sizeof(vis)); queue <Node> que; while(!que.empty()) que.pop(); pre.x = x; pre.y = y; pre.t = 0; vis[x][y] = 1; que.push(pre); while(!que.empty()) { pre = que.front(); que.pop(); if(maze[pre.x][pre.y] == '@') time[pre.x][pre.y] += pre.t; for(int i = 0; i < 4; i++) { int xx = pre.x + dir[i][0]; int yy = pre.y + dir[i][1]; if(xx < 0 || xx >= n || yy < 0 || yy >= m) continue; if(!vis[xx][yy] && maze[xx][yy] != '#') { vis[xx][yy] = 1; cur.x = xx; cur.y = yy; cur.t = pre.t+11; que.push(cur); } } } } int main() { while(scanf("%d%d", &n, &m) != EOF) { memset(time,0,sizeof(time)); for(int i = 0; i < n; i++) scanf("%s", maze[i]); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(maze[i][j] == 'Y') sx = i, sy = j; else if(maze[i][j] == 'M') ex = i, ey = j; bfs(sx,sy); bfs(ex,ey); int Min = INF; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(time[i][j] && time[i][j] < Min) Min = time[i][j]; printf("%d\n", Min); } return 0; }