UVA - 11624 Fire!(两次BFS 两种解法)

UVA11624 Fire! 两次BFS 读懂题意很重要 Fire! Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze. Given Joe's location in 阅读详情

Problem B: Fire!

Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.

Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.

Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input Specification

The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R, C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
  • #, a wall
  • ., a passable square
  • J, Joe's initial position in the maze, which is a passable square
  • F, a square that is on fire
There will be exactly one J in each test case.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Output for Sample Input

3
IMPOSSIBLE

题目大意:
有个人被困于火场中。火焰每分钟蔓延1格,人每分钟移动一格,求出人是否会被困于火场,或者逃脱,如果逃脱,求出最短的时间。
其中
'#'表示墙壁,'F'表示火焰,'J'表示人的初始位置,'.'表示道路。
注意:不要被样例给误导了,可能刚开始火焰不止一处。


解析:
有两种解法。
解法一,
先对火BFS一次,求出每个点的最小着火时间,并用二维数组保存。
再对人BFS一次,求出走到边界的最少时间,每次如果移动的时间>=该点着火的时间,则continue。


解法二,
用一个结构体标记人或者是火,,先将火和人的位置分别bfs(先火再人),如果是火就bfs该点并用'F'占位,如果是人只能移动在'.'上,直到人到达边界或是被火围住无路可走时结束。


解法一

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node {
	int r,c,lev;
	Node(int r,int c,int lev) {
		this->r = r;
		this->c = c;
		this->lev = lev;
	}
};
const int INF = 0x3f3f3f;
const int N = 1010;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = { 0,-1, 0, 1};
int m,n;
char grid[N][N];
int vis[N][N];
int fire[N][N];
void bfs_fire() {
	memset(vis,0,sizeof(vis));
	queue<Node> q;
	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			if(grid[i][j] == 'F') {
				vis[i][j] = true;
				fire[i][j] = 0;
				q.push(Node(i,j,0));
			}	
		}	
	}
	int r,c,lev;
	while( !q.empty() ) {
		Node front = q.front();
		q.pop();
		for(int i = 0; i < 4; i++) {
			r = front.r + dr[i];
			c = front.c + dc[i];
			lev = front.lev + 1;
			if( r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c]) {
				continue;
			}
			vis[r][c] = true;
			fire[r][c] = lev;
			q.push(Node(r,c,lev));
		}
	}
}
int bfs(int sr,int sc) {	
	memset(vis,0,sizeof(vis));
	queue<Node> q;
	q.push(Node(sr,sc,0));
	vis[sr][sc] = true;
	int r,c,lev;
	while( !q.empty() ) {
		Node front = q.front();
		q.pop();
		for(int i = 0; i < 4; i++) {
			r = front.r + dr[i];
			c = front.c + dc[i];
			lev = front.lev + 1;
			if( r < 0 || r >= m || c < 0 || c >= n ) {
				return lev;
			}
			if(fire[r][c] <= lev || grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c] ) {
				continue;
			}
			vis[r][c] = true;
			q.push(Node(r,c,lev));
		}
	}
	return 0;
}
int main() {
	int t;	
	int sr,sc;
	while( scanf("%d",&t) != EOF) {
		while( t-- ) {
			memset(grid,0,sizeof(grid));
			memset(fire,0,sizeof(fire));
			scanf("%d%d",&m,&n);
			for(int i = 0; i < m; i++) {
				scanf("%s",grid[i]);
				for(int j = 0; j < n; j++) {
					fire[i][j] = INF; //初始化为最大
					if(grid[i][j] == 'J') {
						sr = i;
						sc = j;
					}	
				}
			}
			bfs_fire();
			int cnt = bfs(sr,sc);
			if(cnt) {
				printf("%d\n",cnt);
			}else {
				printf("IMPOSSIBLE\n");
			}
		}
	}
	return 0;
}


解法二

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node{
    int r,c,lev;
    bool fire;
    Node(int r,int c,int lev,bool fire) {
        this->r = r;
        this->c = c;
        this->lev = lev;
        this->fire = fire;
    }
};
const int dr[] = {-1, 0, 1, 0};
const int dc[] = { 0,-1, 0, 1};
const int N = 1010;
char grid[N][N];
int vis[N][N];
int m,n;
Node* q[N*N];
int bfs(int sr,int sc) {
    int front,rear;
    front = rear = 0;
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
			if(grid[i][j] == 'F') {   
				q[rear++] = new Node(i,j,0,true);
			}
        }
    }
    q[rear++] = new Node(sr,sc,0,false);
    vis[sr][sc] = true;
    int r,c,lev;
    while(front < rear) {
        Node* pre = q[front++];
        if(pre->fire) {
            for(int i = 0; i < 4; i++) {
                r = pre->r + dr[i];
                c = pre->c + dc[i];
                lev = pre->lev + 1;
                if(r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '#' || grid[r][c] == 'F') {
                    continue;
                }
                grid[r][c] = 'F';
                q[rear++] = new Node(r,c,lev,true);
            }
        }
        else {
             for(int i = 0; i < 4; i++) {
                r = pre->r + dr[i];
                c = pre->c + dc[i];
                lev = pre->lev + 1;
                if(grid[r][c] == '#' || grid[r][c] == 'F' || vis[r][c]) {
                    continue;
                }
                if(r < 0 || r >= m || c < 0 || c >= n) {
                    return lev;
                }
                vis[r][c] = true;
                q[rear++] = new Node(r,c,lev,false);
             } 
        }
    }
    return 0;
}
int main() {
    int t;
    int sc,sr;
    while( scanf("%d",&t) != EOF) {
        while(t--) {
            memset(grid,0,sizeof(grid));
            memset(vis,0,sizeof(vis));
			//memset(q,NULL,sizeof(q));
            scanf("%d%d",&m,&n);
            for(int i = 0; i < m; i++) {
                scanf("%s",grid[i]);
                for(int j = 0; j < n; j++) {
                    if(grid[i][j] == 'J') {
                        sr = i;
                        sc = j;
                    }
                }
            }
            int cnt = bfs(sr,sc);
            if(cnt) {
                printf("%d\n",cnt);
            }
            else {
                printf("IMPOSSIBLE\n");
            }
        }
    }
    return 0;
}



opencv实践项目-图片拼接之缝合线算法 opencv 图片缝合线算法 阅读详情

相关推荐

Python+ArcGIS实战:5步搞定土壤类型预测(附随机森林模型配置技巧)

本文详细介绍了如何结合Python与ArcGIS,利用随机森林模型进行土壤类型预测。通过5个核心步骤——数据预处理、样本创建、模型构建、空间预测与精度验证,构建了一套自动化、高精度的空间预测工作流,并提供了关键的模型配置与调参技巧,助力GIS开发者高效完成土壤制图任务。

weixin_29227585的博客 396

Fire! BFS预处理

题目: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in...

weixin_41988889的博客 281

零基础Python实战:手把手教你用PyGame制作动态烟花效果(附完整代码)

本文为零基础Python编程爱好者提供了一份详细的PyGame实战教程,手把手教你从环境搭建到代码实现,逐步构建一个动态烟花模拟器。教程涵盖了粒子系统、物理模拟(重力与阻力)、效果优化(拖尾与爆炸形态)以及完整的交互式主程序,帮助你通过Python烟花代码项目深入理解图形编程与面向对象设计。

weixin_29062255的博客 257

[kuangbin]专题一 简单搜索 Fire! UVA - 11624【BFS】

【题目描述】 Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in...

inv0ker的博客 234

EPLAN二次开发实战:从API接口到定制化功能的完整指南

本文为电气工程师和自动化设计师提供了一份EPLAN二次开发的完整实战指南。文章详细介绍了从脚本、插件到独立程序三种开发模式,重点讲解了如何使用C#或VB.NET通过API接口操作EPLAN数据模型,实现定制化功能以提升设计效率,并分享了开发环境搭建、调试及项目打包分发的避坑经验。

c6d7e8f9g的博客 1121

Gazebo仿真环境下的强化学习实现

实际上利用Gazebo进行强化学习无非是环境获取上的不同,Gazebo的环境控制需要使用ROS服务进行控制,状态可以通过Gazebo进行获取,同时某些必要数据需要从话题中获取,最重要的是组织获取 的数据(通过话题等)与控制Gazebo的仿真步骤之间的组合。强化学习的手段与传统算法无异,强化学习方法最重要的是要确定动作空间、状态空间、奖励空间,这三个空间是work的前提。在笔者自己的环境中,将获取里程计的话题以及获取激光点的数据替换成为了自己的本机的仿真环境匹配的话题。

Moresweet 猫甜 6450

UVA-11624 Fire!(bfs)

Problem Description: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Jo...

一只在校程序猿的技术贴 354

Python爬虫实战:美团商家数据采集系统

本文将详细介绍如何使用Python最新技术栈构建高效的美团商家信息采集系统。内容涵盖异步爬虫、反爬对抗、数据存储、可视化分析等完整流程,提供可落地的代码方案和工程实践。

2201_76125261的博客 2660

UVA - 11624 Fire!

Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you

weixin_51996479的博客 332

深度学习论文: Towards Total Recall in Industrial Anomaly Detection

在大规模工业制造中,能够准确检测出有缺陷的零件至关重要。我们在这项工作中面临的一个特殊挑战是冷启动问题:仅使用正常的示例图像来训练模型。虽然可以针对每个类别手动设计解决方案,但我们的目标是构建能够自动适用于多个任务的系统。最佳的方法是将ImageNet模型的嵌入特征与异常检测模型结合起来。在本文中,我们在这一研究基础上进行了扩展,并提出了PatchCore,它利用了一个具有最大代表性的正常补丁特征内存库。通过这种方法,我们能够更好地解决冷启动问题,并提高系统在多任务上的性能。

mingo_敏 1876

手把手教你学SOME/IP「1.0•概述」

本文介绍了SOME/IP协议的基本概念和主要特点。SOME/IP全称为"Scalable service-Oriented MiddlewarE over IP",是一种面向服务的车载嵌入式通信协议,支持远程过程调用、事件通知等功能。文章指出SOME/IP是AUTOSAR标准的一部分,在车载以太网架构中处于应用层位置,负责服务调度和通信封装。协议具有可扩展性强、资源消耗低、兼容性好等优势,能满足汽车电子领域对实时性、可靠性的严格要求。同时,文章也说明了SOME/IP的正确书写规范,并解释

nihaoljq2010的博客 679

uva 11624(BFS)

UVA 11624 题意:图中头几堆火,火是按四周蔓延的,然后判断J只能走‘.’,而且要在火蔓延之前才能通过,然后如果J在图的边界,那么就逃出了; 思路:对火堆进行BFS,得到火到达各个'.'的最短的时间,然后对J进行BFS,要在火到达之前进行通过; 注意:因为BFS的性质,先到达的就是最短的时间,然后对于没用火到达的地方,不用判断火的时间,防止因为初始化times数组使得时间一样引

Everything can be done! 377

基于Xilinx XDMA 的PCIE通信

基于Xilinx XDMA 的PCIE通信 概述:   想实现基于FPGA的PCIe通信,查阅互联网各种转载…基本都是对PCIe的描述,所以想写一下基于XDMA的PCIe通信的实现(PCIe结构仅做简单的描述(笔记),了解详细结构移至互联网)。实践实践!!! 说明: 参考文档: PCI Express Base Specification Revision 3.0 PCI Express Base Specification Revision 5.0 pg195-pcie-dma PCI Express体系

Crazzy_M的博客 1万+

UVa 11624 Fire

UVa 11624 Fire 题目大意: Joe在迷宫中工作。不幸的是,迷宫的一部分着火了,请帮助乔逃离迷宫。根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必须确定火焰烧到他之前,乔是否可以离开迷宫,如果能离开他能跑多快。Joe和火每分钟移动一个方格,上、下、左、右,四个方向中的一个。火势向四个方向同时蔓延。Joe可以从迷宫的任何一个边界逃离迷宫。无论是乔还是火都不会到达有墙的位置。 注意:可能会...

指弹键盘哼民谣 836

廖雪峰Python教程[完整版]

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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

UVA - 11624 Fire! (双BFS)

UVA - 11624 Fire! BFS UVA - 11624 题意 简单的BFS运用,注意火源不止一个且火要比人先行动。 分别给火和人写一个队列就行了。 #pragma GCC optimize(3)//O3 #pragma GCC optimize(2)//O2 #include<bits/stdc++.h> using namespace std; using ll = ...

qq_43586463的博客 1563

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

uva 11624 - Fire!(bfs)

题目链接:uva 11624 - Fire! 先从F开始,做一遍BFS,处理出每个位置着火的时间。再从J开始走,每次只能走到着火时间大于当前时间的点。 #include #include #include #include using namespace std; const int maxn = 1005; const int inf = 0x3f3f3f3f;

不慌不忙、不急不躁 568

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

UVA - 11624 Fire!(BFS+ 思维)

UVA - 11624 Fire! 题目 给一个迷宫,起始位置,还有着火点,每过一个时间,着火点都会蔓延,问最终能否从迷宫逃出。 分析 如果没有着火的情况,直接BFS模板题,加上着火的话,可以预处理每个点被蔓延的时间,到时候 bfsbfsbfs 能不能走出去的时候,不能走当前已经着火的地方。 预处理着火的地方,要将所有起始找着火点都加入初始队列,相当于多个源点。 代码 #include <c...

Stormjing的博客 313

UVA - 11624  Fire! 两次BFS

UVA - 11624  Fire!Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s lo...

GYH0730的博客 603

UVa 11624 Fire!(着火了!)

UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述   Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglect...

183

uva 11624 大火蔓延的迷宫 Fire!(两次bfs)

 题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,             其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。 思路是先用bfs预处理每个格子起火的时间,在来一次bfs走迷宫,入队时判断着火事件和父节点时间大小关系 代码如下: #include #include #include #inc

GODSPEED 1940

UVA 11624 Fire!(预处理bfs)

https://vjudge.net/problem/UVA-11624 UVa真的好卡,, 先预处理‘F’,地图上标记好每个位置火最早出现的时间,之后正常的跑bfs就行了,还是挺水的题 做题前不要莽,磨刀不误砍柴工,仔细想想再下手 最后一道简单搜索了啊,去刷进阶版! #include&lt;iostream&gt; #include&lt;cstring&gt; #inc...

STOP 510

UVA 11624 Fire! 两次BFS求解

题目链接:http://uva.onlinejudge.org/external/116/11624.pdf 题意:给定一个迷宫和一些着火点,火会不断想四周蔓延,求一条逃出迷宫的最短路径。 思路:先用一次BFS求出火蔓延到每个点的最短时间,可以把#的时间设为0,'.'火到不了的,设为oo(这里应该注意一下,WA了一次)。后面就是常规的BFS求最短路了。 代码: #include #

catch_catch的专栏 662

UVA 11624 10054

图论题目小结,考察了BFS和欧拉回路的输出。题号:UVA 11624 10054

那些年我们切过的题 878

UVA 11624 Fire!(BFS)

UVA 11624 https://vjudge.net/contest/281425#problem/J Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. ...

夏日蹲坑的博客 164

UVA11624 Fire! (BFS)

Fire! Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze. Given Joe’s location in th...

C'est la vie! 278

UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动。火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间。 题解:先考虑模拟火,肯定是bfs(每次把同一时间着火的格子pop出来,再将它们周围的格子的t加一push进去)     然后考虑怎么模拟人,现在人处在一个会变化的迷宫中。貌似很复杂。     ...

Vincent19999999的博客 228
上一篇: UVA 10341 Solve It (解方程 二分查找+精度)
下一篇: 二叉树遍历(前序,中序,后序)
HelloWorld10086
博客等级 码龄12年 88粉丝 614原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值