Escape from Ayutthaya
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
standard input/output
Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to its collapse in 1767. The organization of Extraordinary Mystery Investigators (IME, in their language) aims to uncover the secrets of this ancient kingdom. One of IME‘s most notorious historians is Márcio "the indispensable" Himura. He is currently researching the laws and punishments in place during King Ramathibodi I‘s rule. Recent discoveries suggest how Ramathibodi I used to punish the subjects that did not convert to Theravada Buddhism, the religion he adopted.
The punishment involved trapping the accused prisoner in a room with a single exit and to light up a fire. If the prisoner could manage to reach the exit before getting caught on fire, she or he was forgiven and allowed to live. Márcio has access to some records that describe the floorplans of the rooms where this punishment took place. However, there are no documents asserting whether the prisoners were forgiven. Márcio would like to know whether each of these prisoners had any chance at all of having been forgiven. For that, Márcio represented each room as a grid with N rows and M columns, where each position has a symbol with the following meaning
where "start" is the person‘s initial position in the room when fire has been lit up. Moreover, Márcio imposed the following constraints in his model:
- Fire spreads in the four cardinal directions (N, S, E, O) at the speed of one cell per minute.
- The prisoners can also move in these four directions at the same speed.
- Neither fire nor the prisoners can walk through a wall.
- If the prisoner and fire occupy the same position at any instant, the prisoner dies instantaneously.
You are a member of IME and Márcio would like to know if you deserve your position. He has charged you with the task of determining whether a prisoner had any chance to be forgiven.
Input
The first line has a single integer T, the number if test cases.
Each instance consists of several lines. The first line contains two integers, N and M. Each of the following N lines contains exactly Msymbols representing, as described above, a room from which the prisoner must escape.
Limits
- 1 ≤ T ≤ 100
- The sum of the sizes of the matrices in all test cases will not exceed 2 cdot106
- 1 ≤ N ≤ 103
- 1 ≤ M ≤ 103
Output
For each instance, print a single line containing a single character. Print Y if the prisoner had any chance of being forgiven; otherwise, print N.
Sample Input
Input
34 5....S..........F...E4 4...S........F..E3 4###S####E..F
Output
YNN 题意:火会不断的往上下左右蔓延 火的速度和人一样 看人能不能到终点 如果火和人同时到达一个地方 人会gg 做的时候没想到 后面听了菊花之言 才发现可以转换为从终点出发 看先遇到火还是先遇到起点 这样的话时间复杂度会降低很多而且这道题比较坑的地方就是火不止一把 我们队做的时候完全没发现 wa成狗
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using namespace std; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w",stdout); #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; typedef pair<int,int> PII; const double PI = acos(-1); const int MAXN = 1e3 + 5; char mp[MAXN][MAXN]; int vis[MAXN][MAXN]; int T, n, m; int flag; int fx, fy, sx, sy, ex, ey; struct node{ int x, y; int setp; node(int xx, int yy, int ss){ x = xx; y = yy; setp = ss; } }; bool check(int x, int y){ return x >= 0 && x < n && y >= 0 && y < m && mp[x][y] != ‘#‘ && !vis[x][y]; } void bfs(int x, int y){ node c(0, 0, INF); queue<node> q; q.push(node(x, y, 0)); memset(vis, 0, sizeof(vis)); while(!q.empty()){ node now = q.front(); q.pop(); if(!check(now.x, now.y)) continue; vis[now.x][now.y] = 1; if(mp[now.x][now.y] == ‘S‘){ c = now; } if(now.setp > c.setp){ flag = 1; break; } if(mp[now.x][now.y] == ‘F‘){ break; } q.push(node(now.x + 1, now.y, now.setp + 1)); q.push(node(now.x - 1, now.y, now.setp + 1)); q.push(node(now.x, now.y + 1, now.setp + 1)); q.push(node(now.x, now.y - 1, now.setp + 1)); } } int main() { //FIN scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); for(int i = 0; i < n; i ++) scanf("%s", mp[i]); for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++){ if(mp[i][j] == ‘E‘){ ex = i; ey = j; } } flag = 0; bfs(ex, ey); if(flag) puts("Y"); else puts("N"); } }