B-逃脱
题号:NC14548
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园某处的mengxiang000和Tabris听到了火灾警报声的同时拔腿就跑,不知道两人是否能够逃脱险境?
幼儿园可以看成是一个N*M的图,在图中一共包含以下几种元素:
“.”:表示这是一块空地,是可以随意穿梭的。
“#”:表示这是一块墙,是不可以走到这上边来的,但是可以被火烧毁。
“S”:表示mengxiang000和Tabris所在位子。
“E”:表示幼儿园的出口。
“*”表示火灾发源地(保证输入只有一个火灾发源地)。
已知每秒有火的地方都会向周围八个格子(上下左右、左上、右上、左下、右下)蔓延火势.mengxiang000和Tabris每秒都可以选择周围四个格子(上下左右)进行移动。(假设两人这一秒行动完之后,火势才蔓延开)
根据已知条件,判断两人能否成功逃脱险境,如果可以,输出最短逃离时间,否则输出T_T。
为了防止孩子们嬉戏中受伤,墙体是橡胶制作的,可以燃烧的哦。
输入描述:
第一行输入一个整数t,表示一共的测试数据组数。 第二行输入两个整数n,m,表示幼儿园的大小。 接下来n行,每行m个字符,表示此格子是什么元素。 t<=200 3<=n<=30 3<=M<=30 保证图中有一个起点,一个出口,一个火灾源处
输出描述:
每组数据输出一行,如果两人能够成功到达出口,那么输出最短逃离时间,否则输出T_T
示例1
输入
复制
3 5 5 *.... ..... ..S#. ...E. ..... 5 5 ...#* ..#S# ...## ....E ..... 5 5 ..... S.... ..*#. ...E. .....
输出
复制
2 T_T T_T
说明
为了防止孩子们嬉戏中受伤,墙体是橡胶制作的,可以燃烧的哦。
备注:
为了防止孩子们嬉戏中受伤,墙体是橡胶制作的,可以燃烧的哦。
//#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[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {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[35][35];
int vis[35][35];
struct node{
int x,y;
int step;
};
int sx,sy,ex,ey,cx,cy;
int fire[35][35];
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;
}
}

该博客讨论了一个关于幼儿在幼儿园火场逃生的算法问题。题目描述了幼儿园地图的结构,包括空地、墙、起点、终点和火源,并规定了火势蔓延和幼儿移动的规则。算法通过模拟火势扩散和幼儿移动来判断是否能成功逃离。示例给出了三个测试用例,其中两个无法逃离,一个能在两步内逃离。博客内容涉及网格搜索、状态转移和最短路径寻找等算法知识。
738

被折叠的 条评论
为什么被折叠?



