Barareh on Fire(BFS)

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.

输入

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.

输出

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.

思路:

先bfs一边处理出每个点着火的时间,然后从起点bfs找最短路,用f[][]判断是否可走

注意因为存在一开始没火的情况f[][]数组初始化为无穷大表示不会着火(初始化为0,wa了几发qwq)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=110,INF=0x3f3f3f3f;
int n,m,k;
char g[N][N];
int f[N][N],d[N][N];
int nx[]={1,0,-1,0,1,1,-1,-1},ny[]={0,1,0,-1,-1,1,1,-1};
bool st[N][N];
queue<PII> q;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    while(1)
    {
        cin>>n>>m>>k;
        if(!n&&!m&&!k) break;
        memset(f,0x3f,sizeof f);
        memset(d,0,sizeof d);
        PII ss,tt;
        while(q.size()) q.pop();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                cin>>g[i][j];
                if(g[i][j]=='f') {f[i][j]=1;q.push({i,j});}
                if(g[i][j]=='s') ss={i,j};
                if(g[i][j]=='t') tt={i,j};
            }
        while(q.size())
        {
            auto t=q.front();
            q.pop();
            int x=t.first,y=t.second;
            for(int i=0;i<8;i++)
            {
                int xx=x+nx[i],yy=y+ny[i];
                if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&f[xx][yy]==INF)
                {
                    f[xx][yy]=f[x][y]+k;
                    q.push({xx,yy});
                }
            }
        }
        q.push(ss);
        d[ss.first][ss.second]=1;
        while(q.size())
        {
            auto t=q.front();
            q.pop();
            int x=t.first,y=t.second;
            for(int i=0;i<4;i++)
            {
                int xx=x+nx[i],yy=y+ny[i];
                if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&d[x][y]<f[xx][yy]-1&&!d[xx][yy])
                {
                    d[xx][yy]=d[x][y]+1;
                    //if(xx==tt.first&&yy==tt.second) break;
                    q.push({xx,yy});
                }
            }
        }
        if(d[tt.first][tt.second]) cout<<d[tt.first][tt.second]-1<<'\n';
        else cout<<"Impossible\n";
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值