Test instructions: Just choose two points to start, only to go to the lawn, see if you can walk through all the lawn
Analysis: Because the data range is very small, all enumerate these two points, in advance all the lawn points are saved, and then choose two points to go, (two points can be the same point)
Then the BFS will do.
Note: The situation of no solution is very good to do, in advance the number of deep search connected block is good, more than 2 no solution (code comparison rotten)
#include <cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stdlib.h>#include<string>#include<Set>using namespaceStd;typedefLong LongLL;Const intmaxn= A;Const intinf=0x3f3f3f3f;intn,m;structpoint{intx, y;} o,t,a[ the];intmp[maxn][maxn],p,h;intBEL[MAXN][MAXN];CharS[MAXN][MAXN];intdx[4]= {0,0,-1,1};intdy[4]= {-1,1,0,0};voidDfsintXinty) {Bel[x][y]=p; for(intI=0; i<4; ++i) {inte=x+Dx[i]; intr=y+Dy[i]; if(e<1|| e>n| | r<1|| R>M)Continue; if(s[e][r]=='.'|| BEL[E][R])Continue; DFS (E,R); }}queue<Point>Q;intBFs () {intans=0; H=q.size (); while(!Q.empty ()) {o=Q.front (); Q.pop (); for(intI=0; i<4; ++i) {t.x=o.x+Dx[i]; T.y=o.y+Dy[i]; if(t.x<1|| t.x>n| | t.y<1|| T.Y>M)Continue; if(s[t.x][t.y]=='.'|| mp[t.x][t.y]!=-1)Continue; MP[T.X][T.Y]=mp[o.x][o.y]+1; Ans=Max (Ans,mp[t.x][t.y]); H++; Q.push (t); } } returnans;}intMain () {intt,cas=0; scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&m); Memset (BEL,0,sizeof(BEL)); P=0; for(intI=1; i<=n; ++i) scanf ("%s", s[i]+1); intCnt=0, ans=INF; for(intI=1; i<=n; ++i) for(intj=1; j<=m; ++j)if(s[i][j]=='#') { if(!bel[i][j]) + +P,dfs (I,J); a[cnt].x=i,a[cnt++].y=J; } printf ("Case %d:",++CAs); if(p>2) {printf ("-1\n"); Continue; } BOOLflag=0; for(intI=0; i<cnt; ++i) { for(intj=0; j<cnt; ++j) {if(p==2&&bel[a[i].x][a[i].y]==Bel[a[j].x][a[j].y])Continue; H=0; Memset (MP,-1,sizeof(MP)); MP[A[I].X][A[I].Y]=mp[a[j].x][a[j].y]=0; Q.push (A[i]); if(i!=j) Q.push (A[j]); intres=BFS (); if(h==cnt) ans=min (ans,res); }} printf ("%d\n", ans); } return 0;}
View Code
FZU2150 Fire Game BFS Search