• Fire! UVA 11624


    http://acm.hust.edu.cn/vjudge/contest/121377#problem/J

    题意: 有一个人 'J' 身处迷宫, 迷宫四周有墙 '#', 还有火的源点 'F',当然,火会向上下左右四个方向蔓延, 问你 'J' 能否逃出这个迷宫。

     

    注意:  火的源点可能不止有一个, 也有可能一个也没有。

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<queue>
    using namespace std;
    typedef long long LL;
    #define oo 0x3f3f3f3f
    #define maxn 1500
    int dir[4][2]={{0, 1},{1, 0},{0, -1},{-1, 0}};
    char maps[maxn][maxn];
    int  v1[maxn][maxn], v2[maxn][maxn];
    int n, m, nfir;
    
    struct node
    {
        int x, y;
    };
    
    node fir[maxn*maxn];
    void BFSFIR()
    {
        queue<node>Q;
    
        for(int i=0; i<nfir; i++)
        {
            Q.push(fir[i]);
            v1[fir[i].x][fir[i].y]=1;
        }
    
        while(Q.size())
        {
            node now, next;
            now = Q.front();
            Q.pop();
    
            for(int i=0; i<4; i++)
            {
                next.x =now.x+dir[i][0];
                next.y = now.y+dir[i][1];
                if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && v1[next.x][next.y]==oo && maps[next.x][next.y]!='#')
                {
                    v1[next.x][next.y] = v1[now.x][now.y]+1;
                    Q.push(next);
                }
            }
        }
    }
    
    int BFS(node p)
    {
        queue<node>Q;
        Q.push(p);
        node now, next;
        v2[p.x][p.y] = 1;
    
        while(Q.size())
        {
            now = Q.front();
            Q.pop();
    
            if((now.x==0 || now.x==n-1 || now.y==0 || now.y==m-1) && v2[now.x][now.y] < v1[now.x][now.y])
                 return v2[now.x][now.y];
    
            for(int i=0; i<4; i++)
            {
                next.x =now.x+dir[i][0];
                next.y = now.y+dir[i][1];
                if(next.x>=0 && next.x<n && next.y>=0 && next.y<m
                && !v2[next.x][next.y] && maps[next.x][next.y]!='#')
                {
                   v2[next.x][next.y] = v2[now.x][now.y]+1;
                    Q.push(next);
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        int T;
        node  s;
    
        scanf("%d",&T);
    
        while(T--)
        {
            nfir = 0;
    
            scanf("%d %d", &n, &m);
    
            for(int i=0; i<n; i++)
            {
                scanf("%s",maps[i]);
                for(int j=0; j<m; j++)
                {
                    v1[i][j] = oo;
                    if(maps[i][j] == 'J')
                        s.x=i,s.y=j;
                    if(maps[i][j] == 'F')
                    {
                        fir[nfir].x=i,fir[nfir].y=j;
                        nfir++;
                    }
                }
            }
    
    
           BFSFIR();
    
           memset(v2, 0, sizeof(v2));
    
           int ans = BFS(s);
    
            if(ans == -1)  printf("IMPOSSIBLE
    ");
            else printf("%d
    ", ans);
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    2407: C语言习题 整数转换成字符串
    2484: 字母图形
    1658: Easier Done Than Said?
    Easier Done Than Said?(应用到的知识)
    1653: Champion of the Swordsmanship
    1614: 五位以内的对称素数
    1612: 最小公倍数
    $ vs $()
    数学
    计算机科学与技术课程
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5687078.html
Copyright © 2020-2023  润新知