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

  • 相关阅读:
    linux下/proc/sysrq-trigger文件的功能
    Windows下bat命令
    转——802.11协议
    转 ——eclipse下进行Python开发 环境配置
    U盘装centos7系统过程
    django框架介绍
    2-事件之间关系(概率论与数理统计学记笔记)
    1-基本概念(概率论与数理统计学习笔记)
    舆情正负面判断

  • 原文地址:https://www.cnblogs.com/Roni-i/p/8761677.html
Copyright © 2011-2022 走看看