#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include <iostream> #include <cstring> using namespace std; char map[25][25]; bool flag; void DFS(int x, int y); void check(); struct K { int maxnum,num; }key[5]; struct DOOR { int x,y,flag; }door[5]; void check() { for(int i=0;i<5;i++) { if(door[i].flag&&key[i].num==key[i].maxnum) { map[door[i].x][door[i].y]='X'; door[i].flag=0; DFS(door[i].x,door[i].y+1); DFS(door[i].x+1,door[i].y); DFS(door[i].x,door[i].y-1); DFS(door[i].x-1,door[i].y); } } } int main() { int m,n,i,j; while(scanf("%d%d",&n,&m),n||m) { int bx,by; memset(door,0,sizeof(door)); memset(key,0,sizeof(key)); memset(map,'X',sizeof(map)); flag=0; for(int i = 1; i <= n; ++i) { scanf("%s",map[i]+1); for(int j = 1; j <= m; ++j) { if(map[i][j]=='S') { bx=i;by=j; } if(map[i][j]>='a'&&map[i][j]<='e') key[map[i][j]-'a'].maxnum++; } } DFS(bx,by); if(flag) printf("YES "); else printf("NO "); } return 0; } void DFS(int x,int y) { if(map[x][y]!='X') { if(map[x][y]=='G') { flag=1;return; } else if(map[x][y]>='a'&&map[x][y]<='e') { key[map[x][y]-'a'].num++; } else if(map[x][y]>='A'&&map[x][y]<='E') { door[map[x][y]-'A'].x=x; door[map[x][y]-'A'].y=y; door[map[x][y]-'A'].flag++; return; } map[x][y]='X'; DFS(x,y+1); DFS(x+1,y); DFS(x,y-1); DFS(x-1,y); check(); } }
这个题的输入很头疼