Maze
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 3846 | Accepted: 1220 |
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
题解:
题意是找到这个门的所有钥匙才能打开这扇门,问能否到达终点
bfs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n, m, sx, sy;
int xx[5] = {0, 0, 1, -1};
int yy[5] = {1, -1, 0, 0};
bool used[550][550];
char map[550][550];
struct qer
{
int x, y;
};
struct dqs
{
int x, y, key;
bool vis;
}door[600];
queue <qer> q;
void open(char ss)
{
if(door[ss].vis == 1)
{
qer v;
v.x = door[ss].x;
v.y = door[ss].y;
q.push(v);
}
else
map[door[ss].x][door[ss].y] = '.';
}
bool check(int x, int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m && map[x][y] != 'X' && used[x][y] == 0)
return 1;
return 0;
}
void bfs()
{
while(!q.empty())
q.pop();
q.push((qer){sx, sy});
used[sx][sy] = 1;
while(!q.empty())
{
qer u = q.front();
q.pop();
qer now;
for(int i = 0;i < 4;i ++)
{
now.x = u.x + xx[i];
now.y = u.y + yy[i];
if(check(now.x, now.y))
{
if(map[now.x][now.y] == 'G')
{
printf("YES\n");
return ;
}
else
{
used[now.x][now.y] = 1;
if(map[now.x][now.y] >= 'a' && map[now.x][now.y] <= 'e')
{
q.push(now);
int ss = map[now.x][now.y] - 'a' + 1;
door[ss].key --;
if(door[ss].key==0)
open(ss);
}
if(map[now.x][now.y] >= 'A' && map[now.x][now.y] <= 'E')
{
int ss = map[now.x][now.y] - '0' - 16;
door[ss].vis = 1;
}
if(map[now.x][now.y] == '.')
q.push(now);
}
}
}
}
puts("NO");
return ;
}
int main()
{
while(cin>>n>>m)
{
if(n == 0 && m == 0)
break;
memset(map, 0, sizeof(map));
memset(used, 0, sizeof(used));
memset(door, 0, sizeof(door));
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
cin>>map[i][j];
if(map[i][j] >= 'A' && map[i][j] <= 'E')
{
int ss = map[i][j] - '0' -16;
door[ss].x = i;
door[ss].y = j;
door[ss].vis = 0;
door[ss].key = 0;
}
}
}
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
if(map[i][j] == 'S')
sx = i, sy = j;
if(map[i][j] >= 'a' && map[i][j] <= 'e')
{
door[map[i][j]-'0'-48].key++;
}
}
}
bfs();
}
return 0;
}