[Locked] Walls and Gates
Walls and Gates
You are given a m x n 2D grid initialized with these three possible values.
-1
- A wall or an obstacle.0
- A gate.INF
- Infinity means an empty room. We use the value231 - 1 = 2147483647
to representINF
as you may assume that the distance to a gate is less than2147483647
.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF
.
For example, given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
分析:
深搜DFS即可,注意剪枝,注意overflow
代码:
//深搜,三种情况return: 房间编号已经比当前距离小的,墙和门,访问过的,而这三种情况可以用一个式子表示grids[i][j] < dist
void dfs(vector<vector<int> > &grids, int dist, int i, int j) {
if(grids[i][j] < dist)
return;
grids[i][j] = dist;
dfs(grids, dist + , i, j + );
dfs(grids, dist + , i + , j);
dfs(grids, dist + , i, j - );
dfs(grids, dist + , i - , j);
return;
}
void distanceFromGate(vector<vector<int> > &grids) {
if(grids.empty())
return;
//设立边界岗哨
grids.insert(grids.begin(), vector<int> (grids[].size(), -));
grids.push_back(vector<int> (grids[].size(), -));
for(auto &row : grids) {
row.insert(row.begin(), -);
row.push_back(-);
}
//从每个0开始进行递归
for(int i = ; i < grids.size(); i++)
for(int j = ; j < grids[].size(); j++)
if(grids[i][j] == )
dfs(grids, , i, j);
//除去边界岗哨
grids.erase(grids.begin());
grids.pop_back();
for(auto &row : grids) {
row.erase(row.begin());
row.pop_back();
}
return;
}
[Locked] Walls and Gates的更多相关文章
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- [LeetCode] Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- Walls and Gates
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...
- Walls and Gates 解答
Question You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or ...
- [Swift]LeetCode286. 墙和门 $ Walls and Gates
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- Walls and Gates -- LeetCode
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- [LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
随机推荐
- Quartz.NET配置(Log4net)
最近有个任务关于服务调度,想起以前看过Quartz.NET调度任务非常棒. 今天小试Quartz.NET,前面配置Quartz.NET很轻松,控制台也输出了.但是想配合Log4net来做日志文件,怎么 ...
- Html网页生成Pdf
在http://code.google.com/p/wkhtmltopdf/downloads/list下载安装程序. 1.添加引用 using System.Diagnostics; 添加引用 2. ...
- IDEA SVN1.8 问题解决
转自 http://blog.jetbrains.com/idea/2013/12/subversion-1-8-and-intellij-idea-13/
- Alljoyn 概述(2)
AllJoyn 基本概念 • 总线(Bus) – 实现P2P通信的基础 – AllJoyn 的底层协议类似于D-Bus,相当于是跨设备分布式的 D-Bus • 总线附件(Bus Attachment) ...
- C# var
VAR 是3.5新出的一个定义变量的类型其实也就是弱化类型的定义VAR可代替任何类型编译器会根据上下文来判断你到底是想用什么类型的 至于什么情况下用到VAR 我想就是你无法确定自己将用的是什么类型就可 ...
- PHP常用代码段:
1.PHP加密解密 function encryptDecrypt($key, $string, $decrypt){ if($decrypt){ $decrypted ...
- 在CMD下用java命令出现“找不到或无法加载主类”问题
解决思路: 从网上查找原因和解决方法,有提到环境变量classpath设置问题,但多次尝试问题依旧没有解决.然后使用java -cp %classpath; Hello执行,结果正确. 使用echo ...
- 高级停靠(Dock)技术的实现
高级停靠(Dock)技术的实现 介绍 所谓停靠就是可以用鼠标拖动窗体或者控件,并将其从一个父窗体移出或者移动到另一个父窗体上,可以按水平,垂直方向整齐排列, 并且可以停靠在分页控制组件上.下面的示意图 ...
- CentOS下命令行和桌面模式的切换方法(转载)
桌面模式和命令行模式的切换方法 用编辑器打开 /etc/inittab 文件(这里用的是vi,你可以选择你喜欢的): #vi /etc/inittab 打开效果图如下: 桌面模式 : 把光标所在 ...
- vim操作命令-笔记
显示行号:在vim命令行模式下输入 :set nu 或 :set number 取消显示行号:在vim命令行模式下输入 :set nonu 或 :set nonumber 查看文件编码格式: :set ...