FZU2150 Fire Game —— BFS

題目連接:https://vjudge.net/problem/FZU-2150node


 

Problem 2150 Fire Game

Accept: 2702    Submit: 9240
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 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.)ios

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.this

 Input

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

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..net

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

 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.ip

 Sample Input

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

 Sample Output

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

 

 



題解:ci

1.直接枚舉兩個起火點,而後BFS。get

2.計算次數:100(測試組數)*100(第一個起火點)*100(第二個起火點)*100(棋盤大小) = 1e8,不是會超時嗎?可能數據比較弱吧。




代碼以下:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 10+10;

int n, m;
char M[MAXN][MAXN];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};

struct node
{
    int x, y, step;
};

int vis[MAXN][MAXN];
queue<node>que;
int bfs(int x, int y, int xx, int yy)
{
    ms(vis, 0);
    while(!que.empty()) que.pop();

    node now, tmp;
    now.x = x; now.y = y;
    now.step = 0;
    vis[now.x][now.y] = 1;
    que.push(now);

    now.x = xx; now.y = yy;
    now.step = 0;
    vis[now.x][now.y] = 1;
    que.push(now);

    int ret = 0;
    while(!que.empty())
    {
        now = que.front();
        que.pop();

        for(int i = 0; i<4; i++)
        {
            tmp.x = now.x + dir[i][0];
            tmp.y = now.y + dir[i][1];
            if(tmp.x>=1 && tmp.x<=n && tmp.y>=1 && tmp.y<=m
               && M[tmp.x][tmp.y]=='#' && !vis[tmp.x][tmp.y])
            {
                vis[tmp.x][tmp.y] = 1;
                tmp.step = now.step + 1;
                ret = max(ret, tmp.step);
                que.push(tmp);
            }
        }
    }
    for(int i = 1; i<=n; i++)  //判斷是否全部的草地都被焚了
    for(int j = 1; j<=m; j++)
        if(M[i][j]=='#' && !vis[i][j])
            return -1;
    return ret;
}

int solve()
{
    int ret = INF;
    for(int i = 1; i<=n; i++)  //枚舉兩個起火點
    for(int j = 1; j<=m; j++)
    if(M[i][j]=='#')
    {
        for(int ii = 1; ii<=n; ii++)
        for(int jj = 1; jj<=m; jj++)
        if(M[ii][jj]=='#')
        {
            int tmp = bfs(i,j,ii,jj);
            if(tmp!=-1) ret = min(ret, tmp);
        }
    }
    return (ret!=INF)?ret:-1;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int kase = 1; kase<=T; kase++)
    {
        scanf("%d%d",&n,&m);
        for(int i = 1; i<=n; i++)
            scanf("%s", M[i]+1);
        printf("Case %d: %d\n", kase, solve() );
    }
}
相關文章
相關標籤/搜索