bnu 52037 Escape from Ayutthaya

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

2015 USP Try-outs

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

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

附上代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <queue>
  6 #define MP make_pair
  7 using namespace std;
  8 char maps[1005][1005];
  9 int n,m;
 10 int vis[1005][1005];
 11 int ss[1005][1005]; ///记录火的蔓延速度
 12 int s[1005][1005];  ///记录人的行走速度
 13 int dx[]={1,0,-1,0};
 14 int dy[]={0,1,0,-1};
 15
 16 bool judge(int x,int y)
 17 {
 18     if(x>=1 && x<=n && y>=1 && y<=m) return 1;
 19     return 0;
 20 }
 21
 22 void BFS(int x,int y) ///搜索人到达终点的时间
 23 {
 24     int i;
 25     queue< pair<int,int> > q;
 26     memset(s,-1,sizeof(s));
 27     s[x][y]=0;
 28     q.push(MP(x,y));
 29     while(!q.empty())
 30     {
 31         x=q.front().first;
 32         y=q.front().second;
 33         q.pop();
 34         if(maps[x][y]==‘E‘)
 35         {
 36             // cout<<s1.t<<endl;
 37             printf("Y\n");
 38             return;
 39         }
 40         for(i=0; i<4; i++)
 41         {
 42             int xx=x+dx[i];
 43             int yy=y+dy[i];
 44             if(judge(xx,yy)&&s[xx][yy] ==-1&&maps[xx][yy]!=‘#‘&&s[x][y]+1<ss[xx][yy]) ///人到达这个点一定要比火快,才能走
 45             {
 46                 s[xx][yy]=s[x][y]+1;
 47                 q.push(MP(xx,yy));
 48             }
 49         }
 50     }
 51     printf("N\n");
 52     return;
 53 }
 54
 55 void BFS2()  ///搜索火的蔓延速度
 56 {
 57     queue< pair<int,int> >qq;
 58     int i,j;
 59     for(i=1; i<=n; i++)
 60         for(j=1; j<=m; j++)
 61             if(maps[i][j]==‘F‘)
 62             {
 63                 ss[i][j]=0;
 64                 qq.push(MP(i,j));
 65             }
 66     while(!qq.empty())
 67     {
 68         int x=qq.front().first;
 69         int y=qq.front().second;
 70         qq.pop();
 71         for(int i=0; i<4; i++)
 72         {
 73             int xx=x+dx[i];
 74             int yy=y+dy[i];
 75             if(judge(xx,yy)&&maps[xx][yy]!=‘#‘&&ss[xx][yy]==-1)
 76             {
 77                 ss[xx][yy]=ss[x][y]+1;
 78                 qq.push(MP(xx,yy));
 79             }
 80         }
 81     }
 82     return;
 83 }
 84 int main()
 85 {
 86     int i,j,T;
 87     int a1,b1;
 88     scanf("%d",&T);
 89     getchar();
 90     while(T--)
 91     {
 92         scanf("%d%d",&n,&m);
 93         memset(vis,0,sizeof(vis));
 94         for(i=1; i<=n; i++)
 95             for(j=1; j<=m; j++)
 96                 ss[i][j]=-1;
 97         int w=0;
 98         for(i=1; i<=n; i++)
 99         {
100             getchar();
101             for(j=1; j<=m; j++)
102             {
103                 scanf("%c",&maps[i][j]);
104                 if(maps[i][j]==‘S‘)
105                 {
106                     a1=i;
107                     b1=j;
108                 }
109             }
110         }
111         BFS2();
112         BFS(a1,b1);
113     }
114     return 0;
115
116 }
时间: 2024-07-28 16:09:37

bnu 52037 Escape from Ayutthaya的相关文章

ACM: Gym 101047E Escape from Ayutthaya - BFS

Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to it

CodeForces Gym 101047E Escape from Ayutthaya BFS

Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 101047E Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to its collaps

newLISP处理mysql escape character

什么是转义字符 mysql的escape character指的是需要转义的特殊字符,这些字符出现在sql语句中,如果没有转移会导致sql语法报错或者有sql注入攻击的可能. 主要有以下几种都需转义: \x00, \n, \r, \, ', " and \x1a. 比如' 就需要变成\' 下面是sql测试: mysql> INSERT INTO nodes(name) VALUES ('select a.dt, count(*), count(distinct a.uv) from (se

hzau 1204 Escape from the Darkness

1204: Escape from the Darkness Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 93  Solved: 3[Submit][Status][Web Board] Description Xiao Ming, a high school student, learnt blackbody radiation from the physics class. The black body on the book is ind

Python 3 与 Javascript escape 传输确保数据正确方法和中文乱码解决方案

前几天用Python的Bottle框架写个小web程序,在进行Ajax交互之时,前端则先用 JSON.stringify 来将类序列化,然后用escape() 函数将其编码,确保传输正确. 再基本上配合上Jquery的$.ajax应该就可以了,可能是经验不足,即使编码之后的数据依然在 Python 中难以处理. 后来慢慢思考出一种方式,在网上也发现了类似的方式,于是将其实现. 基本思路如下: escape('你好世界ABC'); //返回 "%u4F60%u597D%u4E16%u754CABC

【转】escape()、encodeURI()、encodeURIComponent()区别详解

escape().encodeURI().encodeURIComponent()区别详解 原文链接:http://www.cnblogs.com/tylerdonet/p/3483836.html JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent . 下面简单介绍一下它们的区别 1 escape()函数 定义和用法 e

BNU 4096 逆序 思维题

https://www.bnuoj.com/v3/problem_show.php?pid=4096 对于一个序列a,我们定义它的逆序数为满足a[i]>a[j]且i<j的有序对<i,j>的个数,这样的有序对称为逆序对. 例如 a[0]=1,a[1]=2,a[2]=4,a[3]=5,a[4]=3,存在的逆序对就有<2,4>和<3,4>,其逆序数就是2. 现在,给你一个长为N的序列,要求恰好执行K次交换操作,每次交换只能在相邻的两个数之间进行,问得到的结果序列其

hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 8001    Accepted Submission(s): 1758 Problem Description 2012 If this is the end of th

ZOJ3640-Help Me Escape

Help Me Escape Time Limit: 2 Seconds      Memory Limit: 32768 KB Background     If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him. And Ca