题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间
分析:两遍BFS,然后比较边界
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stdlib.h>
#include<string>
#include<set>
using namespace std;
typedef long long LL;
const int maxn=;
const int INF=0x3f3f3f3f;
char s[maxn][maxn];
int mp[maxn][maxn];
int v[maxn][maxn];
int n,m;
struct Point
{
int x,y;
Point() {}
Point(int a,int b)
{
x=a,y=b;
}
} o,t;
queue<Point>q,e;
int dx[]= {,,-,};
int dy[]= {-,,,};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)
scanf("%s",s[i]+);
for(int i=; i<=n; ++i)
{
for(int j=; j<=m; ++j)
{
mp[i][j]=v[i][j]=-;
if(s[i][j]=='F')q.push(Point(i,j)),mp[i][j]=;
else if(s[i][j]=='J')e.push(Point(i,j)),v[i][j]=;
}
}
while(!q.empty())
{
o=q.front();
q.pop();
for(int i=; i<; ++i)
{
t.x=o.x+dx[i];
t.y=o.y+dy[i];
if(t.x<||t.x>n||t.y<||t.y>m)continue;
if(s[t.x][t.y]=='#'||mp[t.x][t.y]!=-)continue;
mp[t.x][t.y]=mp[o.x][o.y]+;
q.push(t);
}
}
while(!e.empty())
{
o=e.front();
e.pop();
for(int i=; i<; ++i)
{
t.x=o.x+dx[i];
t.y=o.y+dy[i];
if(t.x<||t.x>n||t.y<||t.y>m)continue;
if(s[t.x][t.y]=='#'||v[t.x][t.y]!=-)continue;
v[t.x][t.y]=v[o.x][o.y]+;
e.push(t);
}
}
int ans=INF;
for(int i=;i<=n;++i)
{
if(v[i][]!=-)
{
if(mp[i][]==-||v[i][]<mp[i][])
ans=min(ans,v[i][]);
}
if(v[i][m]!=-)
{
if(mp[i][m]==-||v[i][m]<mp[i][m])
ans=min(ans,v[i][m]);
}
}
for(int i=;i<=m;++i)
{
if(v[][i]!=-)
{
if(mp[][i]==-||v[][i]<mp[][i])
ans=min(ans,v[][i]);
}
if(v[n][i]!=-)
{
if(mp[n][i]==-||v[n][i]<mp[n][i])
ans=min(ans,v[n][i]);
}
}
if(ans==INF)printf("IMPOSSIBLE\n");
else printf("%d\n",ans+);
}
return ;
}