kuangbin专题一 简单搜索
弱菜做了好久23333333........
传送门: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#overview
A 只能摆k个棋子,只能摆在#
1 #include <cstring> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 const int max_n = 10; 6 char maze[max_n][max_n]; 7 bool vis[max_n]; 8 int n, m, ans, cnt; 9 void init(){ 10 memset(maze, 0, sizeof(maze)); 11 ans = cnt = 0; 12 } 13 void dfs(int k){ 14 if(cnt == m){ ++ans; return ; } 15 if(k > n) return ; 16 for(int i = 0; i < n; ++i){ 17 if(maze[i][k] == '#' && !vis[i]){ 18 vis[i] = 1; ++cnt; 19 dfs(k+1); 20 vis[i] = 0; --cnt; 21 } 22 } 23 dfs(k+1); 24 } 25 int main(){ 26 ios::sync_with_stdio(false); 27 while(cin >> n >> m){ 28 if(n == m && n == -1) break; 29 init(); 30 for(int i = 0; i < n; ++i){ 31 cin.get(); 32 for(int j = 0; j < n; ++j) cin >> maze[i][j]; 33 } 34 dfs(0); 35 cout << ans << endl; 36 } 37 return 0; 38 }
B 三维BFS
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 const int max_n = 40; 8 const int cx[] = {0, 1, 0, -1, 0, 0}; 9 const int cy[] = {1, 0, -1, 0, 0, 0}; 10 const int cz[] = {0, 0, 0, 0, 1, -1}; 11 int maze[max_n][max_n][max_n]; 12 bool vis[max_n][max_n][max_n]; 13 struct point{ 14 int x, y, z, step; 15 }; 16 int L, R, C; 17 int ex, ey, ez; 18 int main(){ 19 ios::sync_with_stdio(false); 20 while(cin >> L >> R >> C){ 21 if(L == 0 && R == 0 && C == 0) break; 22 queue<point> q; 23 memset(vis, 0, sizeof(vis)); 24 memset(maze, 0, sizeof(maze)); 25 for(int k = 0; k < L; ++k){ 26 for(int i = 0; i < R; ++i){ 27 cin.get(); 28 for(int j = 0; j < C; ++j){ 29 char ch = cin.get(); 30 if(ch == 'S'){ 31 maze[k][i][j] = 1; 32 point p = {k, i, j, 0}; 33 q.push(p); 34 } 35 else if(ch == '.') maze[k][i][j] = 1; 36 else if(ch == 'E'){ 37 maze[k][i][j] = 1; 38 ex = k; ey = i; ez = j; 39 } 40 } 41 } 42 cin.get(); 43 } 44 int x, y, z, step; 45 while(!q.empty()){ 46 point p = q.front(); 47 q.pop(); 48 x = p.x, y = p.y, z = p.z, step = p.step; 49 if(x == ex && y == ey && z == ez) break; 50 for(int i = 0; i < 6; ++i){ 51 int nx = x + cx[i], ny = y + cy[i], nz = z + cz[i]; 52 if(nx<0||L<nx||ny<0||R<ny||nz<0||C<nz) continue; 53 if(maze[nx][ny][nz]){ 54 maze[nx][ny][nz] = 0; 55 point tp = {nx, ny, nz, step + 1}; 56 q.push(tp); 57 } 58 } 59 } 60 if(x == ex && y == ey && z == ez) printf("Escaped in %d minute(s).\n", step); 61 else printf("Trapped!\n"); 62 } 63 return 0; 64 }
C 搜......
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 struct point{ 8 int x, step; 9 }; 10 int S, E; 11 int vis[100010]; 12 int main(){ 13 ios::sync_with_stdio(false); 14 while(cin >> S >> E){ 15 memset(vis, 0, sizeof(vis)); 16 vis[S] = 1; 17 queue<point> q; 18 q.push(point{S, 0}); 19 int x, step; 20 while(!q.empty()){ 21 point p = q.front(); 22 x = p.x; step = p.step; 23 q.pop(); 24 if(x == E) break; 25 for(int i = 0; i < 3; ++i){ 26 int nx; 27 if(i == 0) nx = x + 1; 28 else if(i == 1) nx = x - 1; 29 else nx = x << 1; 30 if(0 <= nx && nx <= 100000 && !vis[nx]){ q.push(point{nx, step+1}); vis[nx] = 1; } 31 } 32 } 33 cout << step << endl; 34 } 35 return 0; 36 }
D 卡了好久看了题解 第一行确定下来后其他行的也都确定下来了(说的好有道理),所以枚举第一行状态,确定下其他行,看最后一行是否都是0
然后在这道题发现了之前二进制枚举的一个错误....... 正确姿势:(i>>j) & 1(吧?)
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int MAXN = 20; 9 const int INF = 0x3f3f3f3f; 10 const int cx[] = {0, 0, -1, 0}; 11 const int cy[] = {1, -1, 0, 0}; 12 int n, m; 13 int ans[MAXN][MAXN]; 14 int flip[MAXN][MAXN]; 15 int origin[MAXN][MAXN]; 16 17 int check(int x, int y){ 18 int cnt = origin[x][y]; 19 for(int i = 0; i < 4; ++i){ 20 int nx = x + cx[i], ny = y + cy[i]; 21 if(nx >= 0 && nx < n && ny >= 0 && ny < m) 22 cnt += flip[nx][ny]; 23 } 24 return cnt & 1; 25 } 26 27 int duang(){ 28 for(int i = 1; i < n; ++i) 29 for(int j = 0; j < m; ++j) 30 if(check(i-1, j)) flip[i][j] = 1; 31 for(int j = 0; j < m; ++j) 32 if(check(n-1, j)) return 0; 33 int cnt = 0; 34 for(int i = 0; i < n; ++i) 35 for(int j = 0; j < m; ++j) 36 cnt += flip[i][j]; 37 return cnt; 38 } 39 40 int main(){ 41 while(cin >> n >> m){ 42 memset(origin, 0, sizeof(origin)); 43 for(int i = 0; i < n; ++i) 44 for(int j = 0; j < m; ++j) 45 cin >> origin[i][j]; 46 int tans = INF; 47 for(int i = 0; i < (1<<m); ++i){ 48 memset(flip, 0, sizeof(flip)); 49 for(int j = 0; j < m; ++j) 50 flip[0][j] = (i>>j) & 1; 51 int cnt = duang(); 52 if(cnt < tans && cnt != 0){ 53 tans = cnt; 54 memcpy(ans, flip, sizeof(flip)); 55 } 56 } 57 if(tans == INF) cout << "IMPOSSIBLE" << endl; 58 else{ 59 for(int i = 0; i < n; ++i) 60 for(int j = 0; j < m; ++j) 61 cout << ans[i][j] << " \n"[j==m-1]; 62 } 63 } 64 return 0; 65 }
E 听说答案都在long long范围内hhhh.......
1 #include <queue> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 typedef long long ll; 6 int main(){ 7 ios::sync_with_stdio(false); 8 int n; 9 while(cin >> n){ 10 if(n == 0) break; 11 queue<ll> q; 12 q.push(1LL); 13 while(!q.empty()){ 14 ll k = q.front(); 15 q.pop(); 16 if(k % n == 0){ 17 cout << k << endl; 18 break; 19 } 20 q.push(k*10); 21 q.push(k*10+1); 22 } 23 } 24 return 0; 25 }
F GOJ有这道题........
1 #include <map> 2 #include <set> 3 #include <queue> 4 #include <deque> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <cstdio> 9 #include <cstdlib> 10 #include <cstring> 11 #include <utility> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 int m, n; 16 bool isPrime[10010]; 17 int vis[10010]; 18 void preprocess(){ 19 memset(isPrime, true, sizeof(isPrime)); 20 isPrime[0] = isPrime[1] = false; 21 for(int i = 2; i < 10000; ++i){ 22 if(isPrime[i]){ 23 for(int j = i; i*j < 10000; ++j){ 24 isPrime[i*j] = false; 25 } 26 } 27 } 28 } 29 void bfs(){ 30 memset(vis, 0, sizeof(vis)); 31 vis[m] = 1; 32 queue<int> q; 33 q.push(m); 34 while(!q.empty()){ 35 int k = q.front(); 36 q.pop(); 37 int d[4] = {k/1000, k%1000/100, k%100/10, k%10}; 38 int b[4] = {1000, 100, 10, 1}; 39 for(int i = 0; i < 4; ++i){ 40 for(int j = 0; j < 10; ++j){ 41 int t = k - d[i] * b[i] + j * b[i]; 42 if(k == n) return ; 43 if(t > 999 && isPrime[t] && !vis[t]){ 44 vis[t] = vis[k] + 1; 45 q.push(t); 46 } 47 } 48 } 49 } 50 return ; 51 } 52 int main(){ 53 ios::sync_with_stdio(false); 54 preprocess(); 55 int t; 56 cin >> t; 57 while(t--){ 58 cin >> m >> n; 59 bfs(); 60 if(vis[n]) cout << vis[n]-1 << endl; 61 else cout << "Impossible" << endl; 62 } 63 return 0; 64 }
G 水 直接模拟 略坑是第一张都是最底的一张
1 #include <set> 2 #include <string> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 8 int main(){ 9 int t; 10 cin >> t; 11 for(int x = 1; x <= t; ++x){ 12 int l; 13 cin >> l; 14 string str1, str2, desstr; 15 cin >> str1 >> str2 >> desstr; 16 int step = 0; 17 set<string> s; 18 s.insert(str1+str2); 19 bool flag = true; 20 while(flag){ 21 string tmpstr; 22 for(int i = 0; i < l; ++i){ 23 tmpstr += str2[i]; 24 tmpstr += str1[i]; 25 } 26 //cout << tmpstr << endl; 27 ++step; 28 if(tmpstr == desstr) break; 29 if(s.count(tmpstr)){ 30 flag = false; 31 break; 32 } 33 s.insert(tmpstr); 34 str1 = tmpstr.substr(0, l); 35 str2 = tmpstr.substr(l, 2*l); 36 //cout << str1 << " " << str2 << endl; 37 } 38 cout << x << ' ' << (flag ? step : -1) << endl; 39 } 40 return 0; 41 }
H M题写吐了H不想做.......
还是搜 6种状态
I 暴力找两个#搜.......
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int INF = 0x3f3f3f; 9 const int cx[4] = {0, 0, 1, -1}; 10 const int cy[4] = {1, -1, 0, 0}; 11 int n, m; 12 int dis[15][15]; 13 char map[15][15]; 14 15 struct Point{ 16 int x, y; 17 Point(){} 18 Point(int xx, int yy) :x(xx), y(yy){} 19 }; 20 queue<Point> q; 21 int bfs(int x1, int y1, int x2, int y2){ 22 memset(dis, INF, sizeof(dis)); 23 q.push(Point(x1, y1)); 24 q.push(Point(x2, y2)); 25 dis[x1][y1] = 0; 26 dis[x2][y2] = 0; 27 while (!q.empty()){ 28 Point p = q.front(); 29 q.pop(); 30 for (int i = 0; i < 4; i++){ 31 int xx = p.x + cx[i], yy = p.y + cy[i]; 32 if (xx >= 0 && xx < n && yy >= 0 && yy<m && map[xx][yy] == '#' && dis[xx][yy] > dis[p.x][p.y] + 1){ 33 dis[xx][yy] = dis[p.x][p.y] + 1; 34 q.push(Point(xx, yy)); 35 } 36 } 37 } 38 39 int ret = 0; 40 for (int i = 0; i < n; i++) 41 for (int j = 0; j < m; j++) 42 if (map[i][j] == '#') 43 ret = max(ret, dis[i][j]); 44 return ret; 45 } 46 47 int main(){ 48 int t; 49 cin >> t; 50 for (int x = 1; x <= t; x++){ 51 while(!q.empty()) q.pop(); 52 cin >> n >> m; 53 for (int i = 0; i < n; i++) 54 cin >> map[i]; 55 int ans = INF; 56 for (int i = 0; i < n; i++) 57 for (int j = 0; j < m; j++) 58 if (map[i][j] == '#') 59 for (int ii = 0; ii < n; ii++) 60 for (int jj = 0; jj < m; jj++) 61 if (map[ii][jj] == '#'){ 62 int temp = bfs(i, j, ii, jj); 63 ans = min(ans, temp); 64 } 65 if (ans == INF) ans = -1; 66 cout << "Case " << x << ": " << ans << endl; 67 } 68 return 0; 69 }
J 多处火.....先处理火,再处理人,遇到可行的情况退出来
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 struct pos{ 5 int x, y; 6 pos(int xx = 0, int yy = 0): x(xx), y(yy) {} 7 }; 8 const int MAXN = 1111; 9 const int INF = 0x7f7f7f7f; 10 const int cx[] = {0, 0, 1, -1}; 11 const int cy[] = {1, -1, 0, 0}; 12 int n, m; 13 int jx, jy, fx, fy; 14 int maze[MAXN][MAXN]; 15 int jstep[MAXN][MAXN]; 16 int fstep[MAXN][MAXN]; 17 18 int main(){ 19 int t; 20 cin >> t; 21 while(t--){ 22 cin >> n >> m; 23 queue<pos> qf; 24 memset(fstep, -1, sizeof(fstep)); 25 for(int i = 0; i < n; ++i){ 26 cin.get(); 27 for(int j = 0; j < m; ++j){ 28 char ch; 29 cin >> ch; 30 if(ch == '#') maze[i][j] = 0; 31 else{ 32 maze[i][j] = 1; 33 if(ch == 'J'){ 34 jx = i; 35 jy = j; 36 } 37 else if(ch == 'F'){ 38 fx = i; 39 fy = j; 40 qf.push(pos(fx, fy)); 41 fstep[fx][fy] = 0; 42 } 43 } 44 } 45 } 46 while(!qf.empty()){ 47 pos p = qf.front(); 48 int x = p.x, y = p.y; 49 qf.pop(); 50 for(int i = 0; i < 4; ++i){ 51 int nx = x + cx[i], ny = y + cy[i]; 52 if(nx < 0 || n <= nx || ny < 0 || m <= ny || maze[nx][ny] == 0 || fstep[nx][ny] != -1) continue; 53 qf.push(pos(nx, ny)); 54 fstep[nx][ny] = fstep[x][y] + 1; 55 } 56 } 57 // cout << endl; 58 // for(int i = 0; i < n; ++i){ 59 // for(int j = 0; j < m; ++j) 60 // cout << fstep[i][j] << '\t'; 61 // cout << endl; 62 // } 63 memset(jstep, -1, sizeof(jstep)); 64 queue<pos> qj; 65 qj.push(pos(jx, jy)); 66 jstep[jx][jy] = 0; 67 int ans = -1; 68 while(!qj.empty()){ 69 pos p = qj.front(); 70 int x = p.x, y = p.y; 71 qj.pop(); 72 if(x == 0 || x == n-1 || y == 0 || y == m-1){ 73 ans = jstep[x][y]; 74 break; 75 } 76 for(int i = 0; i < 4; ++i){ 77 int nx = x + cx[i], ny = y + cy[i]; 78 if(nx < 0 || n <= nx || ny < 0 || m <= ny || maze[nx][ny] == 0 || jstep[nx][ny] != -1 || (fstep[nx][ny] != -1 && fstep[nx][ny] <= jstep[x][y] + 1)) continue; 79 qj.push(pos(nx, ny)); 80 jstep[nx][ny] = jstep[x][y] + 1; 81 } 82 } 83 // cout << endl; 84 // for(int i = 0; i < n; ++i){ 85 // for(int j = 0; j < m; ++j) 86 // cout << jstep[i][j] << '\t'; 87 // cout << endl; 88 // } 89 if(ans == -1) cout << "IMPOSSIBLE" << endl; 90 else cout << ans+1 << endl; 91 } 92 return 0; 93 }
K 搜 开数组记录上一个点的坐标
1 #include <map> 2 #include <set> 3 #include <queue> 4 #include <deque> 5 #include <cmath> 6 #include <stack> 7 #include <string> 8 #include <vector> 9 #include <cstdio> 10 #include <cstdlib> 11 #include <cstring> 12 #include <utility> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 struct point{ 17 int x, y; 18 }; 19 int maze[5][5]; 20 point state[5][5]; 21 const int cx[] = {0, 0, 1, -1}; 22 const int cy[] = {1, -1, 0, 0}; 23 int main(){ 24 ios::sync_with_stdio(false); 25 memset(state, 0, sizeof(state)); 26 for(int i = 0; i < 5; ++i) 27 for(int j = 0; j < 5; ++j) 28 cin >> maze[i][j]; 29 queue<point> q; 30 q.push({0, 0}); 31 maze[0][0] = 1; 32 bool flag = false; 33 while(!q.empty()){ 34 point k = q.front(); 35 q.pop(); 36 for(int i = 0; i < 4; ++i){ 37 int x = k.x + cx[i], y = k.y + cy[i]; 38 if(x < 0 || x > 4 || y < 0 || y > 4 || maze[x][y]) continue; 39 q.push({x, y}); 40 maze[x][y] = 1; 41 state[x][y] = k; 42 if(x == 4 && y == 4){ 43 flag = true; 44 break; 45 } 46 } 47 if(flag) break; 48 } 49 point k = {4, 4}; 50 stack<point> s; 51 while(!(k.x == 0 && k.y == 0)){ 52 s.push(k); 53 k = state[k.x][k.y]; 54 } 55 s.push(point{0, 0}); 56 while(!s.empty()){ 57 cout << "(" << s.top().x << ", " << s.top().y << ")" << endl; 58 s.pop(); 59 } 60 return 0; 61 }
L 数水坑类的 8个方向
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 int ans; 6 char maze[120][120]; 7 const int cx[] = {0, 0, 1, 1, 1, -1, -1, -1}; 8 const int cy[] = {1, -1, 1, -1, 0, 1, 0, -1}; 9 void dfs(int x, int y){ 10 maze[x][y] = '*'; 11 for(int i = 0; i < 8; ++i){ 12 int nx = x + cx[i], ny = y + cy[i]; 13 if(maze[nx][ny] == '@') dfs(nx, ny); 14 } 15 } 16 int main(){ 17 ios::sync_with_stdio(false); 18 int m, n; 19 while(cin >> m >> n){ 20 if(m == 0 && n == 0) break; 21 ans = 0; 22 for(int i = 0; i < m; ++i){ 23 cin.get(); 24 for(int j = 0; j < n; ++j) 25 cin >> maze[i][j]; 26 } 27 for(int i = 0; i < m; ++i){ 28 for(int j = 0; j < n; ++j){ 29 if(maze[i][j] == '@'){ 30 ++ans; 31 dfs(i, j); 32 } 33 } 34 } 35 cout << ans << endl; 36 } 37 return 0; 38 }
M 可乐.....改了好久蜜汁哇 果断重写 简直恶心
1 #include <bits/stdc++.h> 2 using namespace std; 3 //#define print() cout<<ts<<" "<<tn<<" "<<tm<<endl; 4 5 struct state{ 6 int s, n, m; 7 state(int ss = 0, int nn = 0, int mm = 0): s(ss), n(nn), m(mm) {} 8 }; 9 int s, n, m; 10 queue<state> q; 11 const int MAXN = 111; 12 int step[MAXN][MAXN][MAXN]; 13 14 int main(){ 15 while(cin >> s >> n >> m){ 16 if(s == 0 && n == 0 && m == 0) break; 17 if(s & 1) cout << "NO" << endl; 18 else{ 19 while(!q.empty()) q.pop(); 20 memset(step, -1, sizeof(step)); 21 int ans = -1; 22 q.push(state(s, 0, 0)); 23 step[s][0][0] = 0; 24 while(!q.empty()){ 25 state p = q.front(); 26 q.pop(); 27 int ss = p.s, nn = p.n, mm = p.m; 28 if((ss == s/2 && nn == s/2) || (ss == s/2 && mm == s/2) || (nn == s/2 && mm == s/2)){ 29 ans = step[ss][nn][mm]; 30 break; 31 } 32 state tmp; 33 //s -> n || s -> m 34 if(ss){ 35 //s -> n 36 if(ss > n - nn){ 37 tmp.s = ss - (n - nn); 38 tmp.n = n; 39 tmp.m = mm; 40 } 41 else{ 42 tmp.s = 0; 43 tmp.n = nn + ss; 44 tmp.m = mm; 45 } 46 if(step[tmp.s][tmp.n][tmp.m] == -1){ 47 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 48 q.push(tmp); 49 } 50 //ss -> m 51 if(ss > m - mm){ 52 tmp.s = ss - (m - mm); 53 tmp.m = m; 54 tmp.n = nn; 55 } 56 else{ 57 tmp.s = 0; 58 tmp.m = mm + ss; 59 tmp.n = nn; 60 } 61 if(step[tmp.s][tmp.n][tmp.m] == -1){ 62 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 63 q.push(tmp); 64 } 65 } 66 //n -> s || n -> m 67 if(nn){ 68 //n -> s 69 if(nn > s - ss){ 70 tmp.n = nn - (s - ss); 71 tmp.s = s; 72 tmp.m = mm; 73 } 74 else{ 75 tmp.n = 0; 76 tmp.s = ss + nn; 77 tmp.m = mm; 78 } 79 if(step[tmp.s][tmp.n][tmp.m] == -1){ 80 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 81 q.push(tmp); 82 } 83 //n -> m 84 if(nn > m - mm){ 85 tmp.n = nn - (m - mm); 86 tmp.m = m; 87 tmp.s = ss; 88 } 89 else{ 90 tmp.n = 0; 91 tmp.m = mm + nn; 92 tmp.s = ss; 93 } 94 if(step[tmp.s][tmp.n][tmp.m] == -1){ 95 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 96 q.push(tmp); 97 } 98 } 99 //m -> n || m -> s 100 if(mm){ 101 //m -> n 102 if(mm > n - nn){ 103 tmp.m = mm - (n - nn); 104 tmp.n = n; 105 tmp.s = ss; 106 } 107 else{ 108 tmp.m = 0; 109 tmp.n = nn + mm; 110 tmp.s = ss; 111 } 112 if(step[tmp.s][tmp.n][tmp.m] == -1){ 113 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 114 q.push(tmp); 115 } 116 //m -> s 117 if(mm > s - ss){ 118 tmp.m = mm - (s - ss); 119 tmp.s = s; 120 tmp.n = nn; 121 } 122 else{ 123 tmp.m = 0; 124 tmp.s = mm + ss; 125 tmp.n = nn; 126 } 127 if(step[tmp.s][tmp.n][tmp.m] == -1){ 128 step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + 1; 129 q.push(tmp); 130 } 131 } 132 } 133 if(ans == -1) cout << "NO" << endl; 134 else cout << ans << endl; 135 } 136 } 137 return 0; 138 }
N 从两个人的位置开始bfs整个图
1 #include <queue> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <vector> 6 #include <iostream> 7 #include <algorithm> 8 using namespace std; 9 10 struct pos{ 11 int x, y; 12 pos(int xx, int yy): x(xx), y(yy){}; 13 }; 14 const int MAXN = 222; 15 char maze[MAXN][MAXN]; 16 const int cx[] = {1, -1, 0, 0}; 17 const int cy[] = {0, 0, 1, -1}; 18 vector<pos> kfc; 19 queue<pos> qy; int yx, yy, stepy[MAXN][MAXN]; 20 queue<pos> qm; int mx, my, stepm[MAXN][MAXN]; 21 22 int main(){ 23 int n, m; 24 while(cin >> n >> m){ 25 kfc.clear(); 26 for(int i = 0; i < n; ++i){ 27 cin.get(); 28 for(int j = 0; j < m; ++j){ 29 char ch; 30 cin >> ch; 31 if(ch == '#') maze[i][j] = ch; 32 else{ 33 if(ch == 'Y'){ 34 yx = i; 35 yy = j; 36 } 37 if(ch == 'M'){ 38 mx = i; 39 my = j; 40 } 41 if(ch == '@') 42 kfc.push_back(pos(i, j)); 43 maze[i][j] = '.'; 44 } 45 } 46 } 47 while(!qy.empty()) qy.pop(); 48 while(!qm.empty()) qm.pop(); 49 memset(stepy, -1, sizeof(stepy)); 50 memset(stepm, -1, sizeof(stepm)); 51 qy.push(pos(yx, yy)); 52 stepy[yx][yy] = 0; 53 while(!qy.empty()){ 54 int x = qy.front().x, y = qy.front().y; 55 qy.pop(); 56 for(int i = 0; i < 4; ++i){ 57 int nx = x + cx[i], ny = y + cy[i]; 58 if(nx < 0 || n < nx || ny < 0 || m < ny || maze[nx][ny] == '#' || stepy[nx][ny] != -1) continue; 59 stepy[nx][ny] = stepy[x][y] + 1; 60 qy.push(pos(nx, ny)); 61 } 62 } 63 qm.push(pos(mx, my)); 64 stepm[mx][my] = 0; 65 while(!qm.empty()){ 66 int x = qm.front().x, y = qm.front().y; 67 qm.pop(); 68 for(int i = 0; i < 4; ++i){ 69 int nx = x + cx[i], ny = y + cy[i]; 70 if(nx < 0 || n < nx || ny < 0 || m < ny || maze[nx][ny] == '#' || stepm[nx][ny] != -1) continue; 71 stepm[nx][ny] = stepm[x][y] + 1; 72 qm.push(pos(nx, ny)); 73 } 74 } 75 int ans = 0x7f7f7f7f; 76 for(vector<pos>::iterator i = kfc.begin(); i != kfc.end(); ++i){ 77 int x = (*i).x, y = (*i).y; 78 ans = min(ans, stepy[x][y] + stepm[x][y]); 79 } 80 cout << ans*11 << endl; 81 } 82 return 0; 83 }
.............................................................................