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

BFS [uva 11624] Fire!

时间:2014-12-20 15:26:18      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

J - Fire!
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
 

Problem B: Fire!

技术分享Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape 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 Specification

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.

Sample Input

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

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE

Malcolm Sharpe, Ond?ej Lhoták
 
6级之前来一发、交之前我说如果过了、6级就能过、然后AC、保佑我的6级、哈哈~
最开始理解错了、可能有多个火、Wa了好多次。
然后改、遇到一个F就bfs一次,无奈超时。
最后想到可以先把所有F进队、在一次bfs、这样就快多了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 1<<30
#define N 1010

struct JF
{
    int x,y,t;
    JF(){}
    JF(int x,int y,int t):x(x),y(y),t(t){}
};

int n,m;
int jx,jy;
queue<JF> q;
int vis[N][N];
int fire[N][N];                   //fire[i][j]表示火到i,j位置的最短时间
char mpt[N][N];
int dir[4][2]={1,0,-1,0,0,1,0,-1};

bool judge(int x,int y)
{
    if(x<1 || x>n || y<1 || y>m) return 0;
    if(mpt[x][y]==#) return 0;
    return 1;
}

void bfs1()
{
    JF now,next;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            next=now;
            next.t++;
            next.x+=dir[i][0];
            next.y+=dir[i][1];
            if(judge(next.x,next.y))
            {    
                if(next.t<fire[next.x][next.y])
                {
                    fire[next.x][next.y]=next.t;
                    q.push(next);
                }
            }
        }
    }
}

void bfs2()
{
    queue<JF> q;
    memset(vis,0,sizeof(vis));
    JF now,next;
    now.x=jx;
    now.y=jy;
    now.t=0;
    vis[jx][jy]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==1 || now.x==n || now.y==1 || now.y==m)
        {
            cout<<now.t+1<<endl;
            return;
        }
        for(int i=0;i<4;i++)
        {
            next=now;
            next.t++;
            next.x+=dir[i][0];
            next.y+=dir[i][1];
            if(judge(next.x,next.y) && !vis[next.x][next.y] && fire[next.x][next.y]-next.t>=1)
            {
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
    cout<<"IMPOSSIBLE\n";
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",mpt[i]+1);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                fire[i][j]=INF;
                if(mpt[i][j]==F)
                {
                    fire[i][j]=0;
                    q.push(JF(i,j,0));
                }
                else if(mpt[i][j]==J)
                {
                    jx=i;
                    jy=j;
                }
            }
        }
        bfs1();
        bfs2();
    }
    return 0;
}

 

BFS [uva 11624] Fire!

标签:

原文地址:http://www.cnblogs.com/hate13/p/4175394.html

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