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. Django--创建

    软件开发架构: c/s架构 客户端 服务端 b/s架构 浏览器 服务端 本质:b/s架构也是c/s架构 HTTP协议 超文本传输协议:规定了客户端与服务端之间消息传输的格式 四个特性: 1.基于TCP ...

  2. tcpdump的表达式介绍

    表达式是一个正则表达式,tcpdump利用它作为过滤报文的条件,如果一个报文满足表 达式的条件,则这个报文将会被捕获.如果没有给出任何条件,则网络上所有的信息包 将会被截获. 在表达式中一般如下几种类 ...

  3. 洛谷P1006 NOIP提高组2008 传纸条

    P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n 列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无 ...

  4. Leetcode598.Range Addition II范围求和2

    给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 < ...

  5. 举例分析private的作用【c/c++学习】

    抛砖引玉: c++中private的用处 我知道我们可以用 public 中的值,把private中的数据给提出来,但是还是搞不懂private该怎么用,或者说在一个具体程序中,private有什么用 ...

  6. Django快速创建新项目

    Python免费视频含配套文件QQ124111294 https://pan.baidu.com/s/1bL5ml4 python.exe manage.py startapp app01 pytho ...

  7. kubernetes1.5即将发布

    Kubernetes1.5将会在12月9日正式GA 目前已经进行到了第三阶段后期:从2016年11月22日到2016年12月8日 建立1.5发布分支 所有修复bug的代码都合入1.5发布分支中 将1. ...

  8. adb安装apk包时提示:device unauthorized

    adb install apk时提示device unauthorized,手机上还没出现usb是否允许调试的询问弹框: 解决方法: 1.cmd输入:adb kill-server,点击回车键 2.c ...

  9. HTML小技巧:按钮中的文字换行 .

    一般按钮的文字都是一行的.但是有的时候画面需要按钮中的文字换行. 刚开始有个开发人员说没法实现.\r\n 都用过了没有效果.其实google这个老师是非常强大的. 直接换行的方法:<input ...

  10. [运维]ESXI系统的安装 标签: 虚拟机运维vmware服务器虚拟化 2017-05-05 09:24 496人阅读 评论(15)

    上篇博客说到了VMware vSphere,那么接下来就讲一下我们如何将之投入使用.vsphere的虚拟机管理程序就是esxi. 什么是ESXI? vSphere产品套件的核心产品是虚拟机管理程序,作 ...