「POJ2157」Maze
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
1 2 3 4 5 6 7 8 9 10 |
4 4 S.X. a.X. ..XG .... 3 4 S.Xa .aXB b.AG 0 0 |
Sample Output
1 2 |
YES NO |
题解
注意两种坑爹情况
1.图中没有某扇门的任何钥匙
这样不能通过这扇门
2.门有可能多扇
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char mp[21][21]; int n,m,bx,by,tot; bool flag=0,mark[21][21],vis[21][21]; int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0}; struct key{int sum,tot;}key[5]; struct door{int k,x,y;}d[401]; void dfs(int x,int y) { mark[x][y]=1; if(flag)return; for(int i=0;i<4;i++) { int nowx=x+xx[i],nowy=y+yy[i]; if(nowx<1||nowy<1||nowx>n||nowy>m)continue; if(mp[nowx][nowy]=='X'||mark[nowx][nowy])continue; if(mp[nowx][nowy]>='a'&&mp[nowx][nowy]<='e') { dfs(nowx,nowy); int tmp=mp[nowx][nowy]-'a'; key[tmp].sum++; if(key[tmp].sum==key[tmp].tot&&key[tmp].sum) for(int j=1;j<=tot;j++) if(d[j].k==tmp&&vis[d[j].x][d[j].y]) dfs(d[j].x,d[j].y); } else if(mp[nowx][nowy]=='G')flag=1; else if(mp[nowx][nowy]>='A'&&mp[nowx][nowy]<='E') { vis[nowx][nowy]=1; int tmp=mp[nowx][nowy]-'A'; if(key[tmp].sum==key[tmp].tot&&key[tmp].sum) dfs(nowx,nowy); } else dfs(nowx,nowy); } } int main() { while(scanf("%d%d",&n,&m)&&n) { memset(d,0,sizeof(d)); memset(key,0,sizeof(key)); memset(mark,0,sizeof(mark)); memset(vis,0,sizeof(vis)); tot=flag=0; for(int i=1;i<=n;i++) { scanf("%s",mp[i]+1); for(int j=1;j<=m;j++) if(mp[i][j]=='S'){bx=i;by=j;} else if(mp[i][j]>='a'&&mp[i][j]<='e') key[mp[i][j]-'a'].tot++; else if(mp[i][j]>='A'&&mp[i][j]<='E') {d[++tot].x=i;d[tot].y=j;d[tot].k=mp[i][j]-'A';} } dfs(bx,by); if(flag)printf("YES\n"); else printf("NO\n"); } return 0; } |
Subscribe