Barareh on Fire

题目描述

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.

样例输入

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

样例输出

4
Impossible
Impossible
1


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
#define ll long long
#define maxn 110
#define inf 0x3f3f3f3f
using namespace std;

char mmp[maxn][maxn];
int cmmp[maxn][maxn];
int  vis[maxn][maxn];
int x,y,k;
int dir[8][2]={
-1,-1, -1,0, -1,1,
0,-1,        0,1,
1,-1, 1,0, 1,1
};
int dr[4][2]={
    -1,0,
0,-1,    0,1,
    1,0
};
struct point
{
    int x,y,indx;
};


int Funtion()
{
    for(int i=1;i<=x;i++)
        cin>>mmp[i]+1;

    queue<point> Q;

    point a,s,t;

    for(int i=1;i<=x;i++)
    {
        for(int j=1;j<=y;j++)
        {
            cmmp[i][j]=inf;
            if(mmp[i][j]=='f')
            {
                a.indx=0;
                cmmp[i][j]=0;
                a.x=i,a.y=j;
                Q.push(a);
            }
            else if(mmp[i][j]=='s')
            {
                s.indx=0;
                s.x=i,s.y=j;
            }
            else if(mmp[i][j]=='t')
            {
                t.x=i,t.y=j;
            }
        }
    }

    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();

        for(int i=0;i<8;i++)
        {
            int tx=a.x+dir[i][0];
            int ty=a.y+dir[i][1];
            if(tx>=1 && tx<=x && ty>=1 && ty<=y)
            {
                if(cmmp[tx][ty]>a.indx+k)
                {
                    point b;
                    b.x=tx,b.y=ty,b.indx=a.indx+k;
                    cmmp[tx][ty]=b.indx;
                    Q.push(b);
                }
            }
        }
    }

    Q.push(s);

    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();

        for(int i=0;i<4;i++)
        {
            int tx=a.x+dr[i][0];
            int ty=a.y+dr[i][1];
            if(tx>=1 && tx<=x && ty>=1 && ty<=y)
            {
                if(cmmp[tx][ty]>a.indx+1 && vis[tx][ty]>a.indx+1)
                {
                    vis[tx][ty]=a.indx+1;
                    point b;
                    b.indx=a.indx+1;
                    b.x=tx,b.y=ty;
                    Q.push(b);
                }
            }
        }
    }
    if(vis[t.x][t.y]==inf)
        printf("Impossible\n");
    else
        printf("%d\n",vis[t.x][t.y]);
    return 0;
}

int main()
{

    while(cin>>x>>y>>k)
    {
        ms(cmmp,0);
        ms(mmp,0);
        for(int i=0;i<=x;i++)
        {
            for(int j=0;j<=y;j++)
                vis[i][j]=inf;
        }
        if(!x&&!y&&!k)
            break;
        Funtion();
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/du_mingm/article/details/80043669