UVA 11624

/*
Joe works in a maze.  Unfortunately, portions of the maze have
caught on  re, and the owner of the maze neglected to create a  re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on  re, you must determine whether Joe can exit the maze before
the  re reaches him, and how fast he can do it.
Joe and the  re each move one square per minute, vertically or
horizontally (not diagonally).  The  re spreads all four directions
from each square that is on  re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the  re
may enter a square that is occupied by a wall.
Input
The  rst line of input contains a single integer, the number of test
cases to follow.  The  rst line of each test case contains the two
integers
R
and
C
, separated by spaces, with 1

R;C

1000. The
following
R
lines of the test case each contain one row of the maze. Each of these lines contains exactly
C
characters, and each of these characters is one of:

#
, a wall

.
, a passable square

J
, Joe's initial position in the maze, which is a passable square

F
, a square that is on  re
There will be exactly one
J
in each test case.
Output
For each test case, output a single line containing `
IMPOSSIBLE
' if Joe cannot exit the maze before the
 re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
SampleInput
2
44
####
#JF#
#..#
#..#
33
###
#J.
#.F
SampleOutput
3
IMPOSSIBLE

题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,
            其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。
分析:搜索。迷宫中的最短路,首先就会想到bfs;并且bfs利用队列会使状态空间按时间顺序分层。
            而火的扩散过程正好符合这个时间的层次。所以我们会想到,利用两个队列,一个储存人的状态,
            一个储存火的状态。按照时间顺序,先更新火蔓延的节点,再扩展人能到达的节点。
            通过分析,我们发现这两个队列可以合并,只须初始化的时候,按照火节点然后是人的顺序入队即可。
           (节点中加入一个是否是火节点的判断,就可以两种节点按不同的细节处理)
注意:时间是指走出迷宫的时间,到达边界后要加1;初始节点的判断。
*/

# include <iostream>
# include <cstdio>
# include <cstring>
# include <queue>
using namespace std;
const int MAX = 1010;
int m, n;
char map[MAX][MAX];
int fire[MAX][MAX];     //火
int people[MAX][MAX];   //人
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
queue<pair<int, int> > Q;
void BFS_fire()
{
    memset(fire, -1, sizeof(fire));
    while ( !Q.empty() )
    {
        Q.pop();
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            if (map[i][j] == 'F')
            {
                fire[i][j] = 0;
                Q.push(make_pair(i, j));
            }
        }
    }
    while ( !Q.empty() )        //BFS火能烧到的地方,并且记录时间
    {
        pair<int, int> a;
        a = Q.front();
        Q.pop();
        int x = a.first;
        int y = a.second;
        for (int i=0; i<4; i++)
        {
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if (xx<0 || xx>=n || yy<0 || yy>=m) continue;
            if (fire[xx][yy] != -1) continue;        //被烧过
            if (map[xx][yy] == '#') continue;       //有墙
            fire[xx][yy] = fire[x][y] + 1;      //记录时间
            Q.push(make_pair(xx, yy));
        }
    }
}
int BFS_people()
{
    memset(people, -1, sizeof(people));
    while ( !Q.empty() )
    {
        Q.pop();
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<m; j++)
        {
            if (map[i][j] == 'J')
            {
                Q.push(make_pair(i, j));
                people[i][j] = 0;
            }
        }
    }
    while ( !Q.empty() )
    {
        pair<int, int> a;
        a = Q.front();
        Q.pop();
        int x = a.first;
        int y = a.second;
        if (x==0 || y==0 || x==n-1 || y==m-1)
        {
            return people[x][y]+1;
        }
        for (int i=0; i<4; i++)
        {
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if (xx<0 || xx>=n || yy<0 || yy>=m) continue;
            if (people[xx][yy] != -1) continue;      //已经走过
            if (map[xx][yy] == '#') continue;           //有墙
            if (fire[xx][yy] != -1 && people[x][y]+1 >= fire[xx][yy]) continue;     //&&  之前是不能被火烧过  之后是到达的时间不能有火
            people[xx][yy] = people[x][y] + 1;      //更新时间
            Q.push(make_pair(xx, yy));
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d %d", &n, &m);
        //getchar();
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<m; j++)
            {
                cin >> map[i][j];
            }
        }
        BFS_fire();
        int ans = BFS_people();
        if(ans == -1)
        {
            printf("IMPOSSIBLE\n");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值