【HDOJ】1885 Key Task

状态压缩+BFS,一次AC。

  1 /* 1885 */
  2 #include <iostream>
  3 #include <queue>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cstdlib>
  7 using namespace std;
  8 
  9 #define MAXN 105
 10 
 11 bool visit[MAXN][MAXN][16];
 12 
 13 typedef struct node_t {
 14     int x, y, t, k;
 15     node_t() {}
 16     node_t(int xx, int yy, int tt, int kk) {
 17         x = xx; y = yy; t = tt; k = kk;
 18     }
 19 } node_t;
 20 
 21 int n, m;
 22 node_t beg;
 23 char map[MAXN][MAXN];
 24 int dir[4][2] = {
 25     -1,0,1,0,0,-1,0,1
 26 };
 27 
 28 inline bool check(int x, int y) {
 29     return x<0 || x>=n || y<0 || y>=m;
 30 }
 31 
 32 int bfs() {
 33     int i, j, k;
 34     int x, y, t;
 35     node_t nd;
 36     queue<node_t> Q;
 37     
 38     memset(visit, false, sizeof(visit));
 39     visit[beg.x][beg.y][beg.k] = true;
 40     Q.push(beg);
 41     
 42     while (!Q.empty()) {
 43         nd = Q.front();
 44         Q.pop();
 45         t = nd.t + 1;
 46         for (i=0; i<4; ++i) {
 47             x = nd.x + dir[i][0];
 48             y = nd.y + dir[i][1];
 49             if (check(x, y) || map[x][y]=='#')
 50                 continue;
 51             if (map[x][y] == 'X')
 52                 return t;
 53             k = nd.k;
 54             if (map[x][y]>='A' && map[x][y]<='D') {
 55                 j = map[x][y] - 'A';
 56                 if ((nd.k & (1<<j)) == 0)
 57                     continue;
 58             } else if (map[x][y]>='a' && map[x][y]<='d') {
 59                 j = map[x][y] - 'a';
 60                 k |= (1<<j);
 61             }
 62             if (visit[x][y][k])
 63                 continue;
 64             visit[x][y][k] = true;
 65             Q.push(node_t(x, y, t, k));
 66         }
 67     }
 68     
 69     return -1;
 70 }
 71 
 72 int main() {
 73     int i, j, k;
 74     
 75     #ifndef ONLINE_JUDGE
 76         freopen("data.in", "r", stdin);
 77         freopen("data.out", "w", stdout);
 78     #endif
 79     
 80     beg.k = beg.t = 0;
 81     while (scanf("%d%d",&n,&m)!=EOF && (n||m)) {
 82         for (i=0; i<n; ++i) {
 83             scanf("%s", map[i]);
 84             for (j=0; j<m; ++j) {
 85                 if (map[i][j] == '*') {
 86                     beg.x = i;
 87                     beg.y = j;
 88                 } else if (map[i][j] == 'Y') {
 89                     map[i][j] = 'A';
 90                 } else if (map[i][j] == 'R') {
 91                     map[i][j] = 'C';
 92                 } else if (map[i][j] == 'G') {
 93                     map[i][j] = 'D';
 94                 } else if (map[i][j] == 'y') {
 95                     map[i][j] = 'a';
 96                 } else if (map[i][j] == 'r') {
 97                     map[i][j] = 'c';
 98                 } else if (map[i][j] == 'g') {
 99                     map[i][j] = 'd';
100                 } 
101             }
102         }
103         k = bfs();
104         if (k < 0)
105             puts("The poor student is trapped!");
106         else
107             printf("Escape possible in %d steps.\n", k);
108     }
109     
110     return 0;
111 }

 

posted on 2015-02-23 20:50  Bombe  阅读(171)  评论(0编辑  收藏  举报

导航