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或者没有可以进入的门则结束循环。
【代码】
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxn = +;
const int dx[]={-,,,};
const int dy[]={,,,-};
struct Node{
int x,y;
};
struct door{
int num,sum;
}door[maxn]; char G[maxn][maxn];
int n,m,ex,ey;
queue<Node> q; int vis[maxn][maxn],vis_door[maxn];
bool inside(int x,int y) {
return x>= && x<=n && y>= && y<=m;
}
void BFS() {
bool f=true;
while(f)
{
while(!q.empty()) {
Node u=q.front(); q.pop();
int x=u.x,y=u.y;
if(x==ex && y==ey) {
printf("YES\n");
return ;
}
FOR(i,,) {
int xx=x+dx[i],yy=y+dy[i];
if(inside(xx,yy) && G[xx][yy]!='X') {
if(vis[xx][yy]) continue;
if(G[xx][yy]>='A'&&G[xx][yy]<='E') {
vis_door[G[xx][yy]-'A']=;
continue;
}
if(G[xx][yy]>='a'&&G[xx][yy]<='e') {
door[G[xx][yy]-'a'].sum++;
}
vis[xx][yy]=;
q.push((Node){xx,yy});
}
}
}
f=false;
FOR(i,,n) FOR(j,,m)
if(G[i][j]>='A' && G[i][j]<='E' && !vis[i][j])
{
int c=G[i][j]-'A';
if(vis_door[c]&& door[c].num> && (door[c].num==door[c].sum))
{
vis[i][j]=;
q.push((Node){i,j});
f=true;
}
}
}
printf("NO\n");
} void init() {
while(!q.empty()) q.pop();
memset(vis,,sizeof(vis));
memset(vis_door,,sizeof(vis_door));
for(int i=;i<;i++) door[i].num=door[i].sum=;
}
void read() {
FOR(i,,n) FOR(j,,m)
{
cin>>G[i][j];
if(G[i][j]=='S')
{
vis[i][j]=;
q.push((Node){i,j});
}
if(G[i][j]=='G') ex=i,ey=j;
if(G[i][j]>='a' && G[i][j]<='e') //统计钥匙数目
door[G[i][j]-'a'].num++;
}
} int main() {
while(cin>>n>>m && (n&&m))
{
init();
read();
BFS();
}
return ;
}