码迷,mamicode.com
首页 > 其他好文 > 详细

uva_11624_Fire!(bfs)

时间:2015-04-28 22:59:05      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:algorithm   acm   uva   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;
}


uva_11624_Fire!(bfs)

标签:algorithm   acm   uva   bfs   

原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45341621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!