[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 ...
随机推荐
- oracle如何获取当年第一月,如今年是2015年,则需获取 201501
当年第一个月 SQL> select to_char(sysdate,'yyyy')||'01' from dual;TO_CHA ------ 201501当前年,月 SQL> sele ...
- 英语学习[ZZ]
本文作者三年间从四级勉强及格到高级口译笔试210,口试232.找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考了.虽不敢说方法一定是最好的,但从现在开始随便谁不要再去找学习资料,每天花两个钟 ...
- 【转】 wpf系列-入门
转自:http://www.cnblogs.com/huangxincheng/category/388852.html 8天入门wpf—— 第八天 最后的补充 摘要: 从这一篇往前看,其实wpf ...
- 【POJ1417】【带标记并查集+DP】True Liars
Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...
- How far away ?
#include<iostream> #include <algorithm> using namespace std; const int M=40010; int dis[ ...
- 人人网FED CSS编码前端开发规范
文件相关规范 1.文件名必须由小写字母.数字.中划线-组成 2.文件必须用utf-8编码 3.文件引入可通过外联或内联方式引入: 3.1 外联方式:<link rel=”stylesheet” ...
- angularJS中如何写自定义指令
指令定义 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能 例如,ng-click可以让一个元素能够监听click事件,并在接收到事件的时候执行angularJS ...
- 【技术宅11】php入门运算
//1.空bool $a=''; $b=NULL; $c=false; $d=0; $e='0'; $f=array(); $g=array(array()); $h='NULL'; var_dump ...
- 我摘录的js代码
1.修改样式 document.getElementByIdx( "div1").style.display = "none"; 2.鼠标悬停图标变小手 sty ...
- php——composer 1、安装使用
Composer 是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件. 系统要求 运 ...