FZOJ Problem 2150 Fire Game

                                                                                                                                          Problem 2150 Fire Game

Accept: 2185    Submit: 7670
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

题意:两个“变态”在给定的区域内烧草坪,给定区域为N*M的矩形阵,起先两个人同时各选定矩阵中的一块草坪并开始烧,并且火会蔓延,只考虑当前草坪的上下左右四个格子,如果这些临近的格子中有草坪,那么火会蔓延至临近的格子中,此时火延续的时间加1.

问两个人分别应该选定哪两块草坪开始烧,使得火以最短的时间烧完矩阵中所有的草坪,如果火灭了任然还有草坪没被烧,则判定为失败,输出-1,否则输出火的最短延续时间.

思路:广度优先搜索,从两个点起始点同时开始搜索即可。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
using namespace std;
const int N_MAX = 10+ 2;
int N,M;
char field[N_MAX][N_MAX];
bool vis[N_MAX][N_MAX];
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
queue<pair<int,int> >que;
int num[N_MAX][N_MAX];

int bfs(int x1,int y1,int x2,int y2) {//返回 烧完所有草需要的时间
    memset(vis, 0, sizeof(vis));//记录走过的点
    memset(num, 0, sizeof(num));//记录到达某点的时间
    int Max=0;
    que.push(make_pair(x1,y1));
    que.push(make_pair(x2, y2));
    vis[x1][y1] = vis[x2][y2] = 1;
    while (!que.empty()) {
        int xx=que.front().first;
        int yy = que.front().second;
        que.pop();
        for (int i = 0; i < 4; i++) {
            int x = xx + dx[i];
            int y = yy + dy[i];
            if (x >= 0 && x < N&&y >= 0 && y < M&&!vis[x][y]&&field[x][y] == ‘#‘) {
                vis[x][y] = true;
                que.push(make_pair(x, y));
                num[x][y] = num[xx][yy]+1;
                if (Max < num[x][y])Max = num[x][y];
            }
        }
    }
    for (int i = 0; i < N;i++)
        for (int j = 0; j < M; j++)
            if (field[i][j] == ‘#‘&&!vis[i][j]) {
                Max = INT_MAX;
            }

    return Max;
}

int main() {
    int T,number;
    scanf("%d", &T);
    int cs = 0;
    while (T--) {
        number = 0;
        cs++;
        scanf("%d%d",&N,&M);
        memset(field, 0, sizeof(field));
        for (int i = 0; i < N;i++) {
            for (int j = 0; j < M;j++) {
                scanf(" %c",&field[i][j]);
                if (field[i][j] == ‘#‘)
                    number++;
            }
        }
        if (number <= 2) {//!!!!草坪数小于2不用搜索了
            printf("Case %d: %d\n", cs, 0);
            continue;
        }
        int min_time=INT_MAX;
        for (int i = 0; i < N*M;i++) {
            int x1 = i / M; int y1 = i%M;
            if (field[x1][y1] != ‘#‘)continue;
            for (int j = i+1; j < N*M;j++) {
                int x2 = j / M; int y2 = j%M;
                if (field[x2][y2] != ‘#‘)continue;
                int tmp= bfs(x1, y1, x2, y2);
                if (tmp < min_time)
                    min_time = tmp;
            }
        }

        if (min_time == INT_MAX)min_time = -1;
        printf("Case %d: %d\n",cs,min_time);

    }
    return 0;
}
时间: 2024-10-24 01:12:37

FZOJ Problem 2150 Fire Game的相关文章

FZU Problem 2150 Fire Game (双起点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 a

FZU Problem 2150 Fire Game

这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由于数据比较小,所以可以暴力的去解,不过先判断一下联通块可以解决一小部分问题的. #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std;

FZU 2150 Fire Game(DFS+BFS)

题意  在n*m个格子组成的草地上   你可以选择两个是草('#')的格子点燃  每个点燃的格子在下一秒其四个相邻的是草的格子也会被点燃   问点燃所有的草至少需要多少秒 DFS和BFS的综合  如果'#'连通块的数量大于2个是肯定不能点燃所有的  先dfs判断连通块个数  再bfs找出选哪两个格子可以最快把草烧完 #include <map> #include <cstdio> #include <cstring> using namespace std; const

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 2150 Fire Game --两点同步搜索

枚举两点,然后同步BFS,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #define Mod 1000000007 using namespace std; struct Po

FZOJ 2150 Fire Game (DFS + BFS)

题目链接:Fire Game 题意:一块n*m的矩形中,'#'代表是草,'.'代表空地,空地点不着火.两个人同时开始点火,问最短多少时间能把所有草地点着,不能输出'-1'. 解析:先用dfs预判断草地的连通块数,超过2则无法全部点燃 任选两个草地作起点,两者看作是一个整体,用bfs搜到起点到所有草地的最短时间,然后保留其中最长的时间 在所有的最长时间中,选择最短的,即为所求. AC代码: #include <cstdio> #include <cstring> #include &

(FZU 2150) Fire Game (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 a

(简单) FZU 2150 Fire Game ,Floyd。

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

FZU 2150 Fire Game(BFS)

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