<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>BFS on 一共十七划</title>
		<link>https://noirzh.com/tags/bfs/</link>
		<description>Recent content in BFS on 一共十七划</description>
		<generator>Hugo</generator>
		<language>zh-CN</language>
		
		
		
		
			<lastBuildDate>Mon, 13 Nov 2023 09:23:49 +0800</lastBuildDate>
		
			<atom:link href="https://noirzh.com/tags/bfs/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>题解-逃离火灾-两次BFS-在路上不会被烧到,处理边界</title>
				<link>https://noirzh.com/solution-escape-the-spreading-fire/</link>
				<pubDate>Thu, 09 Nov 2023 09:23:43 +0800</pubDate>
				<guid>https://noirzh.com/solution-escape-the-spreading-fire/</guid>
				<description>&lt;blockquote&gt;&#xD;&#xA;&lt;p&gt;Problem: [2258. 逃离火灾](https://leetcode.cn/problems/escape-the-spreading-fire/description/)&lt;/p&gt;&#xD;&#xA;&lt;/blockquote&gt;&#xD;&#xA;&lt;p&gt;&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;&lt;strong&gt;思路&lt;/strong&gt;&lt;/h2&gt;&#xD;&#xA;&lt;p&gt;– 由题意可知需要计算一条最短路,耗时最短以保证后续火传过来的时候让火传递的时间足够长来接近答案&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 火的蔓延时间用DFS求解,可以求出来火到各个节点的最少用时&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 应该考虑每次刷新一下出发时间来看此次最短路是否被烧掉吗?其实不用&lt;/p&gt;&#xD;&#xA;&lt;p&gt;    – 只需要考虑在终点人能否比火早到即可&lt;/p&gt;&#xD;&#xA;&lt;p&gt;    – 假如人在路上遇到火,则火到终点的用时一定小于等于人&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 保证了到终点的时候火耗时大于人可以反推出&lt;/p&gt;&#xD;&#xA;&lt;p&gt;    1. 人和火相遇的路都能保证有如此长的等待时间&lt;/p&gt;&#xD;&#xA;&lt;p&gt;    2. 若人和火不相遇则显然&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;&lt;strong&gt;细节&lt;/strong&gt;&lt;/h2&gt;&#xD;&#xA;&lt;p&gt;1. 如果起点起火不可达&lt;/p&gt;&#xD;&#xA;&lt;p&gt;2. 对于只有人可达的情况,是等待多久都可以的情况&lt;/p&gt;&#xD;&#xA;&lt;p&gt;3. 重点:因为允许人和火同时进安全屋,但是不能要求其他格子里人和火同时到&lt;/p&gt;&#xD;&#xA;&lt;p&gt;    1. 考虑将终点旁边的两格当做终点,如果他们到达终点所剩下的时间大于等于1,显然可以在外面等一分钟&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;&lt;strong&gt;解题方法&lt;/strong&gt;&lt;/h2&gt;&#xD;&#xA;&lt;p&gt;– 两遍BFS求火和人到各个节点的距离&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 对于起点和终点的几种情况特判&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 查询以终点左边和上边的点是否允许多等一分钟&lt;/p&gt;&#xD;&#xA;&lt;p&gt;– 返回答案&lt;/p&gt;&#xD;&#xA;&lt;h2&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;/h2&gt;&#xD;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xD;&#xA;    def maximumMinutes(self, grid: List[List[int]]) -&amp;gt; int:&#xD;&#xA;        n=len(grid[0])&#xD;&#xA;        m=len(grid)&#xD;&#xA;        times=[[0x7fffffff]*n for _ in range(m)]&#xD;&#xA;        q=deque()&#xD;&#xA;        directions=[[0,-1],[0,1],[1,0],[-1,0]]&#xD;&#xA;        for i in range(m):&#xD;&#xA;            for j in range(n):&#xD;&#xA;                if grid[i][j]==1:&#xD;&#xA;                    q.append([i,j])&#xD;&#xA;                elif grid[i][j]==2:&#xD;&#xA;                    times[i][j]=-1#不可达&#xD;&#xA;        now=0&#xD;&#xA;        h=set()  &#xD;&#xA;        while q:&#xD;&#xA;            length=len(q)&#xD;&#xA;            for _ in range(length):&#xD;&#xA;                i,j=q.popleft()&#xD;&#xA;                times[i][j]=now&#xD;&#xA;                h.add((i,j))&#xD;&#xA;                for dx,dy in directions:&#xD;&#xA;                    x,y=i+dx,j+dy&#xD;&#xA;                    if 0&amp;lt;=x&amp;lt;m and 0&amp;lt;=y&amp;lt;n and grid[x][y]!=2 and (x,y) not in h:&#xD;&#xA;                        q.append((x,y)) &#xD;&#xA;            now+=1&#xD;&#xA;        #然后我们求人到各个节点用时&#xD;&#xA;        people=[[0x7fffffff]*n for _ in range(m)]&#xD;&#xA;        q.append((0,0))&#xD;&#xA;        h.clear()&#xD;&#xA;        now=0&#xD;&#xA;        h=set()  &#xD;&#xA;        while q:&#xD;&#xA;            length=len(q)&#xD;&#xA;            for _ in range(length):&#xD;&#xA;                i,j=q.popleft()&#xD;&#xA;                people[i][j]=now&#xD;&#xA;                h.add((i,j))&#xD;&#xA;                for dx,dy in directions:&#xD;&#xA;                    x,y=i+dx,j+dy&#xD;&#xA;                    if 0&amp;lt;=x&amp;lt;m and 0&amp;lt;=y&amp;lt;n and grid[x][y]!=2 and grid[x][y]!=1 and (x,y) not in h:&#xD;&#xA;                        q.append((x,y)) &#xD;&#xA;            now+=1&#xD;&#xA;        if times[-1][-1]&amp;lt;people[-1][-1] or people[-1][-1]==0x7fffffff:&#xD;&#xA;            return -1 &#xD;&#xA;        if times[0][0]==0:&#xD;&#xA;            return -1&#xD;&#xA;        if times[-1][-1]==0x7fffffff:&#xD;&#xA;            return 1000000000#火不可达,说明人到火这一路都没有火&#xD;&#xA;        maxNeed=times[-1][-1]-people[-1][-1]&#xD;&#xA;        if times[-1][-2]&amp;gt;people[-1][-2]+maxNeed or times[-2][-1]&amp;gt;people[-2][-1]+maxNeed:&#xD;&#xA;            return maxNeed&#xD;&#xA;        return maxNeed-1&#xD;&#xA;&lt;p&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description>
			</item>
	</channel>
</rss>
