NOI题库1159 Maze

1159:Maze

总时间限制:

2000ms

内存限制:

65536kB

描述

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.

输入

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.

输出

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

样例输入

4 4

S.X.

a.X.

..XG

....

3 4

S.Xa

.aXB

b.AG

0 0

样例输出

YES

NO

来源

POJ Monthly,Wang Yijie

【思路】

  BFS。

  题意:

  

  不断循环BFS,在每次BFS中:统计可以得到的钥匙,对于可以访问到的门检查是否钥匙齐全,如果是则入队继续BFS。直到到达G或者没有可以进入的门则结束循环。

【代码】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<iostream>
 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 6 using namespace std;
 7 
 8 const int maxn = 30+5;
 9 const int dx[]={-1,0,1,0};
10 const int dy[]={0,1,0,-1};
11 struct Node{
12     int x,y;
13 };
14 struct door{
15     int num,sum;
16 }door[maxn];
17 
18 char G[maxn][maxn];
19 int n,m,ex,ey;
20 queue<Node> q;
21 
22 int vis[maxn][maxn],vis_door[maxn];
23 bool inside(int x,int y) {
24     return x>=1 && x<=n && y>=1 && y<=m;
25 }
26 void BFS() {
27     bool f=true;
28     while(f)
29     {
30         while(!q.empty()) {
31             Node u=q.front(); q.pop();
32             int x=u.x,y=u.y;
33             if(x==ex && y==ey) {
34                 printf("YES
");
35                 return ;
36             }
37             FOR(i,0,3) {
38                 int xx=x+dx[i],yy=y+dy[i];
39                 if(inside(xx,yy) && G[xx][yy]!='X') {
40                     if(vis[xx][yy]) continue;
41                     if(G[xx][yy]>='A'&&G[xx][yy]<='E') {
42                         vis_door[G[xx][yy]-'A']=1;
43                         continue;
44                     }
45                     if(G[xx][yy]>='a'&&G[xx][yy]<='e') {
46                         door[G[xx][yy]-'a'].sum++;
47                     }
48                     vis[xx][yy]=1;
49                     q.push((Node){xx,yy});
50                 }
51             }
52         }
53         f=false;
54         FOR(i,1,n) FOR(j,1,m)
55             if(G[i][j]>='A' && G[i][j]<='E' && !vis[i][j])
56             {
57                 int c=G[i][j]-'A';
58                 if(vis_door[c]&& door[c].num>0 && (door[c].num==door[c].sum))
59                 {
60                     vis[i][j]=1;
61                     q.push((Node){i,j});
62                     f=true;
63                 }
64             }
65     }
66     printf("NO
");
67 }
68 
69 void init() {
70     while(!q.empty()) q.pop();
71     memset(vis,0,sizeof(vis));
72     memset(vis_door,0,sizeof(vis_door));
73     for(int i=0;i<10;i++) door[i].num=door[i].sum=0;
74 }
75 void read() {
76     FOR(i,1,n) FOR(j,1,m)
77     {
78         cin>>G[i][j];
79         if(G[i][j]=='S')
80         {
81             vis[i][j]=1;
82             q.push((Node){i,j});
83         }
84         if(G[i][j]=='G') ex=i,ey=j;
85         if(G[i][j]>='a' && G[i][j]<='e')  //统计钥匙数目
86             door[G[i][j]-'a'].num++;
87     }    
88 }
89 
90 int main() {
91     while(cin>>n>>m && (n&&m)) 
92     {
93         init();
94         read();
95         BFS();
96     }
97     return 0;
98 }
原文地址:https://www.cnblogs.com/lidaxin/p/4933158.html