【UVa】[11624]Fire!

Problem Description

Loading...

Problem Analysis

Problem: in a planar maze there is a person, and some cells of the maze are on fire. Every unit of time, the fire and the person can only move to an adjacent cell; some spaces are occupied by walls. What is the fastest time in which the person can leave the maze without being burned?

Analysis: search. For a Shortest Path in a maze, the first thing that comes to mind is BFS; and by using a queue, BFS layers the state space in time order.

The fire’s spread process happens to match this time layering. So we think of using two queues: one storing the person’s states, one storing the fire’s states. In time order, first update the nodes the fire spreads to, then expand the nodes the person can reach.

Through analysis, we find that these two queues can be merged: at initialization, simply enqueue the fire nodes first and then the person.

(Add a check on whether a node is a fire node, and the two kinds of nodes can be handled with different details.)

This analysis references the blog: UVa 11624 - Fire! - Xiaobaicaiyoucai

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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\n");
        else
            printf("%d\n",res);
    }
    return 0;
}

Problem link: Puzzle - UVA 227 - Virtual Judge

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...