zoukankan      html  css  js  c++  java
  • 【UVa】[11624]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

    题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动, 其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。 分析:搜索。迷宫中的最短路,首先就会想到bfs;并且bfs利用队列会使状态空间按时间顺序分层。 而火的扩散过程正好符合这个时间的层次。所以我们会想到,利用两个队列,一个储存人的状态, 一个储存火的状态。按照时间顺序,先更新火蔓延的节点,再扩展人能到达的节点。 通过分析,我们发现这两个队列可以合并,只须初始化的时候,按照火节点然后是人的顺序入队即可。 (节点中加入一个是否是火节点的判断,就可以两种节点按不同的细节处理) 本段参考博客:UVa 11624 - Fire! - 小白菜又菜
    #include<stdio.h>
    #include<queue>
    using namespace std;
    char map[1200][1200];
    int cnt[1200][1200];
    int tmove[4]= {1,-1,0,0};
    int inf=99999999;
    struct node {
        int n,m;
    } a[1020*1020];
    int x;
    int H,W;
    int bfs(int n,int m) {
        queue<node>q;
        node t;
        for(int i=0; i<x; i++) {
            t.n=a[i].n,t.m=a[i].m;
            q.push(t);
        }
        t.n=n,t.m=m;
        q.push(t);
        while(!q.empty()) {
            t=q.front();
            if(map[t.n][t.m]=='F') {
                for(int i=0; i<4; i++) {
                    int tn=t.n+tmove[i],tm=t.m+tmove[(i+2)%4];
                    if(tn>=0&&tn<H&&tm>=0&&tm<W&&map[tn][tm]=='.') {
                        map[tn][tm]='F';
                        node temp;
                        temp.n=tn,temp.m=tm;
                        q.push(temp);
                    }
                }
            } else {
                for(int i=0; i<4; i++) {
                    int tn=t.n+tmove[i],tm=t.m+tmove[(i+2)%4];
                    if(tn<0||tn>=H||tm<0||tm>=W)
                        return cnt[t.n][t.m];
                    else if(map[tn][tm]=='.'&&cnt[tn][tm]==inf) {
                        map[tn][tm]='J';
                        cnt[tn][tm]=cnt[t.n][t.m]+1;
                        node temp;
                        temp.n=tn,temp.m=tm;
                        q.push(temp);
                    }
                }
            }
            q.pop();
        }
        return -1;
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%d %d",&H,&W);
            getchar();
            int mH,mW;
            x=0;
            for(int i=0; i<H; i++) {
                for(int j=0; j<W; j++) {
                    cnt[i][j]=inf;
                    map[i][j]=getchar();
                    if(map[i][j]=='F')
                        a[x].n=i,a[x++].m=j;
                    else if(map[i][j]=='J')
                        mH=i,mW=j;
                }
                getchar();
            }
            cnt[mH][mW]=1;
            int res=bfs(mH,mW);
            if(res==-1)
                printf("IMPOSSIBLE
    ");
            else
                printf("%d
    ",res);
        }
        return 0;
    }
    

    题目地址:【UVa】[11624]Fire!

    查看原文:http://www.boiltask.com/blog/?p=1942
  • 相关阅读:
    NVelocity实现违反了LSP法则,使我的一个低级错误排查了一个下午。
    ADO.NET EF 4中 query.Where().Where()和动态组合lambda实现组合查询的不同。
    发现blend4的一个导致崩溃的BUG!!!
    代码回滚:git reset、git checkout和git revert区别和联系
    精确获取函数运行时间,精确到微秒
    在github分支上上传空文件夹
    VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt
    同步github上fork出来的分支
    未能找到任何适合于指定的区域性或非特定区域性的资源。请确保在编译时已将“XXXXX.resources”正确嵌入或链接到程序集“XX”,或者确保所有需要的附属程序集都可加载并已进行了完全签名。
    决定以后把写博客转的主要平台转到cnblogs了
  • 原文地址:https://www.cnblogs.com/BoilTask/p/12569452.html
Copyright © 2011-2022 走看看