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

FZU 2150 Fire Game

时间:2014-05-09 14:08:37      阅读:502      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

Problem 2150 Fire Game

Accept: 237    Submit: 808
Time Limit: 1000 mSec    Memory Limit : 32768 KB

bubuko.com,布布扣 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

给你一个n*m的图,里面有草也有空地(#代表草)。现在有两个人各在一块草地点火要烧掉这些草,并且燃烧的草可以向上下左右四个方向蔓延,问最少多长时间可以将所有的草都烧完。
枚举图中所有草地,找到任意两块不一样的草地,然后bfs求出烧掉所有草的最短时间。不需要判断连通块的个数,如果连通块的个数大于2,答案肯定是inf。
//796 ms	228KB
#include<stdio.h>
#include<string.h>
#include<queue>
#define M 17
#define inf 0x3f3f3f
using namespace std;
struct N
{
    int x,y;
} node[M*M];
int n,m;
char s[M][M];
int d[M][M];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int bfs(int x1,int y1,int x2,int y2)
{
    queue<N>q;
    memset(d,inf,sizeof(d));
    N n1,n2,now,next;
    n1.x=x1;n1.y=y1;
    n2.x=x2;n2.y=y2;
    d[x1][y1]=0;d[x2][y2]=0;
    q.push(n1);q.push(n2);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=now.x+dir[i][0];
            int yy=now.y+dir[i][1];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&s[xx][yy]==‘#‘&&d[xx][yy]>d[now.x][now.y]+1)
            {
                d[xx][yy]=d[now.x][now.y]+1;
                next.x=xx;next.y=yy;
                q.push(next);
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(s[i][j]==‘#‘)
            res=max(d[i][j],res);
    return res;
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        int count=0;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s[i]+1);
            for(int j=1; j<=m; j++)
                if(s[i][j]==‘#‘)count++;
        }
        printf("Case %d: ",cas++);
        if(count<=2)
        {
            printf("0\n");
            continue;
        }
        int ans=inf;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                if(s[i][j]==‘#‘)
                {
                    for(int l=1;l<=n;l++)
                        for(int r=1;r<=m;r++)
                        {
                            if(l<i&&r<=j)continue;
                             if(s[l][r]==‘#‘)
                            {
                                int res=bfs(i,j,l,r);
                                ans=min(ans,res);
                            }
                        }
                }
        if(ans==inf)printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


FZU 2150 Fire Game,布布扣,bubuko.com

FZU 2150 Fire Game

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/crescent__moon/article/details/25365549

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