• CSU 2031


    2031: Barareh on Fire

            Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitted: 429     Solved: 119    


    Description

    The Barareh village is on fire due to the attack of the virtual enemy. Several places are already on fire and the fire is spreading fast to other places. Khorzookhan who is the only person remaining alive in the war with the virtual enemy, tries to rescue himself by reaching to the only helicopter in the Barareh villiage. Suppose the Barareh village is represented by an n × m grid. At the initial time, some grid cells are on fire. If a cell catches fire at time x, all its 8 vertex-neighboring cells will catch fire at time x + k. If a cell catches fire, it will be on fire forever. At the initial time, Khorzookhan stands at cell s and the helicopter is located at cell t. At any time x, Khorzookhan can move from its current cell to one of four edge-neighboring cells, located at the left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1. Note that each move takes one second. Your task is to write a program to find the shortest path from s to t avoiding fire.

    Input

    There are multiple test cases in the input. The first line of each test case contains three positive integers n, m and k (1 ⩽ n,m,k ⩽ 100), where n and m indicate the size of the test case grid n × m, and k denotes the growth rate of fire. The next n lines, each contains a string of length m, where the jth character of the ith line represents the cell (i, j) of the grid. Cells which are on fire at time 0, are presented by character “f”. There may exist no “f” in the test case. The helicopter and Khorzookhan are located at cells presented by “t” and “s”, respectively. Other cells are filled by “-” characters. The input terminates with a line containing “0 0 0” which should not be processed.

    Output

    For each test case, output a line containing the shortest time to reach t from s avoiding fire. If it is impossible to reach t from s, write “Impossible” in the output.

    Sample Input

    7 7 2
    f------
    -f---f-
    ----f--
    -------
    ------f
    ---s---
    t----f-
    3 4 1
    t--f
    --s-
    ----
    2 2 1
    st
    f-
    2 2 2
    st
    f-
    0 0 0
    

    Sample Output

    4
    Impossible
    Impossible
    1


    两遍BFS处理,真的考验码力
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    int n,m,k,tim[105][105],vis[105][105];
    int ddx[8]= {0,0,1,-1,1,1,-1,-1};
    int ddy[8]= {1,-1,0,0,1,-1,1,-1};
    int dx[4]= {0,0,1,-1};
    int dy[4]= {1,-1,0,0};
    char pic[105][105];
    struct node {
        int x,y,step;
    };
    void bfs1() {
        queue<node>p;
        node b,now,next;
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(pic[i][j]=='f') {
                    tim[i][j]=0;
                    b.x=i;
                    b.y=j;
                    b.step=0;
                    p.push(b);
                }
            }
        }
        while(!p.empty()) {
            now=p.front();
            p.pop();
            for(int d=0; d<8; d++) {
                next=now;
                next.x+=ddx[d];
                next.y+=ddy[d];
                if(next.x<0||next.x>=n||next.y<0||next.y>=m) continue;
                next.step=now.step+k;
                if(next.step<tim[next.x][next.y]) {
                    tim[next.x][next.y]=next.step;
                    p.push(next);
                }
            }
        }
    }
    void bfs2(int xx,int yy) {
        queue<node>q;
        node b,now,next;
        b.x=xx;
        b.y=yy;
        b.step=0;
        vis[xx][yy]=1;
        q.push(b);
        while(!q.empty()) {
            now=q.front();
            q.pop();
            if(pic[now.x][now.y]=='t') {
                printf("%d
    ",now.step);
                return;
            }
            for(int d=0; d<4; d++) {
                next=now;
                next.x+=dx[d];
                next.y+=dy[d];
                next.step++;
                if(next.x<0||next.x>=n||next.y<0||next.y>=m) continue;
                if(vis[next.x][next.y]!=1&&next.step<tim[next.x][next.y]) {
                    vis[next.x][next.y]=1;
                    q.push(next);
                }
            }
        }
        printf("Impossible
    ");
    }
    int main() {
        while(scanf("%d%d%d",&n,&m,&k) !=EOF) {
            if(n==0||m==0||k==0) break;
            getchar();
            memset(tim,0x3f3f3f3f,sizeof(tim));
            memset(vis,0,sizeof(vis));
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    scanf("%c",&pic[i][j]);
                }
                getchar();
            }
            bfs1();
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    if(pic[i][j]=='s') {
                        bfs2(i,j);
                        break;
                    }
                }
            }
        }
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    正则表达式
    jdbc,链接调用数据库的方法——例题
    python字符串操作
    python条件判断
    python运算符
    python中输入输出
    python关键字与标识符
    python变量
    python注释
    安装django报错一直安装不上?
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9189194.html
Copyright © 2020-2023  润新知