Fire Game FZU - 2150

fzu 2150 Fire Game 【技巧BFS】 题目: fzu 2141 Sub-Bipartite Graph (贪心) 阅读详情

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的地方,上面有草坪和空地,#表示草坪, 先从中任选两块草坪开始烧,火势每过一秒会向上下左右四个方向蔓延,问烧完所有草坪的最小时间。输入保证最小存在一块草坪。


分析:

当只有一块草坪时,直接输出0就好(WA了一次,忘了这一点了)。 两个难点。第一个难点就是如何选两个点,由于n和m最大就是10,所以可以枚举每个点。这里我选择用vector存储每个草坪(自我感觉会快一点,不用遍历数组)。 还有一个难点是如何描述两个点同时开始烧的这个状态,或者说怎么确定最小值。看上去要两个点同时进行BFS,其实不用(同时BFS实现上也困难),我们只需要先将初始的两个点放进队列里,再一个一个进行BFS即可,这里我选择用一个tempvis数组表示每个点是否被烧过(烧过为1,没有烧过或者是空地就为0),这样就保证了不会重复走,也就相当于保证了走的顺序。用一个cnt记录草坪的总数量,tcnt记录走过的草坪的数量。当队列为空的时候我们并不知道是否还有剩余的草坪,所以只有当tcnt == cnt的时候才能说明所有的草坪都被烧光,此时记录一下时间。

坑点: x,y 与 行和列 容易搞反!






#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
typedef pair<int,int> P;
struct Point {
    int x,y;
    int step;
};
int n,m;
int tcnt;
char maze[10][10];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool vis[10][10];
queue<Point> q;
bool tempvis[10][10];
void bfs(Point p){
    tempvis[p.y][p.x] = 1;
        for(int i=0;i<4;i++){
            Point dp = p;
            dp.x += dx[i];
            dp.y += dy[i];
            if(dp.x >= 0 && dp.x < m && dp.y >= 0 && dp.y <n && tempvis[dp.y][dp.x] == 0){
                dp.step++;
                tcnt++;
                q.push(dp);
                tempvis[dp.y][dp.x] = 1;
            }
        }
}
void init(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            tempvis[i][j] = vis[i][j];
        }
    }
}
void print()
{
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout << tempvis[i][j] <<' ';
        }
        cout << '\n';
    }
}
int main()
{
    int T;
    cin >> T;
    int k=0;
    while(T--){
        vector<pair<int,int> > coor;
        cin >> n >> m;
        int cnt = 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin >> maze[i][j];
                if(maze[i][j] == '#'){
                    coor.push_back(P(i,j));
                    cnt++;
                    vis[i][j] = 0;
                }
                else vis[i][j] = 1;
            }
        }
        if(cnt == 1){
            k++;
            cout << "Case "<<k << ": ";
            cout << 0 << endl;
            continue;
        }
        int ans = INF;			//初始化成无限大,若最后结果还是无限大则输出-1
        int res = NINF;
        for(int i=0;i<coor.size();i++){
            for(int j=i+1;j<coor.size();j++){
                init();     //每次更新tempvis
                //print();
                tcnt = 2;
                Point p1,p2;
                p1.y = coor[i].first;
                p1.x = coor[i].second;
                p2.y = coor[j].first;
                p2.x = coor[j].second;
                tempvis[p1.y][p1.x] = 1;
                tempvis[p2.y][p2.x] = 1;
                p1.step = 0;
                p2.step = 0;
                q.push(p1);
                q.push(p2);
                while(q.size()!=0){
                    Point temp = q.front();
                    q.pop();
                    bfs(temp);
                    if(q.size() == 0){    //队列为空时,最后一个元素的步数即为时间,但注意此时并不知道是否烧光了所有草坪。
                        res = temp.step;
                    }
                }
                //cout << "tcnt = " << tcnt << endl;
                if(tcnt == cnt){    		//判断是否烧光了所有草坪 
                    ans = min(ans,res);
                }
            }
        }
        k++;
        cout << "Case " << k << ": ";
        if(ans == INF) cout << -1 << endl;
        else cout << ans << endl;

    }
}


Carla-UE4Editor导入RoadRunner地图文件(保姆级教程) 以Carla官网导入RoadRunner地图的标准文档为参考,里面增添了很多细节和注意事项,遇到问题的小伙伴过来看看吧。 阅读详情

相关推荐

新年美食鉴赏——基于注意力机制CBAM的美食101分类

新年美食鉴赏——基于注意力机制CBAM的美食101分类一、数据预处理1.数据集介绍2.读取标签3.统一命名4.整理图片路径5.划分训练集与验证集6.定义美食数据集二、注意力机制1.简介2.卷积块注意力网络——CBAM通道注意力模块CAM空间注意力模块SAM3.将通道、空间注意力模块合并三、基于卷积块注意力模块的ResNet-CBAM1.代码实现2.模型测试3.查看网络结构4.模型训练5.模型预测四、总结与升华个人简介 春节是一年当中最隆重、最盛大的节日,吃得最好的一餐可能就是年夜饭了,想想就很激动呢!随着新

zbp_12138的博客 1万+

FZU 2150 Fire Game(点火游戏)

FZU2150FireGame(点火游戏) TimeLimit:1000mSecMemoryLimit:32768KB ProblemDescription - 题目描述   Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...

221

Windowns下Git环境配置-命令行方式

文章目录一、配置Git1.1 下载Git并安装1.2 配置本地Git用户名和邮箱1.3 注册GitHub二、生成本地公钥,放到GitHub上-SSH Key2.1 生成本地公钥命令2.2 查看生成的公钥2.3 在GitHub放上公钥并测试连通性三、创建项目实操四、可能出现的问题4.1.push不成功4.2.提示SSL的问题4.3.域名不能解析的问题4.4.如果github连接不上的问题 一、配置Git 1.1 下载Git并安装 Git官网 默认安装,根据需要修改安装路径 1.2 配置本地Git用户名和邮箱

学习工作笔记 1135

fzu 2150 Fire Game

fzu 2150 Fire Game 题目大意: 给出一个m*n的图,‘#’表示草坪,‘ . ’表示空地,然后可以选择在任意的两个草坪格子点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小? 具体思路: 数据范围较小,直接枚举两点选择的任意情况,分别进行bfs。取最优解就行 详细: 枚举两个人可能选的坐标情况,作为不同的初始状态 对每个初始状态进行bfs搜索,得到...

指弹键盘哼民谣 749

机器学习发展历史及现状(重要的期刊,会议,大牛总结)

1 引言 机器学习是指通过计算机学习数据中的内在规律性信息,获得新的经验和知识,以提高计算机的智能性,使计算机能够像人那样去决策。随着各行业对数据的需求量增多,对处理和分析数据的效率要求变高,一系列机器学习算法应运而生。机器学习算法主要是指通过数学及统计方法求解最优化问题的步骤和过程。针对不同的数据和不同模型需求,选择和使用适当的的机器学习算法可以更高效地解决一些实际问题。本文将从机器学习的起源...

Oscar2018的博客 5万+

雷达仿真:FMCW TDMA-MIMO 3D点云获取方法

2 信号建模摘要:通过设置时分复用(TDMA)波形结合FMCW毫米波雷达仿真获取目标的距离、速度和方位角3D点云信息。在本案例中,首先通过发射天线交替发射FMCW波形并通过接收天线依次接收回波信号从而得到雷达原始数据,其次对获取的原始数据依次经行距离、速度、角度估计从而输出目标的3D点云。阅读本文档,你将对TDMA-MIMO雷达的基本原理、毫米波雷达距离估计、速度估计以及角度估计的基本原理有所了解,另外CA-CFAR恒虚警以及MUSIC-DOA估计在本案例中也有所涉及。1 TDMA-MIMO原理。

Poulen的博客 6958

I - Fire Game FZU - 2150(多起点BFS)

I - Fire Game FZU - 2150 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 jus...

石旭先森的博客 636

字节跳动已上岸测试工程师的面试经验

这个时候发面经已经有点晚了,所以现在打算让这篇面经主要面向即将秋招的童鞋,当然现在还在春招的童鞋也可以看哈,我和大家一起聊聊关于测试的二三事。   一、测试工程师的工作是什么?   测试工程师简单点说就是找bug,然后反馈给开发人员,不要小看这个工作。   首先很明显的bug开发人员有时候自己就能找到,测试人员要有比开发人员更加全面的想法才能找到深层次的问题点,   其次,要端正一个态度就是测试人员不是一个电脑小白,相反国外顶级的测试开发工程师同时也是顶级的开发工程师甚至更厉害,所以作为一个测试工程师一定要

m0_60745607的博客 2933

FZU - 2150 Fire Game

FZU - 2150 Fire Game 题目 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

jame_19的博客 781

ODrive0.5.5源码分析(1) ADC的处理

odrive 0.5.5 ADC

danger的博客 4307

剪映自动化工具来了:AI帮你自动剪辑成片

🎬分享一个剪映自动化剪辑工具!AI 自动帮你完成从写文案、配音、加字幕、选音乐、上特效到最终导出的整套流程🎈

XiaoqiangClub的博客 855

FZU 2150 Fire Game

两个点对着BFS。。。。 Problem 2150 Fire Game Accept: 21    Submit: 45 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Fat brother and Maze are playing a kind of special (

码代码的猿猿 2666

YOLO + Qwen3-VL-Seg 深度诊断:双引擎协同完成AI质检

本文提出了一种创新的工业质检双引擎架构,结合YOLO实时检测与Qwen3-VL-Seg精细化诊断,有效解决了传统检测方法中检测与诊断割裂的问题。YOLO部署在边缘端实现高效定位(3.2MB模型,<10ms延迟),Qwen3-VL-Seg在云端完成语义级缺陷分析(仅17M参数的解码器实现像素级分割)。该方案通过6步闭环流程,从实时感知到工艺反哺,显著提升了工业质检的准确性和可解释性,为智能制造提供了从"发现"到"解决"的完整技术路径。

mingo_敏 221

FZU 2150 Fire Game (BFS)

题目链接: FZU 2150 Fire Game (BFS) 题目大意: n* m 场地,#表示草坪 ,每次选两个草坪,可以向周围四块草丛扩展,扩散时间为1秒,求最少时间,扩散整个草坪. 思路: 穷举:两个点组合,之后bfs选最小 Description Fat brother and Maze are playing a kind of special (hentai) game

Dextrad_ihacker的博客 1162

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

FZU 2150 Fire Game(BFS)

题目链接:FZU 2150 Fire Game BFS。

fobdddf的专栏 2529

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

fzu 2150 Fire Game(BFS)

题目链接:fzu 2150 Fire Game 题目大意:有一块空地上有些草堆‘#’,两个小伙伴想要在这片地上玩耍,然后就要烧掉这些草堆,相邻的草堆可以相互引燃,两个人可以分别选择一块草堆点燃,问说最少需要多少时间才能烧光草堆(每人只能点一次) 解题思路:枚举两个位置同时做为起点,然后BFS,注意有一个坑点就是说草堆的个数少于2的时候,枚举不到两个起点,但是是可以烧光草堆的。

不慌不忙、不急不躁 2374

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

FZU-2150 Fire Game

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

樱花满地集于我心,楪舞纷飞祈愿相随 3650

【简单搜索】FZU 2150 Fire Game详解

Fire Game 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

Qiuker_jl的博客 711

【补题日记】[FZU2150]Fire Game

Pro FZU2150 Sol 之前做搜索的训练的时候这个题就A了 后来提交kuangbin的题单的时候又T了 发现 原来真的可以随缘AC(逃 然后不得不对这个程序进行优化 优化的地方在于:枚举每一个两个火的时候只需要搜索一个直角三角形就行(而不是n×m) 这样时间就少了一大半 我还试过是不是输入cin太慢 改为scanf和getchar之后时间是一样的 思路不用多说 裸的bfs 但就是细节处理不好容易T Code #include<iostream> #include<cstdio&gt

cls1277的博客 199

FZU 2150 Fire Game

原题: 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 grid

@fei 219

FZU Problem-2150 Fire Game

FZU Problem-2150 Fire Game FZU入口:http://acm.fzu.edu.cn/problem.php?pid=2150 AC代码 #include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<algorithm>...

恒治学 197

FZU-Problem 2150 Fire Game

Problem 2150 Fire Game Accept: 392 Submit: 1502 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

有一种力量叫乐观 2371
上一篇: priority_queue practice
下一篇: 带权并查集 shuoj1880
mwhybmj
博客等级 码龄10年 8粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值