Title Link: http://acm.fzu.edu.cn/problem.php?pid=2150
Test instructions: Ignition in any two places, to find the shortest time to burn all the haystack.
Analysis: Because the n,m is relatively small, all the haystack coordinates are recorded, then the violent enumeration of all possible two haystack as the starting point for burning. Finally take the shortest time. It is necessary to use the BFS once every two burning time.
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 100010using namespacestd;structnode{intX,y,step;};intvis[ the][ the],n,m;Chars[ the][ the];intpx[ the],py[ the],tot;intdx[]={0,0,1,-1};intdy[]={1,-1,0,0};queue<node>Que;node Make_node (intAintBintc) {Node temp; Temp.x=a;temp.y=b;temp.step=C; returntemp;}BOOLJudgeintXinty) { returnx>=1&&x<=n&&y>=1&&y<=m&&s[x][y]=='#'&&!vis[x][y];}intBFsintIintj) {memset (Vis,0,sizeof(VIS)); while(!que.empty ()) Que.pop (); Que.push (Make_node (px[i],py[i),0)); Que.push (Make_node (px[j],py[j),0)); Vis[px[i]][py[i]]=1; vis[px[j]][py[j]]=1; intsum=0, mx=0; while(!Que.empty ()) {node cur,nxt; Cur=Que.front (); Que.pop (); intstep=Cur.step; MX=Max (STEP,MX); Sum++; for(intIi=0;ii<4; ii++) { intx=dx[ii]+cur.x,y=dy[ii]+Cur.y; if(judge (x, Y)) {Vis[x][y]=1; Que.push (Make_node (X,y,step+1)); } } } if(Sum==tot)returnMX; Else returninf;}intMain () {intt,cas=1; scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%s", s[i]+1); Tot=0; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) if(s[i][j]=='#') {px[++tot]=i;py[tot]=J; }} printf ("Case %d:", cas++); if(tot<=2) {puts ("0");Continue; } intans=inf; for(intI=1; i<=tot;i++) for(intj=i+1; j<=tot;j++) {ans=min (Ans,bfs (i,j)); } if(Ans==inf) puts ("-1"); Elseprintf"%d\n", ans); }}View Code
fzu2150 (BFS)