博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces Gym 101047E Escape from Ayutthaya BFS
阅读量:6874 次
发布时间:2019-06-26

本文共 4592 字,大约阅读时间需要 15 分钟。

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 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 题意:火会不断的往上下左右蔓延 火的速度和人一样 看人能不能到终点 如果火和人同时到达一个地方 人会gg 做的时候没想到 后面听了菊花之言 才发现可以转换为从终点出发 看先遇到火还是先遇到起点  这样的话时间复杂度会降低很多 而且这道题比较坑的地方就是火不止一把 我们队做的时候完全没发现 wa成狗
 

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define FIN freopen("input.txt","r",stdin);#define FOUT freopen("output.txt","w",stdout);#define INF 0x3f3f3f3f#define INFLL 0x3f3f3f3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;typedef pair
PII;const double PI = acos(-1);const int MAXN = 1e3 + 5;char mp[MAXN][MAXN];int vis[MAXN][MAXN];int T, n, m;int flag;int fx, fy, sx, sy, ex, ey;struct node{ int x, y; int setp; node(int xx, int yy, int ss){ x = xx; y = yy; setp = ss; }};bool check(int x, int y){ return x >= 0 && x < n && y >= 0 && y < m && mp[x][y] != '#' && !vis[x][y];}void bfs(int x, int y){ node c(0, 0, INF); queue
q; q.push(node(x, y, 0)); memset(vis, 0, sizeof(vis)); while(!q.empty()){ node now = q.front(); q.pop(); if(!check(now.x, now.y)) continue; vis[now.x][now.y] = 1; if(mp[now.x][now.y] == 'S'){ c = now; } if(now.setp > c.setp){ flag = 1; break; } if(mp[now.x][now.y] == 'F'){ break; } q.push(node(now.x + 1, now.y, now.setp + 1)); q.push(node(now.x - 1, now.y, now.setp + 1)); q.push(node(now.x, now.y + 1, now.setp + 1)); q.push(node(now.x, now.y - 1, now.setp + 1)); }}int main(){ //FIN scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); for(int i = 0; i < n; i ++) scanf("%s", mp[i]); for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++){ if(mp[i][j] == 'E'){ ex = i; ey = j; } } flag = 0; bfs(ex, ey); if(flag) puts("Y"); else puts("N"); }}

  

 

转载于:https://www.cnblogs.com/Hyouka/p/5801935.html

你可能感兴趣的文章
iptables的四表五链
查看>>
Vue第一天
查看>>
Google V8引擎编译记录
查看>>
VS2015新建asp.net core站点
查看>>
[Head First设计模式]山西面馆中的设计模式——装饰者模式
查看>>
Counting Kangaroos is Fun 求最少可见袋鼠数
查看>>
SQL Access Advisor in Oracle Database 10g
查看>>
又想起Solaris
查看>>
我与前端的二三事
查看>>
Django REST framework+Vue 打造生鲜电商项目(笔记三)
查看>>
51nod加农炮
查看>>
HTML5 学习笔记一(语义化标签、表单)
查看>>
旅游出境登机入住饭店英语
查看>>
Springboot整合mybatis
查看>>
Java面试题集(1-50)
查看>>
Android 常用工具类之 DimenUtil
查看>>
[Noi2016]国王饮水记
查看>>
【AndroidFramework】ATV9遥控器红外模式下,机顶盒在假待机阶段会响应遥控器语音键...
查看>>
学习笔记之Machine Learning Crash Course | Google Developers
查看>>
11-12 元素的属性
查看>>