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 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

34 5....S..........F...E4 4...S........F..E3 4###S####E..F

Output

YNN

题意:火会不断的往上下左右蔓延 火的速度和人一样 看人能不能到终点 如果火和人同时到达一个地方 人会gg

做的时候没想到 后面听了菊花之言 才发现可以转换为从终点出发 看先遇到火还是先遇到起点  这样的话时间复杂度会降低很多而且这道题比较坑的地方就是火不止一把 我们队做的时候完全没发现 wa成狗

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
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|1
typedef long long LL;
typedef pair<int,int> 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<node> 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");

    }

}

  

				
时间: 2024-10-09 23:35:55

CodeForces Gym 101047E Escape from Ayutthaya BFS的相关文章

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 100187E E. Two Labyrinths bfs

E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/E Description A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free

bnu 52037 Escape from Ayutthaya

Escape from Ayutthaya Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. Original ID: 101047E64-bit integer IO format: %I64d      Java class name: (Any) Input/Output: standard input/output Ayutthaya was one of the

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much