Time Limit: 2000MS
Memory Limit: 65536K
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
题意:
给一张n*m的图,S表示起点,X表示障碍,不能走,A,B,C,D,E表示门,只有拿到所有对应的钥匙才能打开相应的门(比如,要打开A,必须先拿到地图上所有a,其他门也一样。)询问能否拿到宝藏(G处)。能则输出YES,不能则输出NO
题解:
从起点开始bfs,如果这遍能得到钥匙,则再从起点开始再次遍历,直到一次遍历找不到钥匙为止。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#define LiangJiaJun main
#define pa pair<int,int>
using namespace std;
int n,m;
bool iskey(char c){
if(c=='a'||c=='b'||c=='c'||c=='d'||c=='e')return 1;
return 0;
}
bool isdoor(char c){
if(c=='A'||c=='B'||c=='C'||c=='D'||c=='E')return 1;
return 0;
}
char mp[24][24];
int hav[14],g[14];
int dx[4]={0,1,0,-1},
dy[4]={1,0,-1,0};
pa st;
queue<pa>q;
bool vis[24][24];
bool pd;
int bfs(pa be){
int get=0;
q.push(be);
memset(vis,0,sizeof(vis));
vis[be.first][be.second]=1;
while(!q.empty()){
pa now=q.front();q.pop();
for(int i=0;i<4;i++){
int nowx=now.first+dx[i],nowy=now.second+dy[i];
if(nowx<=n&&nowx>0&&nowy<=m&&nowy>0&&mp[nowx][nowy]!='X'&&!vis[nowx][nowy]){
if(isdoor(mp[nowx][nowy])){
if(hav[mp[nowx][nowy]-'A']<=g[mp[nowx][nowy]-'A']){
vis[nowx][nowy]=1;
q.push(make_pair(nowx,nowy));
mp[nowx][nowy]='.';
}
}
else{
if(mp[nowx][nowy]=='G'){pd=1;break;}
vis[nowx][nowy]=1;
if(iskey(mp[nowx][nowy])){
++g[mp[nowx][nowy]-'a'];
mp[nowx][nowy]='.';
++get;
}
q.push(make_pair(nowx,nowy));
}
}
}
}
return get;
}
int w33ha(){
for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
memset(hav,0,sizeof(hav));
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(iskey(mp[i][j]))hav[mp[i][j]-'a']++;
if(mp[i][j]=='S')st=make_pair(i,j);
}
}
pd=0;
while(bfs(st));
if(!pd)puts("NO");
else puts("YES");
return 0;
}
int LiangJiaJun(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)break;
w33ha();
}
return 0;
}