标签:
Time Limit: 2000MS | Memory Limit: 65536K |
4 4 S.X. a.X. ..XG .... 3 4 S.Xa .aXB b.AG 0 0
YES NO
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 }
标签:
原文地址:http://www.cnblogs.com/FrozenApple/p/4924735.html