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
| #include<cstdio> #include<queue> #include<cstring> using namespace std;
typedef struct point { int x, y, key, step; point() {} point(int _x, int _y, int _key, int _step) : x(_x), y(_y), key(_key), step(_step) { } } P;
int n, m; char maze[107][107]; bool vis[107][107][1050]; const int dx[4] = {0, 0, -1, 1}; const int dy[4] = {-1, 1, 0, 0};
int main() { memset(vis, false, sizeof(vis)); scanf("%d%d", &n, &m); getchar(); char ch; int startX, startY; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { ch = getchar(); maze[i][j] = ch; if(ch == '2') { startX = i; startY = j; } } getchar(); } queue<P> que; que.push(P(startX, startY, 0, 0)); int curX, curY; P p; while(!que.empty()) { p = que.front(); que.pop(); if(maze[p.x][p.y] == '3') { printf("%d\n", p.step); break; } for(int i=0; i<4; i++) { curX = p.x + dx[i]; curY = p.y + dy[i]; if(curX < 0 || curX>=n || curY < 0 || curY >= m || maze[curX][curY] == '0' || vis[curX][curY][p.key]) continue; ch = maze[curX][curY]; if(ch >= 'A' && ch <= 'Z') { if( p.key & (1 << (ch-'A') ) ) { que.push(P(curX, curY, p.key, p.step+1)); vis[curX][curY][p.key] = true; } } else if(ch >= 'a' && ch <= 'z') { que.push( P(curX, curY, p.key | ( 1 << (ch-'a') ), p.step+1) ); vis[curX][curY][p.key] = true; } else { que.push(P(curX, curY, p.key, p.step+1)); vis[curX][curY][p.key] = true; } } } return 0; }
|