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
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 25
int M[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char s1[6] = "ABCDE", s2[6] = "abcde", s[max][max];
int K[5], F[5], num, sign;
struct NN
{
int x, y;
char ch;
}NN[1000];
void Dfs(int x, int y, int n, int m)
{
if (sign)
{
return;
}
int i, j, row, col;
for (i = 0; i < 4; i++)
{
row = x + M[i][0];
col = y + M[i][1];
if (row >= 0 && row < n && col >= 0 && col < m && s[row][col] != 'X' && s[row][col] != 'Z')
{
if (s[row][col] == 'G')
{
sign = 1;
return;
}
if (s[row][col] != '.')
{
for (j = 0; j < 5; j++)
{
if (s[row][col] == s2[j])
{
K[s2[j] - 'a']++;
s[row][col] = 'X';
Dfs(row, col, n, m);
break;
}
else if (s[row][col] == s1[j])
{
NN[num].ch = s1[j];
NN[num].x = row;
NN[num++].y = col;
s[row][col] = 'X';
break;
}
}
}
else
{
s[row][col] = 'X';
Dfs(row, col, n, m);
}
if (sign)
{
return;
}
}
}
}
int main()
{
int n, m, x, y, tt, i, j;
char ch;
while (scanf("%d %d", &n, &m), (n && m))
{
sign = 0;
num = 0;
for (i = 0; i < 5; i++)
{
F[i] = 0;
K[i] = 0;
}
for (i = 0; i < n; i++)
{
scanf("%s", s[i]);
}
for (i = 0; i < n; i++)
{
for (j = 0; j <= m; j++)
{
if (s[i][j] >= 'a' && s[i][j] <= 'e')
{
F[s[i][j] - 'a']++;
}
else if (s[i][j] == 'S')
{
x = i;
y = j;
s[i][j] = 'X';
}
}
}
Dfs(x, y, n, m);
if (!sign)
{
while (1)
{
tt = 0;
for (i = 0; i < num; i++)
{
x = NN[i].x;
y = NN[i].y;
j = NN[i].ch - 'A';
if (s[x][y] == 'X' && F[j] != 0 && F[j] == K[j])
{
s[x][y] = 'Z';
tt = 1;
Dfs(x, y, n, m);
if (sign)
{
break;
}
}
}
if (sign || (!tt))
{
break;
}
}
}
if (sign)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
system("pause");
return 0;
}