[BFS][NOI题库][poj2157]Maze

 

Maze

 

Time Limit: 2000MS Memory Limit: 65536K

 

 

Description

Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by 'A', 'B', 'C', 'D', 'E' respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door's keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that's three 'a's which denote the keys of 'A' in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.

Input

The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: 'X' (a block of wall, which the explorer cannot enter), '.' (an empty block), 'S' (the start point of Acm), 'G' (the position of treasure), 'A', 'B', 'C', 'D', 'E' (the doors), 'a', 'b', 'c', 'd', 'e' (the keys of the doors). The input is terminated with two 0's. This test case should not be processed.

Output

For each test case, in one line output "YES" if Acm can find the treasure, or "NO" otherwise.

Sample Input

4 4 
S.X. 
a.X. 
..XG 
.... 
3 4 
S.Xa 
.aXB 
b.AG 
0 0

Sample Output

YES 
NO

Source

POJ Monthly,Wang Yijie
 

 
大致题意:给定一张M行N列的图,探险家从起点S出发,探索财宝G。'A' 'B' 'C' 'D' 'E' 为带锁的门,需要收集到整张图上所有对应的钥匙'a' 'b' 'c' 'd' 'e' 才能打开门(例如,A门有3把钥匙在图上,如果要打开A门,他必须先找到所有三把属于A门的钥匙'a')。问探险家能否找到宝藏。
 
分析:从S点出发BFS 更新手上的钥匙,与对应门的钥匙总量进行对比,如果相同即门被打开,将门加入队列。重复进行BFS直到到达G。
 
代码
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <algorithm>
  4 #define MAX 25
  5 using namespace std;
  6 
  7 struct dui{
  8     int r, c; // hang lie
  9 }queue[ MAX * MAX ];
 10 
 11 struct men{
 12     int key_sum, key_num;
 13 }door[ 6 ];
 14 
 15 int m, n;
 16 char map[ MAX ][ MAX ];
 17 bool vis[ MAX ][ MAX ];
 18 bool vis_door[ MAX ][ MAX ];
 19 
 20 int wx[ 5 ] = { 0, -1, 0, 1, 0};
 21 int wy[ 5 ] = { 0 ,0, 1, 0, -1};
 22 
 23 bool able( int x, int y )
 24 {
 25     if( x > 0 && x <= m && y > 0 && y <= n)
 26         return true;
 27     else return false;
 28 }
 29 
 30 int main()
 31 {
 32     int i, j;
 33     int r, c;
 34     char tmp, tt;
 35     while( true ){
 36         scanf("%d%d", &m ,&n);
 37         if( m == 0 && n == 0 ) break;
 38         memset( vis, 0, sizeof( vis ));
 39         memset( door, 0, sizeof( door ));
 40         memset( vis_door, 0, sizeof( vis_door ));
 41         for( i = 1; i <= m; i++ ){
 42             scanf("%s", map[ i ] + 1);
 43             for( j = 1; j <= n; j++ ){
 44                 if( map[ i ][ j ] >= 'a' && map[ i ][ j ] <= 'e'){
 45                     door[ map[ i ][ j ] - 'a' + 1 ].key_sum ++;
 46                 }
 47                 if( map[ i ][ j ] == 'S' ){
 48                     queue[ 0 ].r = i;
 49                     queue[ 0 ].c = j;
 50                     vis[ i ][ j ] = 1;
 51                 }
 52             }
 53         }
 54         
 55         int head = 0, tail = 1;
 56         bool finded = false;
 57         bool newdoor = true;
 58         
 59         while( newdoor ){
 60             while( !finded && head < tail){
 61                 for( i = 1; i <= 4; i++ ){
 62                     r = queue[ head ].r + wx[ i ];
 63                     c = queue[ head ].c + wy[ i ];
 64                     
 65                     if( map[ r ][ c ] == 'G' ){
 66                         finded = true;
 67                         break;
 68                     }
 69 
 70                     if( !vis[ r ][ c ] && able( r, c ) && map[ r ][ c ] != 'X'){
 71                         tmp = map[ r ][ c ];
 72                         
 73                         if( tmp >= 'A' && tmp <= 'E'){
 74                             vis_door[ r ][ c ] = true;
 75                             continue;
 76                         }
 77                         
 78                         if( tmp >= 'a' && tmp <= 'e' ){
 79                             door[ tmp - 'a' + 1 ].key_num++;
 80                         }
 81                         vis[ r ][ c ] = true;
 82                         queue[ tail ].r = r;
 83                         queue[ tail ].c = c;
 84                         tail++;
 85                     }
 86                 }
 87                 head++;
 88             }
 89             if( finded ) break;
 90             
 91             newdoor = false;
 92             
 93             for( r = 1; r <= m; r++ ){
 94                 for( c = 1; c <= n; c++ ){
 95                     tmp = map[ r ][ c ];
 96                     if( tmp >= 'A' && tmp <= 'E' && vis_door[ r ][ c ] && !vis[ r ][ c ]){
 97                         tt = tmp - 'A' + 1;
 98                         if( door[ tt ].key_sum > 0 && door[ tt ].key_sum == door[ tt ].key_num ){
 99                             vis[ r ][ c ] = true;
100                             queue[ tail ].r = r;
101                             queue[ tail ].c = c;
102                             tail++;
103                             newdoor = true;
104                         }
105                     }
106                 }
107             }
108         }
109         
110         if( finded ) printf("YES\n");
111         else printf("NO\n");
112     }
113     
114     return 0;
115 }
View Code

 

 
 

转载于:https://www.cnblogs.com/FrozenApple/p/4924735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值