The main idea: Flame game in aThere are some hay in the n*m grid with ' # ', you can choose to set fire in two places, the flames can burn around, and the quickest time to burn the hay .
It is easy to think of any two places with hay, to see which one can burn the fastest and burn out,1 <= N <=10, 1 <= m <=10, the map is not too big, you can try it ........ .......
There is no problem .....
#include <queue>
#include <stdio.h>
#include <string.h>
usingnamespaceStd
#defineMAXN 15
ConstintOO =0xFFFFFFF;
structnode{intx, y, step;} a[ the];//record the location of all hay
CharG[MAXN][MAXN];
intV[MAXN][MAXN];//Tag Array
intdir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
intM, N;
intOK ()
{
intI, J;
for(i=0; i<m; i++)
for(j=0; j<n; J + +)
{
if(g[i][j]=='#'&& V[i][j] = =0)
return0;
}
return1;
}
intBFS (node s, node Q)//arbitrarily choose two piles of hay
{
Queue<node> Q;
Q.push (s); Q.push (q);
V[S.X][S.Y] = v[q.x][q.y] =1;
while(Q.size ())
{
s = Q.front (); Q.pop ();
for(intI=0; i<4; i++)
{
Q = s;
q.x + = dir[i][0];
Q.y + = dir[i][1];
if(q.x>=0&& q.x< M && q.y >=0&& Q.y < N && G[q.x][q.y] = ='#'&& V[q.x][q.y] = =0)
{
V[Q.X][Q.Y] =1;
Q.step + +;
Q.push (q);
}
}
}
returnS.step;
}
intMain ()
{
intT, t=1;
scanf"%d", &t);
while(t--)
{
intI, J, k=0, Ans=oo;
scanf"%d%d", &m, &n);
for(i=0; i<m; i++)
{
scanf"%s", G[i]);
for(j=0; j<n; J + +)
{
if(G[i][j] = ='#')
{
A[k].step =0;
a[k].x = i;
A[k++].y = j;
}
}
}
for(i=0; i<k; i++)
for(j=i; j<k; j + +)
{
memset (V,0,sizeof(v));
intL = BFS (A[i], a[j]);
if(L < ans && OK ())
ans = L;
}
if(ans = = oo)
printf"Case %d: -1\n", t++);
Else
printf"Case %d:%d\n", t++, ans);
}
return0;
}
I-fire Game