I

题目大意:
火焰游戏
在一个 N*M的网格里面有一些用‘#’表示的干草,可以选择在两个地方放火,火焰可以向四周燃烧,求烧完这些干草最快需要多少时间

////////////////////////////////////////////////////////////////////////
可以很容易想到任意取两个有干草的地方,看看那个可以烧的最快,并且烧完,1 <= n <=10, 1 <= m <=10,地图也不是太大,可以尝试一下...........................

没有问题.............
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;

#define maxn 15
const int oo = 0xfffffff;

struct node{int x, y, step;}a[105];//记录所有干草的位置
char G[maxn][maxn];
int v[maxn][maxn];//标记数组
int dir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
int M, N;

int OK()
{
    int i, j;

    for(i=0; i<M; i++)
    for(j=0; j<N; j++)
    {
        if(G[i][j]=='#' && v[i][j] == 0)
            return 0;
    }

    return 1;
}
int BFS(node s, node q)//任意的选取两堆干草
{
    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(int i=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);
            }
        }
    }

    return s.step;
}

int main()
{
    int T, t=1;

    scanf("%d", &T);

    while(T--)
    {
        int i, 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, 0sizeof(v));
            int L = BFS(a[i], a[j]);

            if(L < ans && OK())
                ans = L;
        }

        if(ans == oo)
            printf("Case %d: -1 ", t++);
        else
            printf("Case %d: %d ", t++, ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4650179.html