CSU 2031: Barareh on Fire

2031: Barareh on Fire

Submit Page    Summary    Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitted: 741     Solved: 221    


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

 

搜索题。刚开始想着bfs,后来脑子一抽想用dfs试一试,后来在地图重复改变的时候感觉空间消耗有点大,就没写了。

思路:先用bfs搜索地图,记录各个区域着火的时间点,可以用一个数组time表示,然后再用一次bfs从起点出发,探索每一层的点,如果到达当前点的时间小于该点着火的时间,说明可行,入队即可。如果到达终点,当前时间即为最短时间(因为是bfs),返回即可。

注意要先处理一下time数组,因为在没有地方着火的时候,会WA。

 

代码:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
int n,m,k;
char Map[105][105];
bool visit[105][105];
int _time[105][105],op1[4][2]={1,0,-1,0,0,1,0,-1},op2[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int sx,sy,ex,ey;

struct node
{
    int x,y,t;
};
typedef struct node cell;

inline bool check(int x,int y)
{
    return (x<n&&x>=0&&y>=0&&y<m);
}

void fire()
{
    queue<cell> q;
    cell temp;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(Map[i][j]=='f')
            {
                temp.x=i;temp.y=j;
                _time[i][j]=0;
                q.push(temp);
            }
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        int _x=temp.x,_y=temp.y,_t=_time[_x][_y],xx,yy;
        for(int i=0;i<8;i++)
        {
            xx=_x+op2[i][0];yy=_y+op2[i][1];
            if(check(xx,yy)&&Map[xx][yy]!='f')
            {
                temp.x=xx;temp.y=yy;
                _time[xx][yy]=_t+k;
                Map[xx][yy]='f';
                q.push(temp);
            }
        }
    }
}

int bfs()
{

    cell temp;
    queue<cell> q;
    temp.x=sx;temp.y=sy;temp.t=0;
    visit[sx][sy]=true;
    q.push(temp);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        int _x=temp.x,_y=temp.y,_t=temp.t+1,xx,yy;
        if(_x==ex&&_y==ey) return temp.t;
        for(int i=0;i<4;i++)
        {
            xx=_x+op1[i][0];yy=_y+op1[i][1];
            if(check(xx,yy)&&!visit[xx][yy]&&_t<_time[xx][yy])
            {
                visit[xx][yy]=true;
                temp.x=xx;temp.y=yy;temp.t=_t;
                q.push(temp);
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)&&n)
    {
        memset(visit,0,sizeof(visit));
        mint=205;
        for(int i=0;i<n;i++)
        {
            scanf("%s",Map[i]);
            for(int j=0;j<m;j++)
            {
                _time[i][j]=100000;
                if(Map[i][j]=='s')
                {
                    sx=i;sy=j;
                }
                else if(Map[i][j]=='t')
                {
                    ex=i;ey=j;
                }
            }
        }
        fire();
        int step=bfs();
        if(step!=-1) cout<<step<<endl;
        else cout<<"Impossible"<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值