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. 页面PC端 / 移动端整屏纵向翻页,点击/触摸滑动效果功能代码非插件

    页面翻页,滑动功能示范代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  2. 怎样判断一个exe可执行程序(dll文件)是32位的还是64位的

    看到一个比较简单粗暴的方式,做个记录. 直接用记事本或者notepad++(文本编辑软件都可)打开exe文件(dll文件), 会有很多乱码,接下来只需要在第二段中找到PE两个字母,在其后的不远出会出现 ...

  3. PyCharm如何删除工程项目

    1.在菜单中选择:file——>close project 2.选择需要删除的项目右上角的“×”号进行删除工程项目 3.找到工程项目的存放路径,删除对应的工程项目文件 通过上诉操作即可在pych ...

  4. MYSQL监控工具--mytop

    https://mp.weixin.qq.com/s/1X_uZaajImRRmpAsdLsNGw mysql可以说如今最为流行的数据库了,虽然现在nosql的风头正盛.但我想很多公司重要的业务数据不 ...

  5. 强力Django+杀手级xadmin开发在线教育网站

    强力Django+杀手级xadmin开发在线教育网站采用 Python3.7全新开发 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的 ...

  6. 阿里云 EMAS HTTPDNS 联合函数计算重磅推出 SDNS 服务,三大能力获得突破

    1. 什么是 HTTPDNS ? 传统的 DNS(Domain Name System)使开发者常面临着域名劫持.调度不精准的问题. HTTPDNS 使用 HTTP 协议替换常用的 UDP 协议,完成 ...

  7. 【codeforces Manthan, Codefest 17 C】Helga Hufflepuff's Cup

    [链接]h在这里写链接 [题意]     k是最高级别的分数,最高界别的分数最多只能有x个.     1<=k<=m;     和k相邻的点的分数只能小于k;     n个点的树,问你每个 ...

  8. Thrift框架应用参考

    Thrift https://blog.csdn.net/aquester/article/details/48261609 https://www.cnblogs.com/liboBlog/p/60 ...

  9. li设置多选和取消选择的样式、输入数据类型判断

    li设置多选和取消选择的样式: $('li').click(function(){ if($(this).hasClass('active')) {$(this).removeClass('activ ...

  10. Android书架实现

    转自http://blog.csdn.net/wangkuifeng0118/article/details/7944215 书架效果: 下面先看一下书架的实现原理吧! 首先看一下layout下的布局 ...