zoukankan      html  css  js  c++  java
  • UVA11624(bfs最短路)

    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
    两次bfs,第一次用于确定着火的时间
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    const int MAXN=1005;
    const int INF=0x3fffffff;
    struct Node{
        int a,b,s;
        Node(){}
        Node(int ca,int cb,int cs)
        {
            a=ca,b=cb,s=cs;
        }
    };
    char mp[MAXN][MAXN];
    int vis[MAXN][MAXN];
    int Fire[MAXN][MAXN];
    int n,m;
    int yJ,xJ;
    int dy[4]={0,1,0,-1};
    int dx[4]={1,0,-1,0};
    void bfsF()
    {
        memset(vis,0,sizeof(vis));
        queue<Node> que;
    
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                Fire[i][j]=INF;
                if(mp[i][j]=='F')
                {
                    que.push(Node(i,j,0));
                    vis[i][j]=1;
                }
            }
        
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            Fire[now.a][now.b]=now.s;
            for(int i=0;i<4;i++)
            {
                int ny=now.a+dy[i];
                int nx=now.b+dx[i];
                if(0<=ny&&ny<n&&0<=nx&&nx<m&&mp[ny][nx]!='#'&&!vis[ny][nx])
                {
                    vis[ny][nx]=1;
                    que.push(Node(ny,nx,now.s+1));
                }
                
            }        
        }
    }
    void bfsJ()
    {
        memset(vis,0,sizeof(vis));
        queue<Node> que;
        que.push(Node(yJ,xJ,0));
        vis[yJ][xJ]=1;
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            if(now.a==0||now.a==n-1||now.b==0||now.b==m-1)
            {
                printf("%d
    ",now.s+1);
                return ;
            }
            for(int i=0;i<4;i++)
            {
                int ny=now.a+dy[i];
                int nx=now.b+dx[i];
                if(0<=ny&&ny<n&&0<=nx&&nx<m&&!vis[ny][nx]&&mp[ny][nx]!='#'&&now.s+1<Fire[ny][nx])
                {
                    vis[ny][nx]=1;
                    que.push(Node(ny,nx,now.s+1));
                }
            }
        }
        printf("IMPOSSIBLE
    ");
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
            {
                scanf("%*c");
                for(int j=0;j<m;j++)
                {
                    scanf("%c",&mp[i][j]);
                    if(mp[i][j]=='J')
                    {
                        yJ=i;
                        xJ=j;
                    }    
                }
            }
            bfsF();
            bfsJ();
        }
            
        return 0;
    }
  • 相关阅读:
    汉字的几何中心
    输入带空格的string类型字符串 c++
    cin函数返回值
    win7玩游戏两边有黑条
    unresolved external symbol __imp__WSACleanup@0
    sizeof和strlen
    printf 函数返回值
    clone() 操作系统实验
    unsigned char 与 char
    【转】向字符数组输入空格的方法
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5260583.html
Copyright © 2011-2022 走看看