Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3183 | Accepted: 996 |
Description
Input
Output
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<iostream> #include <string.h> #include <queue> using namespace std; typedef struct node { int x; int y; node(int a, int b) { x = a; y = b; } node() { } }Map; char Maze[25][25]; int Dir[4][2] = {-1,0,1,0,0,-1,0,1}; int key[10]; void BFS(int sx, int sy, int m, int n) { queue<Map> Queue; Queue.push(Map(sx, sy)); Map temp; Maze[sx][sy] = 'X'; int Limit = 0; while(!Queue.empty() && Limit < 400) { ++Limit; temp = Queue.front(); Queue.pop(); if (Maze[temp.x][temp.y] >= 'A' && Maze[temp.x][temp.y] <= 'E') { if (key[Maze[temp.x][temp.y] - 'A'] == 0) { Maze[temp.x][temp.y] = 'X'; } else { Queue.push(temp); continue; } } for (int i = 0; i < 4; i++) { int x = temp.x + Dir[i][0]; int y = temp.y + Dir[i][1]; if (x >= 0 && x < m && y >= 0 && y < n && Maze[x][y] != 'X') { if (Maze[x][y] == '.') { Maze[x][y] = 'X'; Queue.push(Map(x, y)); } if (Maze[x][y] >= 'a' && Maze[x][y] <= 'e') { key[Maze[x][y] - 'a']--; Maze[x][y] = 'X'; Queue.push(Map(x, y)); } if (Maze[x][y] == 'G') { printf("YES\n"); return; } if (Maze[x][y] >= 'A' && Maze[x][y] <= 'E') { Queue.push(Map(x, y)); } } } } printf("NO\n"); } int main() { int m, n; int sx, sy; while(scanf("%d%d", &m, &n) != EOF) { if (m == 0 && n == 0) { break; } memset(key, 0, sizeof(key)); for (int i = 0; i < m; i++) { scanf("%s", Maze[i]); for (int j = 0; j < n; j++) { if (Maze[i][j] == 'S') { sx = i; sy = j; } else { if (Maze[i][j] >= 'a' && Maze[i][j] <= 'e') { key[Maze[i][j] - 'a']++; } } } } BFS(sx, sy, m, n); } return 0; }