# NC14548-B-escape (bfs)

Posted by antisback on Wed, 24 Nov 2021 02:27:59 +0100

B - Escape

Title No.: NC14548
Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 32768K, other languages 65536K
64bit IO Format: %lld

## Title Description

This is the fourth day when mengxiang000 and Tabris came to the kindergarten. When the kindergarten teacher was on duty, he suddenly found a fire somewhere in the kindergarten, and the fire spread very fast. The teacher gave an alarm at the first time. Mengxiang000 and Tabris, located somewhere in the kindergarten, ran away when they heard the fire alarm. I don't know whether they can escape the danger?

Kindergarten can be regarded as an N*M diagram, which contains the following elements:

"..: it means that this is an open space, which can be shuttled at will.

"#": it means that this is a wall. You can't walk on it, but it can be burned by fire.

"S": indicates the seat of mengxiang000 and Tabris.

"E": the exit of kindergarten.

"*" indicates the fire origin (ensure that there is only one fire origin input).

It is known that the fire will spread to the surrounding eight squares (up, down, left, top left, top right, bottom left and bottom right). mengxiang000 and Tabris can select the surrounding four squares (up, down, left and right) to move every second. (suppose the fire didn't spread until the two men finished their action in this second)

Judge whether the two people can successfully escape the danger according to the known conditions. If they can, output the shortest escape time, otherwise output T_T.

In order to prevent children from being injured in play, the wall is made of rubber and can burn.

## Enter Description:

```In the first line, enter an integer t to represent the total number of test data groups.
On the second line, enter two integers n, m to represent the size of the kindergarten.
The next n lines, m characters per line, indicate what elements this grid is.
t<=200
3<=n<=30
3<=M<=30
Ensure that there is a starting point, an exit and a fire source in the drawing```

## Output Description:

`One row of each group of data is output. If two people can successfully reach the exit, the shortest escape time is output, otherwise t is output_ T`

Example 1

copy

```3
5 5
*....
.....
..S#.
...E.
.....
5 5
...#*
..#S#
...##
....E
.....
5 5
.....
S....
..*#.
...E.
.....```

copy

```2
T_T
T_T```

## explain

`In order to prevent children from being injured in play, the wall is made of rubber and can burn.`

## remarks:

`In order to prevent children from being injured in play, the wall is made of rubber and can burn.`
```//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}

int xd = {0, 1, 0, -1, 1, 1, -1, -1};
int yd = {1, 0, -1, 0, -1, 1, -1, 1};

//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int n,m,t;
char a;
int vis;
struct node{
int x,y;
int step;
};
int sx,sy,ex,ey,cx,cy;
int fire;
void Fire(int x,int y){
memset(fire, -1, sizeof(fire));
queue<node> p;
p.push({x,y,0});
while (!p.empty()) {
node q=p.front();
p.pop();
for(int i=0;i<8;i++){
int xx=q.x+xd[i];
int yy=q.y+yd[i];
if(xx<0||xx>=n||yy<0||yy>=m||fire[xx][yy]!=-1){
continue;
}
p.push({xx,yy,q.step+1});
fire[xx][yy]=q.step+1;
}
}
}
int bfs(int x,int y){
queue<node> p;
memset(vis, 0, sizeof(vis));
p.push({x,y,0});

while (!p.empty()) {
node q=p.front();
p.pop();
vis[q.x][q.y]=1;
for(int i=0;i<4;i++){
int xx=q.x+xd[i];
int yy=q.y+yd[i];
if(xx<0||xx>n-1||yy<0||yy>m-1) continue;
if(a[xx][yy]=='E'&&fire[xx][yy]>=q.step+1) return q.step+1;

if(a[xx][yy]=='#'||vis[xx][yy]||fire[xx][yy]<=q.step+1) continue;
p.push({xx,yy,q.step+1});
}
}
return -1;
}
int main()
{
cin>>t;
while (t--) {
cin>>n>>m;
int ans=0;

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='*'){
cx=i;
cy=j;
}
if(a[i][j]=='S'){
sx=i;
sy=j;
}
if(a[i][j]=='E'){
ex=i;
ey=j;
}
}
}
Fire(cx, cy);
ans=bfs(sx,sy);
if(ans<0) cout<<"T_T"<<endl;
else cout<<ans<<endl;
}
}
```

Topics: bfs codeforce AcWing