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. python基础--计算机网络

    网络编程: 软件开发架构: c/s架构(client/server) c:客户端 s:服务端 b/s架构(browser/server) b:浏览器 s:服务端 服务端:24小时不间断提供服务 客户端 ...

  2. vi中如何跳转到指定行数

    输入:n,代表跳转到第n行,如:100,就跳转到第100行.

  3. PCL配置即常见问题

    1    下载 把与VS版本对应PCL的AllInOne包下载下来.要下对安装包,需要了解安装包的命名的含义,以下面的一个AllInOne包的名字为例. PCL-1.8.0-AllInOne-msvc ...

  4. 2019-2-16-WPF-封装-dotnet-remoting-调用其他进程

    title author date CreateTime categories WPF 封装 dotnet remoting 调用其他进程 lindexi 2019-02-16 09:40:26 +0 ...

  5. 【水滴石穿】react-native-book

    先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=12778558 ...

  6. StatusBar用法

    一.StatusBar组件介绍 StatusBar 是 React Native 0.20 起新增的跨平台组件,它可以用来设置并动态改变设备的状态栏显示特性. StatusBar 组件可以同时加载多个 ...

  7. 深入浅出Cocoa之类与对象【转】

    最近打算写一些ObjC中比较底层的东西,尤其是 runtime 相关的.苹果已经将 ObjC runtime 代码开源了,我们可以从:http://opensource.apple.com/sourc ...

  8. DAY1-作业

    Python-day1-------> 本节内容: Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据 ...

  9. 怎样做一个iOS App的启动分层引导动画?

    一. 为什么要写这篇文章? 这是一个很古老的话题,从两年前新浪微博开始使用多层动画制作iOS App的启动引导页让人眼前一亮(当然,微博是不是历史第一个这个问题值得商榷)之后,各种类型的引导页层出不穷 ...

  10. Oracle使用——Oracle表字段的增加、删除、修改和重命名

    增加字段 语法 alter table tablename add (column datatype [default value][null/not null]); 说明:alter table 表 ...