1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include <bits/stdc++.h> using namespace std;
int n,m; int cx,cy; int fx,fy; int c[1010][1010]; int vis1[1010][1010]; int vis2[1010][1010]; int des[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
struct p { int x; int y; };
bool check(int x,int y){ if(x >= 1 && x <= n && y >= 1 && y <= m && (c[x][y] == '.'''c[x][y] == '@')){ return 1 ; } return 0; }
queue<p>q1;
void bfs1(){ while(!q1.empty()){ p pp = q1.front(); q1.pop(); p pz; for(int i = 1; i <= 4 ; i++){ pz.x = pp.x + des[i][0] ; pz.y = pp.y + des[i][1] ; if(check(pz.x,pz.y)){ if(vis1[pz.x][pz.y] == 0){ vis1[pz.x][pz.y] = vis1[pp.x][pp.y] + 1; q1.push(pz) ; } } } } }
queue<p>q2;
void bfs2(){ while(!q2.empty()){ p pp = q2.front(); q2.pop(); p pz; for(int i = 1; i <= 4 ; i++){ pz.x = pp.x + des[i][0] ; pz.y = pp.y + des[i][1] ; if(check(pz.x,pz.y)){ if(vis2[pz.x][pz.y] == 0){ vis2[pz.x][pz.y] = vis2[pp.x][pp.y] + 1; q2.push(pz) ; } } } } }
struct person { int x; int y; int cnt; };
int main(){ while(scanf("%d %d",&n,&m) != EOF){ getchar(); memset(vis1,0,sizeof vis1); memset(vis2,0,sizeof vis2);
for(int i = 1; i <= n ; i++){ for(int j = 1 ; j <= m ; j++){ scanf("%c",&c[i][j]); if(c[i][j] == 'Y'){ c[i][j] = '.'; vis1[i][j] = 1; p pp; pp.x = i; pp.y = j; q1.push(pp); } if(c[i][j] == 'M'){ c[i][j] = '.'; vis2[i][j] = 1; p pp; pp.x = i; pp.y = j; q2.push(pp); } } getchar(); }
bfs1(); bfs2();
int maxx = 10000; for(int i = 1; i <= n ; i++){ for(int j = 1; j <= m ; j++){ if(c[i][j] == '@' && vis1[i][j] != 0 && vis2[i][j] != 0){ maxx = min(maxx,vis1[i][j] + vis2[i][j] -2); } } } printf("%d\n",maxx*11); } }
|