[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 = 2147483647to representINFas 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 ...
随机推荐
- DataView操作DataTable
1.DataView筛选数据 //假设有一个DataTable数据 DataTable dt = new DataTable(); //转成DefaultView DataView dv = dt.D ...
- Extjs 4学习2
主要学习采自:http://www.ishowshao.com/blog/2012/06/19/extjs-4-getting-started/ 用的sdk为extjs4.2.1 根据其中的提示装了一 ...
- Linux redhat
挂载U盘 fdisk -l 可以列出所有的分区,包括没有挂上的分区和usb设备.我一般用这个来查找需要挂载的分区的位置,比如挂上u盘. mount /dev/sdb1 usb/
- Java环境配置出现的问题及解决办法
我使用的安装文件,从博客园一个童鞋那里找到的,连接是http://www.cnblogs.com/SelectError/p/3205582.html#commentform 开发基础环境,版本为Ja ...
- CSS浮动元素的水平居中
方法一: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- Android 中文 API (29) —— CompoundButton
前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...
- 再次深入理解delphi的类
property WindowState: TWindowState read FWindowState write SetWindowState; {声明一个属性WindowState,它从字段FW ...
- #module-django.db.models
Models A model is the single, definitive source of information about your data. It contains the esse ...
- cygwin编译ffmpeg移植到android平台问题集锦
编译环境: windows xp Cygwin 1.1.3.1 NDK r9 1.提示各种command not found 比如 ./config.sh: line 6: $'--arch=arm\ ...
- 张江在线APP演示
app下载地址:https://itunes.apple.com/cn/app/zhang-jiang-zai-xian/id722630317?mt=8