zoukankan      html  css  js  c++  java
  • 2153: 2018湖南多校第二场-20180407(网络同步赛)

    E(2031): Barareh on Fire

    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
    #include <iostream>
    #include <queue>
    #include <memory.h>
    #include <stdio.h>
    #define MAX 1100
    using namespace std;
    #define ll long long
    char mp[150][150];
    int fx[10]= {-1,0,1,0,-1,1,-1,1};
    int fy[10]= {0,1,0,-1,1,1,-1,-1};
    int t,n,m, k;
    int x,y;
    int x2,y2;
    int x3,y3;
    bool flag;
    int tim[150][150];
    int vis[150][150];
    int ans[150][150];
    
    struct node
    {
        int x,y;
    }p;
    queue<node>q;
    void bfs1()
    {
    
        while(!q.empty())
        {
            p=q.front();
            q.pop();
            vis[p.x][p.y]=1;
            for(int i=0; i<8; i++) //火势八方向蔓延
            {
                int xx=p.x+fx[i];
                int yy=p.y+fy[i];
                if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                {
                    if(vis[xx][yy]!=1)q.push((node){xx,yy});
                    if(tim[xx][yy]>tim[p.x][p.y]+k)
                        tim[xx][yy]=tim[p.x][p.y]+k; //all its 8 vertex-neighboring cells will catch fire at time x + k
                    vis[xx][yy]=1;
    
                }
            }
        }
    }
    void bfs2(int x,int y)
    {
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        queue<node>q;
        vis[x][y]=1;
        q.push((node){x,y});
        while(!q.empty())
        {
            p=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                int xx=p.x+fx[i];
                int yy=p.y+fy[i];
                if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0)//left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1
                {
                    vis[xx][yy]=1;
                    if(mp[xx][yy]=='-') //空地
                    {
                        if(ans[p.x][p.y]+1 < tim[xx][yy]) 
                        {
                            q.push((node){xx,yy});
                            ans[xx][yy]=ans[p.x][p.y]+1;
                        }
                    }
                    if(mp[xx][yy]=='t')
                    {
                        if(ans[p.x][p.y]+1 < tim[xx][yy])//当人和火同时到门的时候,算火胜利
                        {
                            ans[xx][yy]=ans[p.x][p.y]+1;
                            flag=true;
                        }
                        return ;
                    }
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&k),n,m,k)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                    tim[i][j] = 9999999;
                //printf("
    ");
            }
            memset(vis,0,sizeof(vis));
            memset(ans,0,sizeof(ans));
            flag=false;
    
            for(int i=1; i<=n; i++)
            {
                scanf("%s",mp[i]+1);
                for(int j=1; j<=m; j++)
                {
                    if(mp[i][j]=='s')
                    {
                        x=i;
                        y=j;
                    }
                    if(mp[i][j]=='f')
                    {
                        tim[i][j]=0;
                        x2=i;
                        y2=j;
                        q.push(node{x2,y2});
    
                    }
                    if(mp[i][j]=='t')
                    {
                        x3=i;
                        y3=j;
                    }
                }
            }
            bfs1();
            /*
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                    cout<<tim[i][j]<<" ";
                printf("
    ");
            }*/
    
            bfs2(x,y);
            if(flag)
                printf("%d
    ",ans[x3][y3]);
            else
                printf("Impossible
    ");
        }
    }
    两次BFS

  • 相关阅读:
    数据库_初学
    数据库—修改表的列
    做一个导航栏(bootstrap)
    几个比较常用的特效
    当、你想给一个目标挂上一个事件时
    图片轮播的几个小程序
    JS练习题 ( 下拉菜单;好友选中输入)
    Bootstrap 按钮和折叠插件
    Bootstrap 轮播插件
    Bootstrap 弹出框和警告框插件
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8761677.html
Copyright © 2011-2022 走看看