Walls and Gates

You are given a m x n 2D grid initialized with these three possible values.

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.

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的更多相关文章

  1. 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的值. 最 ...

  2. [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 ...

  3. Walls and Gates

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

  4. LeetCode Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  5. 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 ...

  6. Walls and Gates 解答

    Question You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. spring集成 log4j + slf4j

    以maven web项目为例, 首先.在pom文件引入相关依赖,如下(spring官网文档有介绍): <dependencies> <!-- spring 相关 --> < ...

  2. Hessian(C#)介绍及使用说明

    什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...

  3. oracle创建实例SID

    用oracle用户登录 输入startx开发可视化界面,打开命令行模式 (如果只有壁纸,没有桌面图标和任务栏,按下 Ctrl + Alt + T 打开命令行) 输入dbca打开配置窗口 最后就各种下一 ...

  4. kill session真的能杀掉进程吗

    session1 确认sidSYS @ prod > select userenv('sid') from dual; USERENV('SID')-------------- 144 sess ...

  5. Template 模式

    Template 模式是很简单模式,但是也应用很广的模式.Template 是采用继承的方式实现算法的异构,其关键点就是将通用算法封装在抽象基类中,并将不同的算法细节放到子类中实现.Template ...

  6. Qt经典出错信息之”Basic XLib functionality test failed!”

    解决方法: 此完整出错信息是在./configure阶段Basic XLib functionality test failed!You might need to modify the includ ...

  7. Codeforces 474E - Pillars

    一眼看上去非常像最长不下降子序列. 然后比赛的时候对每个答案长度为k的序列,维护最后一个数的最大值和最小值. 当时不知道为什么认为从长度最长倒推至前面不会太长,于是心满意足地敲了个O(n^2).结果T ...

  8. Linux nohup命令详解

    nohup命令及其输出文件                                                                                       ...

  9. python - StringIO文本缓冲

    参考:http://pymotwcn.readthedocs.org/en/latest/documents/StringIO.html 类StringIO提供了一个在内存中方便处理文本的类文件(读, ...

  10. MVC 文本转换成html显示

    最近在学习ASP.NET MVC,项目中需要将后台传输的HTML文本在前台页面显示:@Html.Raw(HttpUtility.HtmlDecode(ViewBag.DisplayText)).记下来 ...