(BFS)福州大学12th,Problem 2196 Escape

<div class="problem_title" style="font-size: 24px; font-family: Tahoma, Helvetica, Arial, sans-serif; line-height: 25px; text-align: -webkit-center; background-color: rgb(219, 218, 252);"><strong>Problem 2196 Escape</strong></div><div class="problem_desc" style="color: green; text-align: center; font-family: Tahoma, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 25px;"><h3>Accept: 76    Submit: 393
Time Limit: 1000 mSec    Memory Limit : 32768 KB</h3></div><div class="problem_content" style="font-family: Tahoma, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 25px;"><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Problem Description</h2><div class="pro_desc" style="text-indent: 28px;">小明进入地下迷宫寻找宝藏,找到宝藏后却发生地震,迷宫各处产生岩浆,小明急忙向出口处逃跑。如果丢下宝藏,小明就能迅速离开迷宫,但小明并不想轻易放弃自己的辛苦所得。所以他急忙联系当程序员的朋友你(当然是用手机联系),并告诉你他所面临的情况,希望你能告诉他是否能成功带着宝藏逃脱。</div><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Input</h2><div class="pro_desc" style="text-indent: 28px;"><p>有多组测试数据。</p><p>每组测试数据第一行是一个整数T,代表接下去的例子数。(0<=T<=10)</p><p>接下来是T组例子。</p><p>每组例子第一行是两个整数N和M。代表迷宫的大小有N行M列(0<=N,M<=1000)。</p><p>接下来是一个N*M的迷宫描述。</p><p>S代表小明的所在地。</p><p>E代表出口,出口只有一个。</p><p>.代表可以行走的地方。</p><p>!代表岩浆的产生地。(这样的地方会有多个,其个数小于等于10000)</p><p>#代表迷宫中的墙,其不仅能阻挡小明前进也能阻挡岩浆的蔓延。</p><p>小明携带者宝藏每秒只能向周围移动一格,小明不能碰触到岩浆(小明不能和岩浆处在同一格)。</p><p>岩浆每秒会向四周不是墙的地方蔓延一格。</p><p>小明先移动完成后,岩浆才会蔓延到对应的格子里。</p><p>小明能移动到出口,则小明顺利逃脱。</p></div><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Output</h2><div class="pro_desc" style="text-indent: 28px;">每组测试数据输出只有一行“Yes”或者“No”。 “Yes”代表小明可以成功逃脱。否则输出“No”。</div><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Sample Input</h2><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">3</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">5 5</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">....!</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">S....</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">#....</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">!#...</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">#E...</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">2 2</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">S.</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">!E</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">2 2</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">SE</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">!.</div><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Sample Output</h2><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">Yes</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">No</div><div class="data" style="font-family: 'Courier New', Courier, monospace; font-size: 16px; white-space: pre; line-height: 20px; border: 1px solid rgb(173, 173, 173); background-color: rgb(224, 224, 224);">Yes</div><h2><img src="http://acm.fzu.edu.cn/image/prodesc.gif" style="border: 0px;" alt="" /> Source</h2>福州大学第十二届程序设计竞赛</div><br style="font-family: Tahoma, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 25px; text-align: -webkit-center;" />
/*
题目:G Escape
链接:http://acm.fzu.edu.cn/problem.php?pid=2196
题意:问小明能不能顺利到达出口。小明和岩浆每秒都会向四周移动一格(“小明先于岩浆”)。
分析:BFS。先BFS预处理每个格子岩浆到达的时间(令其为fire[i][j]);
    再BFS小明到达终点的路径,对每一个格子
    ①如果它是终点,则要求小明在<=fire[i][j]的时间内到达;
    ②如果它不是终点,则要求小明在<fire[i][j]的时间内到达;
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 1005;
const int INF = 0xfffffff;
struct NODE
{
    int x, y;
    int step;
}st, t, en, u;
queue<NODE> q,qv;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int N, M;
char a[maxn][maxn];
bool vis[maxn][maxn],vis0[maxn][maxn];
int fire[maxn][maxn];//记录岩浆到达该格子的最早时间
bool Flag;//标记小明是否能成功逃脱
void init()
{
    memset(vis, false, sizeof(vis));
    memset(vis0, false, sizeof(vis0));
    while (!q.empty())   q.pop();
    while (!qv.empty())   qv.pop();
    Flag = false;
}
void BFS0()
{
    while (!qv.empty())
    {
        u = qv.front();   qv.pop();
        for (int i = 0; i < 4; i++)
        {
            t.x = u.x + dx[i];
            t.y = u.y + dy[i];
            t.step = u.step + 1;
            if (vis0[t.x][t.y]) continue;
            if (t.x<0 || N <= t.x || t.y<0 || M <= t.y) continue;
            if (a[t.x][t.y] == '#') continue;
 
            vis0[t.x][t.y] = true;
            qv.push(t);
            fire[t.x][t.y] = t.step;
        }
    }
}
void BFS()
{
    vis[st.x][st.y];
    q.push(st);
    while (!q.empty())
    {
        u = q.front(); q.pop();
        for (int i = 0; i<4; i++)
        {
            t.x = u.x + dx[i];
            t.y = u.y + dy[i];
            t.step = u.step + 1;
            if (vis[t.x][t.y]) continue;
            if (t.x<0 || N <= t.x || t.y<0 || M <= t.y) continue;
            if (a[t.x][t.y] == '#') continue;
            if (t.x == en.x&&t.y == en.y&&t.step<=fire[t.x][t.y])//小明到达终点
            {
                Flag = true;
                return;
            }
            if (t.step >= fire[t.x][t.y]) continue;//岩浆先于小明到该格子,或与小明同时到达该格子
             
            vis[t.x][t.y] = true;
            q.push(t);
        }
    }
}
/*------------------------------------*/
int main()
{
    //freopen("e:\\input.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        init();
        scanf("%d%d", &N, &M);
        for (int i = 0; i<N; i++)
            scanf("%s", a[i]);
        for (int i = 0; i<N; i++)
        for (int j = 0; j<M; j++)
        {
            fire[i][j] = INF;
            if (a[i][j] == 'S')//找起点
            {
                st.x = i; st.y = j; st.step = 0;
            }
            else if (a[i][j] == 'E')//找终点
            {
                en.x = i; en.y = j;
            }
            else if (a[i][j] == '!')//岩浆入队列qv
            {
                t.x = i; t.y = j; t.step = 0;
                qv.push(t);
                fire[i][j] = 0;
            }
        }
        /*------------------------------------*/
        BFS0();
        BFS();
        printf("%s\n", Flag ? "Yes" : "No");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值