码迷,mamicode.com
首页 > 其他好文 > 详细

FZU Problem 2150 Fire Game (双起点BFS啊 )

时间:2014-12-03 21:33:29      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:fzu   bfs   

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150


Problem Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

bubuko.com,布布扣 Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

bubuko.com,布布扣 Output

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

bubuko.com,布布扣 Sample Input

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

bubuko.com,布布扣 Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2


题意:

一个m * n的图,‘ . ’表示空地,‘#’表示草坪,可以选择在任意的两个草坪格子为起点 点火,每 1 s 火苗会向周围的四个格子扩散,

求选择哪两个点能使得燃烧所有草坪花费的时间最小。

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
const int MAXN = 17;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct node
{
    int x,y;
    int c;
};
struct pp
{
    int x, y;
    int c;
} pp[MAXN];

int n, m;
char mm[MAXN][MAXN];
int vis[MAXN][MAXN];
queue<node > q;
vector<node > v;

int bfs(node a,node b)
{
    node p;
    memset(vis,0,sizeof(vis));
    a.c = 0;
    b.c = 0;
    vis[a.x][a.y] = 1;
    vis[b.x][b.y] = 1;
    q.push(a);
    q.push(b);
    int k;
    while(!q.empty())
    {
        p = q.front();
        q.pop();
        k = p.c;
        for(int i = 0; i < 4; i++)
        {
            b.x = p.x + dx[i];
            b.y = p.y + dy[i];
            b.c = p.c + 1;
            if(b.x>=1 && b.x<=n && b.y>=1 && b.y<=m && mm[b.x][b.y]=='#' && vis[b.x][b.y]==0)
            {
                vis[b.x][b.y] = 1;
                q.push(b);
            }
        }
    }
    return k;
}

int judge()
{
    for(int k = 1; k <= n; k++)
    {
        for(int l = 1; l <= m; l++)
        {
            if(vis[k][l] == 0 && mm[k][l]=='#')//没有被烧完
            {
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    int cas = 0;
    int step;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty())
        {
            q.pop();
        }
        scanf("%d%d",&n,&m);
        v.clear();
        int l = 0;
        for(int i=1; i<=n; i++)
        {
            getchar();
            for(int j=1; j<=m; j++)
            {
                scanf("%c",&mm[i][j]);
                if(mm[i][j] == '#')
                {
                    pp[l].x = i;
                    pp[l].y = j;
                    pp[l].c = 0;
                    //v.push_back(pp[l]);
                    v.push_back((node)
                    {
                        i,j,0
                    });
                }
            }
        }
        step = INF;
        int len = v.size();
        for(int i = 0; i < len; i++)
        {
            for(int j = i; j < len; j++)
            {
                int tt = bfs(v[i], v[j]);
                if(!judge())
                {
                    if(step > tt)
                        step = tt;
                }
            }
        }
        printf("Case %d: ",++cas);
        if(step == INF)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",step);
    }
    return 0;
}



FZU Problem 2150 Fire Game (双起点BFS啊 )

标签:fzu   bfs   

原文地址:http://blog.csdn.net/u012860063/article/details/41701297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!