2031: Barareh on Fire

2031: Barareh on Fire

                Time Limit: 3 Sec     Memory Limit: 512 Mb     

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

Hint

Source

ATRC2017

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031

题解:本题是BFS搜索的典型题,用2次BFS,第一次BFS将f(火)蔓延的时间记录下来,第二次BFS将第一次BFS记录下来的time[ ]用来判断s到达一个可走位置(本题为-)时,火势是否已经扩展到了此处,每一个位置对其4个方向进行搜索。具体看如下代码:

AC代码:

#include<cstring>
#include<cmath>
#include<queue>
#include<iostream>
using namespace std;
const int inf=0x3f3f3f3f;
char s[105][105];
int t[105][105];
int vis[105][105];
int fx[]={1,-1,0,0,1,1,-1,-1};      //火势蔓延的8个方向 
int fy[]={0,0,1,-1,1,-1,1,-1};
int px[]={1,-1,0,0};        //人可走的4个方向 
int py[]={0,0,1,-1};
int n,m,k,sx,sy;
struct node
{
    int x,y,step;
}r,p,q;
queue<node> Q;
void bfs_fire()
{
    int i,j;
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(i=0;i<8;i++)
        {
            q=p;
            q.x+=fx[i];
            q.y+=fy[i];
            q.step+=k;
            if(q.x<0||q.y<0||q.x>=n||q.y>=m)
                continue;
            if(vis[q.x][q.y]==0)        //访问未被访问过的位置,再将该位置入队 
            {
                vis[q.x][q.y]=1;
                t[q.x][q.y]=q.step;
                Q.push(q);
            }
        }
    }
}
int bfs_people()
{
    while(!Q.empty())
        Q.pop();
    memset(vis,0,sizeof(vis));
    int i,j;
    p.x=sx;
    p.y=sy;
    p.step=0;
    vis[p.x][p.y]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(s[p.x][p.y]=='t')
            return p.step;
        for(i=0;i<4;i++)
        {
            q=p;
            q.x+=px[i];
            q.y+=py[i];
            q.step++;
            if(q.x<0||q.y<0||q.x>=n||q.y>=m)
                continue;
            //访问未被访问过的位置,且此时是在火势蔓延到此前访问的,再将该位置入队
            if(vis[q.x][q.y]==0&&q.step<t[q.x][q.y])
            {
                vis[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
    return -1;
}

int main()
{
    int i,j;
    while(scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        if(n==0&&m==0&&k==0) break;
        memset(t,inf,sizeof(t));
        memset(vis,0,sizeof(vis));
        while(!Q.empty())
            Q.pop();
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='f')
                {
                    r.x=i;
                    r.y=j;
                    r.step=0;
                    t[i][j]=0;
                    vis[i][j]=1;
                    Q.push(r);
                }
                if(s[i][j]=='s')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        bfs_fire();
        int ans=bfs_people();
        if(ans==-1)
            printf("Impossible\n");
        else
            printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值