This question really wants to say well, the race time how not to live, the pressure point always error (Vis should be initialized to INF, but I initialized to -1 ...), WA n times, then thought can completely avoid this problem, as long as the queue to judge the line.
Because the data is relatively small, so can be violent to solve, but first judge the Unicom block can solve a small number of problems.
#include <iostream>#include<cstdio>#include<queue>#include<cstring>using namespacestd;#defineINF 99999999Charmaps[ A][ A];intn,m,vis[ A][ A],go[4][2] = {1,0,-1,0,0,1,0,-1};structpos{intx, y;};BOOLOK (Pos a) {return(a.x>=0&& a.x<n && a.y>=0&& a.y<m && maps[a.x][a.y] = ='#');} Queue<Pos>que;intBFS (Pos f1,pos f2) { while(!que.empty ()) Que.pop (); Que.push (F1); Que.push (F2); for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {Vis[i][j]=inf; }} Vis[f1.x][f1.y]= Vis[f2.x][f2.y] =0; while(!Que.empty ()) {Pos now=Que.front (); Que.pop (); Pos NEX; for(inti =0; I <4; i++) {nex.x= now.x + go[i][0]; Nex.y= Now.y + go[i][1]; if(OK (NEX) && vis[nex.x][nex.y] > VIS[NOW.X][NOW.Y] +1) {vis[nex.x][nex.y]= Vis[now.x][now.y] +1; Que.push (NEX); } } } intMAXV =-999999; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) { if(Maps[i][j] = ='#') MAXV=Max (maxv,vis[i][j]); } } returnMAXV;}intMain () {intT,ca =0; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(inti =0; I < n; i++) scanf ("%s", Maps[i]); intCNT =0; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) { if(Maps[i][j] = ='#') CNT++; }} printf ("Case %d:",++CA); if(CNT <=2) {printf ("%d\n",0); Continue; } intAns =inf; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) { if(Maps[i][j] = ='#') { for(intK =0; K < n; k++) { for(intQ =0; Q < m; q++) { if(j = = Q && i = = k)Continue; if(Maps[k][q] = ='#') {Pos f1,f2; f1.x= I,F1.Y =J; f2.x= K,F2.Y =Q; Ans=min (Ans,bfs (F1,F2)); } } } } } } if(ans = =inf) {cout<<-1<<Endl; } Elsecout<<ans<<Endl; } return 0;}
Fzu Problem 2150 Fire Game