uva_11624_Fire!(bfs)

11624 Fire! 

Joe works in a maze. Unfortunately, portions of the maze have caught onfire,and the owner of the maze neglected to create a firee scape plan.Help Joe escape the maze.     

Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.

Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of test cases to follow. The first 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 fire

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 fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input

2

4 4

####

#JF#

# .. #

# .. #

3 3

###

#J .

#. F


Sample Output

3

IMPOSSIBLE


题意:给你一个迷宫,给定起点,其中有些点着火了,而且和人一样往up,down,left,right四个方向拓展,当走出边界即成功。问你能否走出迷宫,若有,输出最小步数,如果没有输出" IMPOSSIBLE "。

分析:一般的bfs。只要把每个火源先加入队列即可。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28833

代码清单:

#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxv = 1000 + 5;

struct Edge{
    int x,y;
    int step;
    int mark;
};

int T,R,C;
int sx,sy,ex,ey;
char str[maxv][maxv];
bool vis[maxv][maxv];
int xy[4][2]={{-1,0},{0,-1},{1,0},{0,1}};

bool judge(Edge p){
    if(p.x>=0&&p.x<R&&p.y>=0&&p.y<C)
        return true;
    return false;
}

Edge w,v;
queue<Edge>q;

void bfs(){
    v.x=sx; v.y=sy; v.step=0; v.mark=1; q.push(v);
    vis[sx][sy]=true;
    while(q.size()){
        w=q.front(); q.pop();
        if(w.mark==1&&(w.x==0||w.x==R-1||w.y==0||w.y==C-1)){
            printf("%d\n",w.step+1);
            return ;
        }
        for(int i=0;i<4;i++){
            v.x=w.x+xy[i][0];
            v.y=w.y+xy[i][1];
            v.step=w.step+1;
            v.mark=w.mark;
            if(judge(v)&&!vis[v.x][v.y]&&str[v.x][v.y]=='.'){
                vis[v.x][v.y]=true;
                q.push(v);
            }
        }
    }
    printf("IMPOSSIBLE\n");
}
int main(){
    scanf("%d",&T);
    while(T--){
        while(q.size()) q.pop();
        memset(vis,false,sizeof(vis));
        scanf("%d%d",&R,&C);
        for(int i=0;i<R;i++){
            scanf("%s",str[i]);
            for(int j=0;j<C;j++){
                if(str[i][j]=='J') { sx=i; sy=j; }
                if(str[i][j]=='F') { w.x=i; w.y=j; w.step=0; w.mark=0; vis[i][j]=true; q.push(w); }
            }
        }
        bfs();
    }return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码解读void bfs() { while (!q.empty()) { Node cur = q.top(); q.pop(); if (cur.box_x == end_x && cur.box_y == end_y) { best = cur.step; flag = true; break; } else for (int i = 0; i < 4; i++) { flag1 = false; memset(visit2, 0, sizeof(visit2)); int x = cur.box_x + dx[i]; int y = cur.box_y + dy[i]; if (x<1 || y<1 || x>n || y>m || board[x][y] == 1) continue; Node next; next.box_x = x; next.box_y = y; next.people_x = cur.box_x; next.people_y = cur.box_y; next.step = cur.step + 1; if (i == 0) if (cur.box_y - 1 > 0) if (board[cur.box_x][cur.box_y - 1] != 'S' && bfs2(cur.box_x, cur.box_y - 1, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x][cur.box_y - 1]) { visit[x][y][cur.box_x][cur.box_y - 1] = 1; q.push(next); } if (i == 1) if (cur.box_y + 1 <= m) if (board[cur.box_x][cur.box_y + 1] != 'S' && bfs2(cur.box_x, cur.box_y + 1, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x][cur.box_y + 1]) { visit[x][y][cur.box_x][cur.box_y + 1] = 1; q.push(next); } if (i == 2) if (cur.box_x - 1 > 0) if (board[cur.box_x - 1][cur.box_y] != 'S' && bfs2(cur.box_x - 1, cur.box_y, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x - 1][cur.box_y]) { visit[x][y][cur.box_x - 1][cur.box_y] = 1; q.push(next); } if (i == 3) if (cur.box_x + 1 <= n) if (board[cur.box_x + 1][cur.box_y] != 'S' && bfs2(cur.box_x + 1, cur.box_y, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x + 1][cur.box_y]) { visit[x][y][cur.box_x + 1][cur.box_y] = 1; q.push(next); } } } }
最新发布
07-14
这段代码是一个广度优先搜索算法的实现,用于寻找最短路径。下面是代码的解读: 1. 首先,定义了一个名为 `bfs` 的函数,没有返回值(void)。 2. 在函数内部使用了一个 while 循环,判断队列 `q` 是否为空。 3. 在每次循环中,取出队首元素 `cur`,并将其从队列中移除。 4. 判断当前节点的箱子位置是否与目标位置相同,如果是,则更新最佳步数 `best`,设置标志位 `flag` 为 true,并跳出循环。 5. 如果当前节点的箱子位置与目标位置不同,则进行下一步的判断。 6. 使用一个 for 循环遍历四个方向(上、下、左、右)。 7. 首先,将一个名为 `flag1` 的布尔变量设为 false。 8. 使用 memset 函数将数组 `visit2` 的元素全部置为 0,该数组可能用于记录访问状态。 9. 根据当前节点 `cur` 的箱子位置和当前方向计算出下一步的位置 `x` 和 `y`。 10. 如果下一步的位置超出了边界或者是障碍物(`board[x][y] == 1`),则继续下一次循环。 11. 创建一个新的节点 `next`,并将下一步的位置赋值给 `next` 的箱子位置。 12. 将当前节点的人的位置赋值给 `next` 的人的位置。 13. 将当前节点的步数加1,并赋值给 `next` 的步数。 14. 根据当前方向的不同,进行不同的判断和操作: - 如果当前方向是向左移动,并且箱子左边的位置不是墙壁(`board[cur.box_x][cur.box_y - 1] != 'S'`),并且调用了一个名为 `bfs2` 的函数,并且当前位置没有被访问过(`!visit[x][y][cur.box_x][cur.box_y - 1]`),则将 `next` 加入队列 `q` 中,并将对应的访问状态设置为已访问。 - 如果当前方向是向右移动,并且箱子右边的位置不是墙壁(`board[cur.box_x][cur.box_y + 1] != 'S'`),并且调用了一个名为 `bfs2` 的函数,并且当前位置没有被访问过(`!visit[x][y][cur.box_x][cur.box_y + 1]`),则将 `next` 加入队列 `q` 中,并将对应的访问状态设置为已访问。 - 如果当前方向是向上移动,并且箱子上边的位置不是墙壁(`board[cur.box_x - 1][cur.box_y] != 'S'`),并且调用了一个名为 `bfs2` 的函数,并且当前位置没有被访问过(`!visit[x][y][cur.box_x - 1][cur.box_y]`),则将 `next` 加入队列 `q` 中,并将对应的访问状态设置为已访问。 - 如果当前方向是向下移动,并且箱子下边的位置不是墙壁(`board[cur.box_x + 1][cur.box_y] != 'S'`),并且调用了一个名为 `bfs2` 的函数,并且当前位置没有被访问过(`!visit[x][y][cur.box_x + 1][cur.box_y]`),则将 `next` 加入队列 `q` 中,并将对应的访问状态设置为已访问。 15. 循环结束后,函数执行完毕。 此处代码片段并不完整,缺少了定义和初始化一些变量的部分,例如队列 `q`、数组 `dx` 和 `dy`、数组 `visit`、数组 `board` 等。同时,函数内部还调用了一个名为 `bfs2` 的函数,但在提供的代码中并没有给出其实现。因此,对于代码的完整性和准确性还需要进一步的了解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值