zoukankan      html  css  js  c++  java
  • UVA11624 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
    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
    思想是 火走一步,然后人再走一步。需要注意的是有可能不只一个火;
    我的方法比较麻烦,但是很容易理解;

    #include<iostream>
    #include<string.h>
    #include<queue>
    using namespace std;
    char map[1005][1005];//地图
    int vis1[1005][1005];//记录人的路径
    int vis2[1005][1005];//记录火的路径
    int go[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};//四个方向搜索
    int ji,jj;
    int n,m;
    struct node1//用来存储人的路径
    {
        int si,sj,ans;
    };
    struct node2//这个是用来转换人的路径的
    {
        int si2,sj2,ans2;
    };
    struct node3//用来存储火的路径
    {
        int di,dj;
    };
    struct node4//这个用来转化火的路径的
    {
        int di2,dj2;
    };
    node2 now2,next2;
    queue<node2>s2;
    node4 now4,next4;
    queue<node4>s4;
    node1 now1,next1;
    queue<node1>s1;
    node3 now3,next3;
    queue<node3>s3;
    int bfs(int si,int sj)
    {
        now1.si=si,now1.sj=sj,now1.ans=0;//把人的起点压入队列
        s1.push(now1);
        while(!s1.empty())//如果人没路可走,跳出循环
        {
            while(!s3.empty())//如果火走完了,跳出
            {
                now3=s3.front();
                for(int u3=0; u3<4; u3++)//四个方向广搜
                {
                    int x3=now3.di+go[u3][0],y3=now3.dj+go[u3][1];
                    if(x3>=0&&y3>=0&&x3<n&&y3<m&&map[x3][y3]!='#'&&!vis2[x3][y3])//能着火压入队列
                    {
                        vis2[x3][y3]=1;
                        map[x3][y3]='F';
                        next4.di2=x3,next4.dj2=y3;//压入第四个队列
                        s4.push(next4);
                    }
                }
                s3.pop();
            }
            while(!s4.empty())//从第四个队列转移到第三个队列
            {
                now4=s4.front();
                int x4=now4.di2,y4=now4.dj2;
                next3.di=x4,next3.dj=y4;
                s3.push(next3);
                s4.pop();
            }
            while(!s1.empty())//如果人走完了,跳出
            {
                now1=s1.front();
                for(int u1=0; u1<4; u1++)
                {
                    int x1=now1.si+go[u1][0],y1=now1.sj+go[u1][1];
                    if(x1==0||y1==0||x1==n-1||y1==m-1)
                    {
                        if(map[x1][y1]=='.')
                        {
                            return now1.ans+2;
                        }
                    }
                    if(x1>=0&&y1>=0&&x1<n&&y1<m&&map[x1][y1]=='.'&&!vis1[x1][y1])
                    {
                        vis1[x1][y1]=1;
                        next2.si2=x1,next2.sj2=y1,next2.ans2=now1.ans+1;
                        s2.push(next2);//人能走,压入第二个队列
                    }
                }
                s1.pop();
            }
            while(!s2.empty())//从第二个队列转移到第一个队列
            {
                now2=s2.front();
                int x2=now2.si2,y2=now2.sj2;
                next1.si=x2,next1.sj=y2,next1.ans=now2.ans2;
                s1.push(next1);
                s2.pop();
            }
        }
        return 0;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            memset(vis1,0,sizeof(vis1));
            memset(vis2,0,sizeof(vis2));
            cin>>n>>m;
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    cin>>map[i][j];
                    if(map[i][j]=='J')//记录人的起点
                    {
                        ji=i;
                        jj=j;
                        vis1[ji][jj]=1;
                    }
                    if(map[i][j]=='F')//找到火并压入队列
                    {
                        now3.di=i;
                        now3.dj=j;
                        vis2[now3.di][now3.dj]=1;
                        s3.push(now3);
                    }
                }
            }
            if(ji==0||jj==0||ji==n-1||jj==m-1)
            {
                cout<<"1"<<endl;
                while(!s3.empty())//清空队列,我在这找bug找了好久好久,,,,,,,,
                {
                    s3.pop();
                }
                while(!s1.empty())
                {
                    s1.pop();
                }
                while(!s2.empty())
                {
                    s2.pop();
                }
                while(!s4.empty())
                {
                    s4.pop();
                }
            }
            else
            {
                int q=bfs(ji,jj);
                if(q==0)
                {
                    cout<<"IMPOSSIBLE"<<endl;
                }
                else
                {
                    cout<<q<<endl;
                }
                while(!s3.empty())
                {
                    s3.pop();
                }
                while(!s1.empty())
                {
                    s1.pop();
                }
                while(!s2.empty())
                {
                    s2.pop();
                }
                while(!s4.empty())
                {
                    s4.pop();
                }
            }
        }
        return 0;
    }
    "No regrets."
  • 相关阅读:
    牛客练习赛44 A 小y的序列 (模拟,细节)
    牛客假日团队赛10 L 乘积最大 (dp,大数)
    三分查找
    几何基础知识点
    POJ 2318 TOYS
    UVA 11916 Emoogle Grid(大步小步算法(解模方程对数) 快速幂 模的逆)
    UVA 11426 GCD
    Aladdin and the Flying Carpet(算术基本定理)
    算术基本定理
    数论总结帖
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215178.html
Copyright © 2011-2022 走看看