There are one person and multiple ignition points in a maze. The speed of fire spread is the same as that of human movement. Ask the shortest time to escape from the maze.
This question has been changed three times before and after, and the last one is the online thinking
Make two bfs, first rush to the ignition point to get the ignition time of each place, the initial time is infinite, the second bfs starts from people, as long as the time of people's arrival is shorter than or, then enter the team. Use array to store fire and time when people arrive.
ACd Code:
#include<cstdio> #include<queue> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<map> using namespace std; typedef pair<int,int> P; typedef long long LL; typedef vector<int> V; #define V(x) vector<x> const int INF=0x3f3f3f3f,MAXN=1000000000+7; int T,vis[1005][1005]={0},cnt=1,m,n,ans,visf[1005][1005],nwj[1005][1005]; int dr[]={-1,1,0,0},dc[]={0,0,-1,1},J; char pic[1005][1005]; queue<P> A,B; void bfs1(){ while (!B.empty()){ P t=B.front(); B.pop(); int r=t.first,c=t.second; //printf("1:%d %d %d\n",r,c,visf[r][c]); for (int i=0;i<4;i++){ int nr=r+dr[i],nc=c+dc[i]; if (pic[nr][nc]!='#'&&nr>=1&&nr<=n&&nc>=1&&nc<=m&&visf[nr][nc]==INF){ B.push(make_pair(nr,nc)); visf[nr][nc]=visf[r][c]+1; } } } } void bfs2(){ while (!A.empty()){ P t=A.front(); A.pop(); int r=t.first,c=t.second; //printf("2:%d %d %d\n",r,c,vis[r][c]); for (int i=0;i<4;i++){ int nr=r+dr[i],nc=c+dc[i]; if (nr<1||nr>n||nc<1||nc>m) { ans=vis[r][c]+1; return ; } if (vis[nr][nc]==INF&&pic[nr][nc]!='#'&&vis[r][c]+1<visf[nr][nc]){ A.push(make_pair(nr,nc)); vis[nr][nc]=vis[r][c]+1; } } } } int main(){ scanf("%d",&T); while (T--){ scanf("%d%d",&n,&m); ans=-1; memset(vis,0x3f,sizeof(vis)); memset(visf,0x3f,sizeof(visf)); while (!A.empty()) A.pop(); while (!B.empty()) B.pop(); for (int i = 1; i <=n; ++i) { for (int j = 1; j <=m; ++j) { scanf(" %c",&pic[i][j]); if (pic[i][j]=='J') { A.push(make_pair(i,j)); vis[i][j]=0; } if (pic[i][j]=='F') { B.push(make_pair(i,j)); visf[i][j]=0; } } } bfs1(); bfs2(); if (ans!=-1) printf("%d\n",ans); else printf("IMPOSSIBLE\n"); } }