Shirlies
宁静专注认真的程序媛~

广搜

要记录方格里面不同状态时行走的步数

注意当当前行走的步数大于记录的步数时要剪枝(当然,在记录方格不同状态时的值不为-1时)

代码如下:
  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 struct node
  7 {
  8     int x,y;
  9     int step;
 10     int state;
 11 };
 12 char map[105][105];
 13 int R,C;
 14 int sx,sy;
 15 int used[105][105][20];
 16 //记录方向
 17 int dirx[4] = {-1,0,1,0};
 18 int diry[4] = {0,-1,0,1};
 19 //记录颜色
 20 char colorB[4] = {'B','Y','R','G'};
 21 char colorS[4] = {'b','y','r','g'};
 22 
 23 int bfs()
 24 {
 25     memset(used,-1,sizeof(used));//习惯用-1表示该处未用过
 26     node Start;
 27     Start.x = sx;
 28     Start.y = sy;
 29     Start.state = Start.step = 0;
 30     queue<node> q;
 31     q.push(Start);
 32     while(!q.empty())
 33     {
 34         node temp = q.front();
 35         q.pop();
 36 
 37         if(map[temp.x][temp.y] == 'X')
 38             return temp.step;
 39 
 40         for(int i = 0;i < 4;i ++)
 41         {
 42             int rx = temp.x + dirx[i];
 43             int ry = temp.y + diry[i];
 44 
 45             if(rx < 0 || rx >= R || ry < 0 || ry >= C || map[rx][ry] == '#')
 46                 continue;
 47 
 48             node tmp;
 49             tmp.x = rx;
 50             tmp.y = ry;
 51             tmp.state = temp.state;//状态
 52             tmp.step = temp.step + 1;
 53             
 54             if(used[rx][ry][tmp.state] != -1 && tmp.step >= used[rx][ry][tmp.state])//注意
 55                 continue;
 56 
 57             used[rx][ry][tmp.state] = tmp.step;
 58 
 59             int f1 = 1;
 60             for(int j = 0;j < 4;j ++)
 61             {
 62                 if(colorB[j] == map[rx][ry])
 63                 {
 64                     if(tmp.state & (1 << j))
 65                         q.push(tmp);
 66 
 67                     f1 = 0;
 68                     break;
 69                 }
 70             }
 71 
 72             int f2 = 1;
 73             for(int j = 0;f1 && j < 4;j ++)
 74             {
 75                 if(colorS[j] == map[rx][ry])
 76                 {
 77                     if((tmp.state & (1 << j)) == 0)
 78                     {
 79                         tmp.state += (1 << j);
 80                     }
 81                     q.push(tmp);
 82                     f2 = 0;
 83                     break;
 84                 }
 85             }
 86             if(f1 && f2)//当既不是钥匙也不是门时
 87             {
 88                 q.push(tmp);
 89             }
 90         }
 91     }
 92 
 93     return -1;
 94 }
 95 
 96 int main()
 97 {
 98     while(cin >> R >> C,R||C)
 99     {
100         for(int i = 0;i < R;i ++)
101         {
102             for(int j = 0;j < C;j ++)
103             {
104                 cin >> map[i][j];
105                 if(map[i][j] == '*')
106                 {
107                     sx = i;
108                     sy = j;
109                 }
110             }
111         }
112 
113         int ans = bfs();
114         if(ans == -1)
115         {
116             cout << "The poor student is trapped!" << endl;
117         }
118         else
119         {
120             cout << "Escape possible in " << ans << " steps." << endl;
121         }
122     }
123 
124     return 0;
125 }
posted on 2012-08-05 00:01  Shirlies  阅读(225)  评论(0编辑  收藏  举报