Gym 101047E Escape from Ayutthaya

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

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 Msymbols 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
/*/
题意:
国王把犯人关在一个迷宫里,然后在迷宫里点火,只有一个出口,如果犯人逃出来了,将会被释放,给出这个迷宫的图和点火位置,问人能不能逃出。 很明显是BFS,但是一开始想错了,找到S和F两个点去BFS的,然后比较返回的最小步数,如果火走到E的步数小于人的步数坑定会被烧死的。但是少考虑了有多个火把的情况WA了一次。然后改好,再交了一次,发现前面的样例过的挺快,以为要A了,结果T在了text 6 噗。。。接着各种奇葩剪枝出来了。一直T在text 6 这组数据真的很可以。。。 在和队友冷静下来讨论了一会,突然灵感一闪,可以从起点BFS到终点,为何不能从终点往起点BFS顺便一次就把F和S全找了。 噗。。。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
#define FK(x) cout<<"["<<x<<"]"<<endl
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define bigfor(x) for(int qq=1;qq<=x;qq++)
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout) const int MX = 1e3+100;
int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
int n,m;
bool vis[MX][MX];
char maps[MX][MX];
struct node {
int x,y,step;
node() {};
node(int xx,int yy,int s):x(xx),y(yy),step(s) {};
}; int BFS(int x,int y) {
node pe(0,0,1e9+10);
node fir(0,0,1e9+10);
memset(vis,0);
queue< node > Q;
while(!Q.empty())Q.pop();
vis[x][y]=1;
int sign=0;
Q.push(node(x,y,0));
while(!Q.empty()) {
node a=Q.front();
Q.pop();
if(maps[a.x][a.y]=='S'){
pe=a;
}
if(a.step>pe.step)return 1;
if(maps[a.x][a.y]=='F'){
return 0;
}
for(int i=0; i<4; i++) {
int xx=a.x+dir[i][0];
int yy=a.y+dir[i][1];
if(xx<0||yy<0||xx>=n||yy>=m||vis[xx][yy]) continue;
if(maps[xx][yy]=='#') continue;
vis[xx][yy]=1;
Q.push(node(xx,yy,a.step+1));
}
}
return 0;
} int main() {
int T;
cin>>T;
bigfor(T) {
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) {
scanf("%s",maps[i]);
}
int flag=0;
int ans=0;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(maps[i][j]=='E') {
ans=BFS(i,j);
flag=1;
}
if(flag)break;
}
if(flag)break;
}
printf("%s\n",ans==1?"Y":"N");
}
return 0;
} /*/ 20
3 7
.......
F..E..S
....... 3 7
.......
.F.E..S
....... 3 7
F......
...E...
.....S. 3 7
......S
E......
......F 3 7
.....S.
E......
...F..F 3 7
.....S.
E...###
.F..#.F /*/

  

 

ACM: Gym 101047E Escape from Ayutthaya - BFS的更多相关文章

  1. bnu 52037 Escape from Ayutthaya

    Escape from Ayutthaya Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on CodeFo ...

  2. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  3. Codeforces Gym 100187E E. Two Labyrinths bfs

    E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...

  4. Gym 101617J Treasure Map(bfs暴力)

    http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...

  5. Gym - 100971J (思维+简单bfs)

    题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...

  6. Gym - 100187E E - Two Labyrinths —— bfs

    题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...

  7. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  8. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  9. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

随机推荐

  1. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

  2. mysql数据库史上最详细起步教程(1)

    本文主要讲解mysql的操作,尽量保证步骤的详细与清晰,希望能帮到大家. 1.登录后进行数据库的创建:create database lf(数据库名);  (一定要记住分号,mysql在语句的结束符就 ...

  3. PhotoSwipe插件的使用

    1.首先引入插件 <link rel="stylesheet" href="css/photoswipe.css"> <link rel=&q ...

  4. 总结初用erlang 时的遇到一些问题

    算起来接触erlang 三个多月快四个月来,期间从零开始看书写erlang代码.修改RabbitMQ.业务开发.系统调优,总算是有点入门了. 最难受的是边学边修改RabbitMQ,开始真心的看不懂,不 ...

  5. 图片根据需要突出div

    1.当代码为: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. MySql: 查看当前登录用户,当前数据库

    mysql> select user();+----------------+| user() |+----------------+| root@localhost |+----------- ...

  7. KRPano资源分析工具使用说明(KRPano XML/JS解密 切片图批量下载 球面图还原 加密混淆JS还原美化)

    软件交流群:571171251(软件免费版本在群内提供) krpano技术交流群:551278936(软件免费版本在群内提供) 最新博客地址:blog.turenlong.com 限时下载地址:htt ...

  8. Camtasia 录屏说明

    准备好要录制的屏幕或网页,在即将播放的位置暂停住. 从开始菜单位置“TechSmith”启动Camtasia Recorder 8,其界面如下所示: 注意,要录制系统声音,须在Recorded inp ...

  9. WPF 如何绘制不规则按钮,并且有效点击范围也是不规则的

    最近在做一个东西,如地图,点击地图上的某一区域,这一区域需要填充成其他颜色.区域是不规则的,而且点击该区域的任一点,都能够变色.普通的按钮只是简单的加载一幅图肯定是不行的.查了很多资料,终于把它搞定了 ...

  10. python之I/O操作

    IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. 比如你打开 ...