您的位置:首页 > 其它

uva 11624(BFS)

2013-05-30 11:21 351 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

Problem B:Fire!

11624(BFS)" TITLE="uva 11624(BFS)" />Joeworks in a maze. Unfortunately, portions of the maze have caught onfire, and the owner of the maze neglected to create a fire escapeplan. Help Joe escape the maze.Given Joe's location in the maze and which squares of the maze areon fire, you must determine whether Joe can exit the maze beforethe fire reaches him, and how fast he can do it.Joeand the fire each move one square per minute, vertically orhorizontally (not diagonally). The fire spreads all four directionsfrom each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the firemay enter a square that is occupied by a wall.

InputSpecification

Thefirst line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R[/i] and C[/i],separated by spaces, with 1<= R[/i],C[/i] <=1000. The following R[/i] linesof the test case each contain one row of the maze. Each of theselines contains exactly C[/i] characters,and each of these characters is one of:#, a wall., a passable squareJ, Joe's initial position in the maze, which is apassable squareF, a square that is on fireTherewill be exactly one J ineach test case.

SampleInput

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

OutputSpecification

Foreach test case, output a single linecontaining IMPOSSIBLE ifJoe cannot exit the maze before the fire reaches him, or an integergiving the earliest time Joe can safely exit the maze, inminutes.

Output forSample Input

3
IMPOSSIBLE
题意:帮助J走出有火的迷宫。J每分钟可以走向上下左右四个方向,火也一样。
分析:广搜,在加入Joe的起始位置之前,先把所有着火点加入队列。Joe只能到达没有障碍和火势控制的格子。
bfs1 就是说 以着火点 bfs 标记一个数组 就是每个点着火的最小的时间bfs2 就是看到达那个点的时候 时间是不是小于着火时间
具体详解参考训练指南307
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
int fire[1010][1010];
int dis[1010][1010];//保存时间
int r,c;//行列
char map[1010][1010];//建图
int sx,sy;//
int fx,fy;//
int dir[4][4]={{-1,0},{1,0},{0,-1},{0,1}};//方向
//判断坐标是否合法的
bool judge(int x,int y){
    if(x>=0&&x<r&&y>=0&&y<c){
        return true;
    }
    return false;
}
//判断是否到边界,即逃出迷宫了
bool is_ok(int x,int  y){
    if(x==0||x==r-1||y==0||y==c-1){
        return true;
    }
    return false;
}
void bfs1(){
    int i,j;
    queue<int> q;
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            if(map[i][j]=='F'){
                q.push(i*c+j);
                fire[i][j]=0;
            }
        }
    }
    int x,y;
    int nx,ny;
    while(!q.empty()){
        int pre;
        pre=q.front();
        q.pop();
        x=pre/c; y=pre%c;
        for(i=0;i<4;i++){
            nx=x+dir[i][0];//下一个点的坐标
            ny=y+dir[i][1];
            if(!judge(nx,ny)){
                continue;
            }
            if(map[nx][ny]=='#'){
                continue;
            }
            if(fire[x][y]+1<fire[nx][ny]){
                fire[nx][ny]=fire[x][y]+1;
                q.push(nx*c+ny);
            }
        }
    }
}
void bfs2(){
    int i;
    queue<int> q;
    memset(dis,-1,sizeof(dis));
    dis[sx][sy]=0;
    q.push(sx*c+sy);
    int nx,ny;
    int x,y;
    int pre;
    while(!q.empty()){
        pre=q.front();
        q.pop();
        x=pre/c,y=pre%c;
        if(is_ok(x,y)){
            printf("%d\n",dis[x][y]+1);
            return;
        }
        for(i=0;i<4;i++){
            nx=x+dir[i][0];
            ny=y+dir[i][1];
            if(!judge(nx,ny)){
                continue;
            }
            if(dis[nx][ny]!=-1){
                continue;
            }
            if(map[nx][ny]=='#'||map[nx][ny]=='F'){
                continue;
            }
            if(dis[x][y]+1>=fire[nx][ny]){
                continue;
            }
            dis[nx][ny]=dis[x][y]+1;
            q.push(nx*c+ny);
        }
    }
    printf("IMPOSSIBLE\n");
}
int main(){
    int T;
    scanf("%d",&T);
    int i,j;
    while(T--){
        scanf("%d%d",&r,&c);
        for(i=0;i<r;i++){
            scanf("%s",map[i]);
            for(j=0;j<c;j++){
                fire[i][j]=inf;
                if(map[i][j]=='J'){
                    sx=i; sy=j;
                }
            }
        }
        bfs1();
        bfs2();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: