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
题目大意
有一个迷宫,迷宫里有最多五种门和对应的钥匙,如果把迷宫中的某种钥匙全部拿到就可以打开对应的门。
思路
这道题数据范围是n,m<=20,所以dfs和bfs都能做,我这里只打了bfs(迷宫问题当然要用bfs╭(╯^╰)╮),思路是,先从起点bfs一遍,如果能到终点就直接输出YES,否则查看拿到的钥匙,把能打开的门全部打开(在地图上标记为可以走的路),不能打开的门要保留下来,说不定下一次就能打开,然后再跑bfs,直到没有搜到终点并且没有搜到新的钥匙,这时退出循环输出NO
#define ll long long
#define vec vector<int>
#define P pair<int,int>
#define inf 0x3f3f3f3f
#define MAX 25
int M, N, num[5], sx, sy, tx, ty, key[5];
bool vis[MAX][MAX];
int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
string s[MAX];
bool check(int x, int y) {
//超出边界或者撞墙或者是一个门
if (x < 0 || x >= M || y < 0 || y >= N || s[x][y] == 'X')return false;
return true;
}
bool bfs(int sx, int sy) {
memset(vis, 0, sizeof(vis));
memset(key, 0, sizeof(key));//统计走过的路中所有的钥匙集合
vis[sx][sy] = 1; queue<P> q;
q.push(P(sx, sy)); vector<P> v;
while (1) {
if (q.empty())return false;
while (!q.empty()) {
int x = q.front().first, y = q.front().second; q.pop();
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (check(xx, yy) && !vis[xx][yy]) {
vis[xx][yy] = 1;
//这是一个门,把能访问到的门单独加入
if (s[xx][yy] >= 'A'&&s[xx][yy] <= 'E') {
v.push_back(P(xx, yy));//门加入队列
}
else {
//找到一个钥匙
if (s[xx][yy] >= 'a'&&s[xx][yy] <= 'e')
key[s[xx][yy] - 'a']++;//找到了新钥匙
q.push(P(xx, yy));
if (xx == tx && yy == ty)return true;
}
}
}
}
//无路可走,看看能不能开门,开了的门要删掉,不能开的门要留下说不定下次可以
int i = 0;
while (i < v.size()) {
int xx = v[i].first, yy = v[i].second;
if (key[s[xx][yy] - 'A'] == num[s[xx][yy] - 'A'])
q.push(P(xx, yy)), v.erase(v.begin() + i);//钥匙够了,可以开
else i++;
}
}
}
int main() {
while (scanf("%d %d", &M, &N) && M + N != 0) {
memset(num, 0, sizeof(num));
for (int i = 0; i < M; i++) {
cin >> s[i];
for (int j = 0; j < N; j++) {
if (s[i][j] == 'S')sx = i, sy = j;
else if (s[i][j] == 'G')tx = i, ty = j;
else if (s[i][j] >= 'a'&&s[i][j] <= 'e')
num[s[i][j] - 'a']++;
}
}
if (bfs(sx, sy))printf("YES\n");
else printf("NO\n");
}
}