kuangbin专题一 简单搜索 点火游戏(FZU-2150)

Fire Game

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.

Sampleinput

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sampleoutput

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


题目大意

给定一个 n * m 的矩阵,'#'代表草地,'.'代表空地,你可以同时最多选择点燃两个草地,每过一分钟后点燃处的上、下、左、右四个方位的草地也会被点燃,问点燃所有草地的最短时间为多少。注意如果没有草地或者无法燃烧所有草地,那么需要返回-1。



解题思路

我一开始想的是先求出所有连通块的数量,然后判断一下一开始需要点燃一个草地就足够还是说需要点燃两个,但是这个思路反而走偏了。首先注意数据范围还是比较小的,所以可以存储所有草地位置,然后枚举点燃每一对不同草地求出最小步数即可,但是要注意只有一块草地的情况,此时只要直接点燃其即可,所以此时要返回0。

枚举两两不同的草地时,千万不要写成都从0开始枚举,这样造成了重复计算,而且有可能TLE。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 11;
char g[N][N];
int dist[N][N];
int n, m, cnt;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int bfs(pii a, pii b){
    int step = 0, sum = 0;
    memset(dist, 0x3f, sizeof(dist));
    queue<pii> q; q.push(a);
    if(a != b) q.push(b);
    dist[a.x][a.y] = dist[b.x][b.y] = 0;

    while(!q.empty()){
        auto [x, y] = q.front();
        q.pop();

        step = max(step, dist[x][y]); //记录最远距离
        sum ++ ;         //统计访问的草地个数

        for(int k = 0; k < 4; k ++ ){
            int nx = x + dx[k], ny = y + dy[k];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;  //出界
            if(g[nx][ny] == '.' || dist[nx][ny] != inf) continue; //不是草地 或 已经被访问过

            q.push({nx, ny});
            dist[nx][ny] = dist[x][y] + 1;
        }
    }

    return sum == cnt ? step : inf;
}

int solve(){
    int ans = inf;
    vector<pii> grass;
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < m; j ++ ){
            cin >> g[i][j];
            if(g[i][j] == '#') grass.push_back({i, j});
        }

    if(grass.empty()) return -1;  //无草地直接返回-1
    if((int)grass.size() == 1) return 0;  //只有一个草地返回0

    cnt = (int)grass.size();
    //枚举选择两个不同的草地
    for(int i = 0; i < cnt; i ++ )
        for(int j = i + 1; j < cnt; j ++ )
            ans = min(ans, bfs(grass[i], grass[j]));

    return ans == inf ? -1 : ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int T; cin >> T;
    for(int id = 1; id <= T; id ++ ){
        cin >> n >> m;
        int res = solve();
        printf("Case %d: %d\n", id, res);    
    }

    return 0;
}
posted @ 2023-04-15 19:34  MarisaMagic  阅读(11)  评论(0编辑  收藏  举报