FZU 2150 Fire Game (双起点BFS)

xxx@xxx.com lorem ipsm dolor sit amet 立即下载

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。

思路:把每一个草堆存成结构体,包括坐标,和第几步。然后把所有草堆存入vector,暴力枚举两个起点,然后在BFS时同时把两个起点加入队列,返回最后一个出队列的点的步数,然后判断是否合法,即每个草堆都被点燃。

代码如下:

#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
char mp[15][15];
int vis[15][15];
int dir[4][2]={1, 0, -1, 0, 0, 1, 0, -1};
int m, n;
struct node{
	int x, y, step;
};
vector<node> grass;
bool judge(){//判断是否所有草堆都被点燃 
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(mp[i][j]=='#' && !vis[i][j]){
				return false;
			}
		}
	}
	return true;
}
int BFS(node n1, node n2){//双起点BFS 
	int d = 0;
	queue<node> q;
	q.push(n1); 
	q.push(n2);
	while(!q.empty()){
		node nd1 = q.front();
		q.pop();
		if(vis[nd1.x][nd1.y])continue;
		vis[nd1.x][nd1.y] = 1;
		d = nd1.step;//取最后一次出对列的节点的步数 
		for(int i = 0; i < 4; i++){
			int xx = nd1.x + dir[i][0];
			int yy = nd1.y + dir[i][1];
			if(xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && mp[xx][yy] == '#'){
				q.push((node){xx, yy, nd1.step + 1});
			}
		}
	}
	return d;
}
int main(){
	int cases, i, j, ans, t = 0;
	cin>>cases;
	while(cases--){
		cin>>n>>m;
		t++;
		ans = INF;
		grass.clear();
		memset(vis, 0, sizeof(vis));
		for(i = 0; i < n; i++){
			scanf("%s", mp[i]); 
		}
		for(i = 0; i < n; i++){
			for(j = 0; j < m; j++){
				if(mp[i][j] == '#'){
					grass.push_back((node){i, j, 0});//将草堆结构体存入vector 
				}
			}
		}
		for(i = 0; i < grass.size(); i++){
			for(j = i; j < grass.size(); j++){
				memset(vis, 0, sizeof(vis));//记得初始化 
				grass[i].step = 0;//初始化 
				grass[j].step = 0;
				int temp = BFS(grass[i], grass[j]);//枚举每个起点进行BFS 
				if(judge()){//如果草堆都被点燃 
					ans = min(ans, temp);//取最小值 
				}
			}
		}
		if(ans == INF){//如果该值没有被刷新过 
			ans = -1;
		}
		printf("Case %d: %d\n", t, ans);
	}
	return 0;
}

Magnet磁力资源 Magnet磁力资源,超强的磁力资源器,磁力资源,磁力资源 立即下载

相关推荐

caoporn-video-address-extractor:双击以获取CaoPorn的视频地址

caoporn视频地址提取器 双击获取视频地址!

flash 小游戏大全

http://www.xiaoyouxi.com/http://www.4399.com/http://yule.sohu.com/flashgame.shtml   Flash游戏-最新FLASH-极酷动漫-搜狐娱乐

xubaogongChina的专栏 2万+

IAR9.30以上版本安装、注册、新建工程和配置过程详细介绍

IAR 一般是指一款嵌入式软件的集成开发环境,类似于 MDK-Keil 这款软件。IAR 对于不同的内核处理器,是对应不同的 IAR 软件的,IAR 到目前为止支持大部分的MCU,比如8051系列、ARM架构系列、MSP430系列、AVR系列等等这些常用的芯片架构。对于 ARM 架构的芯片,有对应的 IAR Embedded Workbench for ARM 软件平台,因为我主要是使用 ARM 架构芯片,下面安装、注册和使用都是基于这个版本进行介绍的。

luobeihai的博客 6万+

2025软考网络工程师--基础知识精讲视频

对软考网络工程师教材进行了详细解析,通过对考查知识点的细致讲解,让考生掌握解题思路和方法,做到举一反三,事半功倍。小任老师帮你把握考试重点,用短的时间,让你学到应该掌握的软考网络工程师内容。

Windows 11下manage-bde命令实战:BitLocker锁了别慌,手把手教你用命令行找回数据

本文详细介绍了在Windows 11系统下使用manage-bde命令解锁BitLocker加密驱动器的实战方法。通过命令行诊断、恢复密钥获取、解锁流程演示及高级故障排除,帮助用户在系统锁定后快速找回数据,特别适合IT技术人员和遇到紧急情况的用户。

weixin_30902251的博客 335

VideoPose3d:环境搭建+制作自己的视频

论文链接:3D human pose estimation in video with temporal convolutions and semi-supervised training 代码链接:https://github.com/facebookresearch/VideoPose3D 环境搭建:ubuntu20.04+pytorch1.8+cuda10.2+python3.6+anaconda 1.启动anaconda环境 新建环境命令: conda create -n pytorc.

willbetter01的博客 4945

FZU 2150 Fire Game 双起点bfs

FZU 2150 Fire Game 双起点bfs题意大概意思题解代码 链接: 原题网址. 题意 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 fi

zkpj12的博客 638

⑩电子产品拆解分析-家用无线遥控开关433Mhz

①免布线随意贴,装上就能使用解决单线开关烦恼;②遥控配对简单,无线通讯距离长,信号可穿墙;

a6662580的博客 4752

fzu 2150 Fire Game(双起点BFS)

传送门 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

lllllan 251

训练救星!EMA多尺度注意力让YOLOv11轻松攻克自定义数据集不涨点难题

在目标检测模型训练中,自定义数据集不涨点是阻碍算法落地的核心痛点之一。传统训练策略在数据分布特殊、样本量有限的场景下极易陷入收敛停滞,而 EMA(指数移动平均)结合多尺度注意力的方案,在自定义数据集上实现mAP 提升 6.5%,同时让训练曲线从“平缓停滞”转为“持续攀升”,彻底解决创新点在自有数据上的性能瓶颈。本教程将带你从原理到实践,通过 EMA 多尺度注意力为 YOLOv11 注入训练稳定性,让你的自定义数据集模型训练实现质的飞跃。EMA 衰减率:小样本数据集建议将decay。

FJN110的博客 161

医疗工具识别(手术器械计数):基于YOLOv8的智能手术器械管理系统

YOLOv8是Ultralytics公司于2023年推出的最新一代目标检测算法,相比前代YOLOv5,在精度和速度上都有显著提升。Anchor-free检测:不再使用预定义的anchor boxes,简化了检测流程更高效的骨干网络:采用CSPDarknet53作为特征提取网络多尺度特征融合:通过PANet结构实现更好的小目标检测损失函数优化:使用CIoU损失提高边界框回归精度YOLOv8提供了多种规模的模型(n/s/m/l/x),可根据实际需求在精度和速度之间进行权衡。

YOLO 485

FZU 2150 Fire Game(双起点bfs)

大同小异的bfs,同时把两个点压进队列就行了 做题真的要注意细节,不然wa到怀疑人生 https://vjudge.net/problem/FZU-2150 #include&lt;iostream&gt; #include&lt;cstring&gt; #include&lt;cstdio&gt; #include&lt;algorithm&gt; #include&lt;vector&...

STOP 201

UE5实现物体高亮描边效果(含UE相关源码浅析)

UE5实现物体高亮描边效果

专注GIS开发 7342

Fire Game FZU - 2150(双起点bfs)

两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。 Input 第一行,输入一个T,表示有T组测试数据。 每组数据包含n,m分别表示行和列 1 <= T <=100, 1 <= n <=10, 1 <= m <=10 O...

Jungle~St... 321

SAP GUI 经典事务在 Fiori Launchpad 中的穿越之道:原理、配置与实践指南

在许多企业的 S/4HANA 或 ECC 系统里,经典 SAP GUI 事务依旧承载着关键业务。如何把这些老将自然地嵌入到 Fiori Launchpad,让用户在统一入口里无缝切换新旧 UI,是项目中经常遇到的主题。本文结合社区博文、官方文档与实际项目经验,系统梳理这一过程的技术原理、配置步骤、性能优化以及常见坑点,并给出贴近中国读者的案例解析与改进建议。阅读完毕,你将不仅知道怎么做,还会理解。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 624

温度PID控制PLC程序

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

FZU - 2150 Fire Game bfs+双起点枚举

题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地。有墙壁‘·‘阻挡。初始可以从任意两点点火。问烧完最短的时间。若烧不完输出-1. 题解:由于100的数据量,直接暴力。枚举两个起点,推入队列,然后bfs。烧完就返回深度,更新一个min值。 坑:(帮同学照bug) return t.step+1;bfs后没有算上最后一步 ac代码 #defin...

Vincent19999999的博客 221

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

FZU 2150 Fire Game(暴力双起点+bfs)

题目通道 解题思路 先暴力取两个点,这里的n和m的上限都很小,不会炸。接着双起点bfs,把草烧到无法再烧的程度( 全部草烧着了,或许有些草无法被点燃 )。此时已经点燃的草vis的值是1。记录烧的最大时间 temp。再判断,草坪上的草是不是全部烧着了,状态(是草,是烧着了,对应的变量是 grid[i][j] == ‘#’ 和 vis[i][j] == 1 )。如果全部草烧着了, ans=min(an...

魔君的博客 190

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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

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

饮酒在风里的博客 345

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

1656

I - Fire Game FZU 2150 双起点的BFS

I - Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2150 Description Fat brother and Maze are playing a kind of special (hentai) game

qwesde 976

fzu2150Fire Game(双起点bfs)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 双起点bfs “#”是草,可以放两把火,求烧光草的最短时间。 枚举点火的位置,双起点bfs,更新结果。 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #include&l...

weixin_30596023的博客 118

FZU 2150 Fire Game (双起点BFS)

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

qmhna_的博客 349

FZU 2150 Fire Game(双起点)【BFS】

<题目链接> 题目大意: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。如不能烧完输出-1。 解题分析: 暴力枚举两个起点,然后用BFS求出这两个火源能够蔓延到最远的草地所花的时间,在那些能够烧完所有草地的情况中,选择用时最少的。 ...

weixin_30361641的博客 70

FZU2150 :Fire Game (双起点BFS)

传送门:点我 题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1 思路:暴力BFS,看到n和m都不大,直接把每个“#”都存下来,每次加入2个点进广搜搜能否烧完,然后更新ans即可。 #include <cstdio> #include <cstring> #includ...

weixin_30752377的博客 82
上一篇: ZOJ 3607 Lazier Salesgirl (贪心,模拟)
下一篇: POJ 1753 Flip Game (DFS + 暴力枚举)
LiQiyaoo
博客等级 码龄11年 337粉丝 206原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址