查看 提交 统计 提问
总时间限制: 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
题意:给你一个row * col的迷宫,‘.’表示可走的空地,‘X’表示不可走的围墙,‘S’表示起点,‘G’表示宝藏的地点(终点),’A’~’E’分别表示5种类型的门,’a’~’e’分别对应表示5种类型门的钥匙。要想打开某种类型的门,只有拿到了迷宫内其对应的所有钥匙。问你最终能不能拿到这份宝藏?
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
const int maxn=1000+10;
const int INF=100000000;
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
struct Node{
int x,y;
Node (int x,int y):x(x),y(y){}
};
struct Door{
int sum,num;
}door[5];
int n,m;
char G[maxn][maxn];
queue<Node>Q;
int vis[maxn][maxn],vis_door[maxn][maxn];
int main()
{
while(scanf(("%d%d"),&n,&m)==2&&n&&m){
while(!Q.empty())Q.pop();
memset(door,0,sizeof(door));
memset(vis,0,sizeof(vis));
memset(vis_door,0,sizeof(vis_door));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>G[i][j];
if(G[i][j]=='S'){Q.push(Node(i,j));vis[i][j]=1;}
if(G[i][j]>='a'&&G[i][j]<='e'){
int w=G[i][j]-'a';
door[w].sum++;//迷宫内钥匙总和
}
}
bool flag=false,newdoor=true;
while(newdoor){
while(!flag&&!Q.empty()){
Node u=Q.front();Q.pop();
for(int i=0;i<4;i++){
int tx=u.x+dx[i];
int ty=u.y+dy[i];
if(G[tx][ty]=='G'){
flag=true;
break;
}
if(tx>=0&&tx<n&&ty>=0&&ty<m&&!vis[tx][ty]&&G[tx][ty]!='X'){
char t=G[tx][ty];
if(t>='A'&&t<='E'){
vis_door[tx][ty]=1;
continue;
}
if(t>='a'&&t<='e'){
door[t-'a'].num++;
}
vis[tx][ty]=1;
Q.push(Node(tx,ty));
}
}
}
if(flag)break;
newdoor=false;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
char t=G[i][j];
if(t>='A'&&t<='E'&&vis_door[i][j]&&!vis[i][j]){
int w=t-'A';
if(door[w].sum>0&&door[w].sum==door[w].num){
vis[i][j]=true;
Q.push(Node(i,j));
newdoor=true;
}
}
}
}
if(flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}