Escape from Ayutthaya

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on CodeForcesGym. Original ID: 101047E
64-bit integer IO format: %I64d      Java class name: (Any)

Input/Output: 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 M symbols 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
3
4 5
....S
.....
.....
F...E
4 4
...S
....
....
F..E
3 4
###S
####
E..F
Output
Y
N
N

Source

题意:S是起点,E是起点,F是火,#是墙,.是路,人从起点跑向终点,碰到火立刻死亡(即使人在终点与火相遇,也不能出去),人每分钟移动一个格子,火每分钟向上下左右四个方向蔓延一个格子,问人是否能跑出去,能输出Y,否则输出N。

两次广搜,第一次先记录火蔓延到每个格子的时间,然后搜索人跑出去的时间。

附上代码:

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#define MP make_pair
using namespace std;
char maps[][];
int n,m;
int vis[][];
int ss[][]; ///记录火的蔓延速度
int s[][]; ///记录人的行走速度
int dx[]={,,-,};
int dy[]={,,,-}; bool judge(int x,int y)
{
if(x>= && x<=n && y>= && y<=m) return ;
return ;
} void BFS(int x,int y) ///搜索人到达终点的时间
{
int i;
queue< pair<int,int> > q;
memset(s,-,sizeof(s));
s[x][y]=;
q.push(MP(x,y));
while(!q.empty())
{
x=q.front().first;
y=q.front().second;
q.pop();
if(maps[x][y]=='E')
{
// cout<<s1.t<<endl;
printf("Y\n");
return;
}
for(i=; i<; i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(judge(xx,yy)&&s[xx][yy] ==-&&maps[xx][yy]!='#'&&s[x][y]+<ss[xx][yy]) ///人到达这个点一定要比火快,才能走
{
s[xx][yy]=s[x][y]+;
q.push(MP(xx,yy));
}
}
}
printf("N\n");
return;
} void BFS2() ///搜索火的蔓延速度
{
queue< pair<int,int> >qq;
int i,j;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
if(maps[i][j]=='F')
{
ss[i][j]=;
qq.push(MP(i,j));
}
while(!qq.empty())
{
int x=qq.front().first;
int y=qq.front().second;
qq.pop();
for(int i=; i<; i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(judge(xx,yy)&&maps[xx][yy]!='#'&&ss[xx][yy]==-)
{
ss[xx][yy]=ss[x][y]+;
qq.push(MP(xx,yy));
}
}
}
return;
}
int main()
{
int i,j,T;
int a1,b1;
scanf("%d",&T);
getchar();
while(T--)
{
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
for(j=; j<=m; j++)
ss[i][j]=-;
int w=;
for(i=; i<=n; i++)
{
getchar();
for(j=; j<=m; j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='S')
{
a1=i;
b1=j;
}
}
}
BFS2();
BFS(a1,b1);
}
return ; }

bnu 52037 Escape from Ayutthaya的更多相关文章

  1. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  2. 简单明了区分escape、encodeURI和encodeURIComponent

    一.前言 讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它们不是同一类 简单来说,escape是对字符串(string)进行编码(而另外两种 ...

  3. c#模拟js escape方法

    public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...

  4. 【BZOJ-1340】Escape逃跑问题 最小割

    1340: [Baltic2007]Escape逃跑问题 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 264  Solved: 121[Submit] ...

  5. LYDSY热身赛 escape

    Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上矩形的行 ...

  6. javascript escape()函数和unescape()函数

    javascript escape()函数和unescape()函数 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法: escape(string) stri ...

  7. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  8. escape,encodeURI,encodeURIComponent的区别

    escape是对字符串进行编码而另外两种是对URL. encodeURI方法不会对下列字符编码 ASCII字母 数字 ~!@#$&*()=:/,;?+'encodeURIComponent方法 ...

  9. C#针对js escape解码

    在javascript 中通常用escape与unescape进行编码以方便传输. 在asp.net页面接收到这些数据以后可以使用 Microsoft.JScript.GlobalObject.une ...

随机推荐

  1. day36 07-Hibernate抓取策略:many-to-one上的抓取策略

    package cn.itcast.test; import java.util.List; import org.hibernate.Hibernate; import org.hibernate. ...

  2. cf round 480E The Number Games

    题意:给一棵树,点$i$的点权是$2^i$,你需要删掉$k$个点,使得剩下的点连通的情况下剩下的点权值和最大. $k \leq n \leq 10^6$ 如果考虑删哪些点,是不好考虑的,会出问题. 反 ...

  3. shared_from_this bad_weak_ptr的原因

    原因:创建类A的对象的时候没有用智能指针包裹,而是直接new的裸指针. enable_from_this 的使用与实现原理说明: shared_from_this()是enable_shared_fr ...

  4. 杨柳絮-Info:太原市多部门通力合作科学治理杨柳飞絮效果好

    ylbtech-杨柳絮-Info:太原市多部门通力合作科学治理杨柳飞絮效果好 1.返回顶部 1. 太原市多部门通力合作科学治理杨柳飞絮效果好 2016-04-21 07:16 4月10日,随着气温升高 ...

  5. Linux操作系统各版本ISO镜像下载(包括oracle linux\redhat\centos\u

    Linux操作系统各版本ISO镜像下载(包括oracle linux\redhat\centos\ubuntu\debian等) 1.Oracle Linux(下载地址) (1)OracleLinux ...

  6. 第二周<岭回归>

    传统最小二乘法缺乏稳定性 额.就是曾加正则项 \( argmin||Xw-y||^2+\alpha||w||^2 \) 对应矩阵的求解方法为 \(w=(X^TX+\alpha*I)^{-1}X^Ty\ ...

  7. Swift 之类的继承与类的访问权限

    http://www.cocoachina.com/swift/20160104/14821.html 上一篇博客<窥探Swift之别具一格的Struct和Class>的博客可谓是给Swi ...

  8. node.js(二)各种模块

    我们知道Node.js适合于IO密集型应用,不适合于CPU密集型应用.    JS和Node.js区别:         JS运行于客户端浏览器中,存在兼容性问题:数据类型:值类型+引用类型(ES+D ...

  9. Directx11教程(18) D3D11管线(7)

    原文:Directx11教程(18) D3D11管线(7) 光栅化阶段(RS)之后,将进入PS/OM阶段. 参考外文资料:http://fgiesen.wordpress.com/2011/07/01 ...

  10. 洛谷 P3951 小凯的疑惑 找规律

    目录 题面 题目链接 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例: 输出样例: 说明 思路 证明 AC代码 include<bits/stdc++.h> 题面 ...