Fire Game FZU - 2150 (bfs)

Problem 2150 Fire Game

Accept: 3772    Submit: 12868
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.)

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.

 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

 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.

 Sample Input

4

3 3

.#.

###

.#.

3 3

.#.

#.#

.#.

3 3

...

#.#

...

3 3

###

..#

#.#

 Sample Output

Case 1: 1

Case 2: -1

Case 3: 0

Case 4: 2

题意:给了你一块草地,‘#‘表示地上有草,火可以蔓延,‘.‘火不可以蔓延,然后有两个吃的没事干的人分别在这个草地上的某一个地方点火,点火的时间不算,之后每一秒火都会蔓延到其上下左右,问可以不可烧完所有的草,可以就输出最少的时间,不可以就输出-1

思路:只能枚举每两个草了,所以题目给的地也不是很大,也就10*10最大了,之后就每两个点一起烧,一开始队列里面要放进去两个点,之后可以烧到的草有可能两把火都会被烧到,所以应该取较早时间被烧到的那个时间,但如果火都烧没了,不能蔓延了却还有草,说明不能烧完所有的草,那就输出-1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = 15;
const int mod = 1e9 + 7;
int gcd(int a, int b) {
    if (b == 0) return a;  return gcd(b, a % b);
}
char maze[maxn][maxn];
int ans;
struct point
{
    int x,y;
};
int n,m;
int dis[maxn][maxn];
queue<point>que;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int bfs(int x1,int y1,int x2,int y2)
{
    while(!que.empty())
        que.pop();
    memset(dis,INF,sizeof(dis));
    point p1,p2,next;
    p1.x=x1;
    p1.y=y1;
    p2.x=x2;
    p2.y=y2;
    dis[x1][y1]=0;
    dis[x2][y2]=0;
    que.push(p1);
    que.push(p2);
    while(!que.empty())
    {
        point p=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int nx=p.x+dx[i];
            int ny=p.y+dy[i];
            if(nx>=0 && nx<n && ny>=0 && ny<m && maze[nx][ny]==‘#‘ && dis[nx][ny]>dis[p.x][p.y]+1)
            {
                dis[nx][ny]=dis[p.x][p.y]+1;
                next.x=nx;
                next.y=ny;
                que.push(next);
            }
        }
    }
    int maxx=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(maze[i][j]==‘#‘)
                maxx=max(maxx,dis[i][j]);
    return maxx;
}
int main()
{
    int t;

    scanf("%d",&t);
    int ca=1;
    while(t--)
    {
        int ans=INF;
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>maze[i][j];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(maze[i][j]==‘#‘)
                {
                    for(int ii=0;ii<n;ii++)
                    {
                        for(int jj=0;jj<m;jj++)
                        {
                            if(maze[ii][jj]==‘#‘)
                            {
                                int temp=bfs(i,j,ii,jj);
                                ans=min(ans,temp);
                            }
                        }
                    }
                }
            }
        }
        if(ans==INF)
            ans=-1;
        printf("Case %d: %d\n",ca++,ans);
    }
}

原文地址:https://www.cnblogs.com/smallhester/p/9567845.html

时间: 2024-08-02 14:04:44

Fire Game FZU - 2150 (bfs)的相关文章

Fire Again CodeForces - 35C (BFS)

After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l