I - 超凡大师 CSU - 2031: Barareh on Fire

本文介绍了一种基于火场蔓延模型的逃生路径规划算法。该算法通过BFS搜索技术来模拟火势扩散过程,并计算出人物从起点到终点的安全路径及时长。适用于虚拟环境中的人物逃生模拟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I - 超凡大师 CSU - 2031 

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

Hint

题意:f火,s起点,t终点,火每K秒向八个方向蔓延,人每秒走一步(只能走东南西北),求最短用时 

先bfs求火走到某点的用时,再bfs人走的路

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>

typedef long long LL;
typedef long double LD;
using namespace std;
const int  maxn=111;
char ma[maxn][maxn];//初始地图
int vis[maxn][maxn];//人
int fvis[maxn][maxn];//火
int ff[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int f[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//int f[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};
int N,M,K;
struct node
{
    int x,y;
    int step;
    void setxys(int xx,int yy,int ss)
    {
        x=xx;
        y=yy;
        step=ss;
    }
    friend bool operator <(node a,node b)
    {
        return a.step>b.step;
    }
};
queue<node>fire;
queue<node>q;
bool OK(int x,int y)
{
    if(x>=0&&y>=0&&x<N&&y<M)
        return true;
    return false;
}
void bfs(node st,node en)
{
    q.push(st);
    vis[st.x][st.y]=1;
    while(!q.empty())
    {
        node t=q.front();
        q.pop();
        //printf("*****%d %d %d\n",t.x,t.y,t.step);
        if(t.x==en.x&&t.y==en.y)
        {
            printf("%d\n",t.step);
            return;
        }
        for(int i=0;i<4;i++)
        {
            int xx=t.x+f[i][0];
            int yy=t.y+f[i][1];
            if(OK(xx,yy)&&vis[xx][yy]==0&&(fvis[xx][yy]>t.step+1||fvis[xx][yy]==-1))
            {
                //printf("%d %d %d %d \n",xx,yy,t.step+1,fvis[xx][yy]);
                q.push((node){xx,yy,t.step+1});
                vis[xx][yy]=1;
            }
        }
    }
    printf("Impossible\n");
}
void init(node &st,node &en)//bfs把火走到某点的时间记录下来
{
    memset(vis,0,sizeof(vis));
    memset(fvis,-1,sizeof(fvis));
    while(!q.empty())q.pop();
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            if(ma[i][j]=='s')
                st.setxys(i,j,0);
            if(ma[i][j]=='t')
                en.setxys(i,j,0);
            if(ma[i][j]=='f')
            {
                fvis[i][j]=0;
                fire.push((node){i,j,0});
            }

        }
    }
    while(!fire.empty())
    {
        node t=fire.front();
        fire.pop();
        for(int i=0;i<8;i++)
        {
            int xx=t.x+ff[i][0];
            int yy=t.y+ff[i][1];
            if(OK(xx,yy)&&(fvis[xx][yy]>t.step+K||fvis[xx][yy]==-1))
            {
                fire.push((node){xx,yy,t.step+K});
                fvis[xx][yy]=t.step+K;
            }
        }
    }
}
int main()
{
    node st,en;
    while(~scanf("%d%d%d",&N,&M,&K)&&(N+M+K))
    {
        for(int i=0;i<N;i++)
            scanf("%s",ma[i]);
        init(st,en);

        bfs(st,en);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值