Walls and Gates
-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.
INF.INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
Understand the problem:
It is very classic backtracking problem. We can start from each gate (0 point), and searching for its neighbors. We can either use DFS or BFS solution.
public class Solution {
public void wallsAndGates(int[][] rooms) {
if (rooms == null || rooms.length == ) return;
int m = rooms.length, n = rooms[].length;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (rooms[i][j] == ) {
helper(i, j, , rooms);
}
}
}
}
private void helper(int row, int col, int distance, int[][] rooms) {
int rows = rooms.length, cols = rooms[].length;
if (row < || row >= rows || col < || col >= cols || rooms[row][col] == -) return;
if (distance > rooms[row][col]) return;
if (distance < rooms[row][col]) {
rooms[row][col] = distance;
}
helper(row - , col, distance + , rooms);
helper(row + , col, distance + , rooms);
helper(row, col - , distance + , rooms);
helper(row, col + , distance + , rooms);
}
}
BFS
对于bfs,因为我们是把所有的0点在最开始的时候加入到了queue里面,所以,当其中一个0点访问到一个空点的时候,那么我们一定可以说那是最短距离。所以,时间复杂度上,bfs比dfs好很多。
public class Solution {
public void wallsAndGates(int[][] rooms) {
Queue<int[]> queue = new LinkedList<int[]>();
int rows = rooms.length;
if (rows == ) {
return;
}
int cols = rooms[].length;
// 找出所有BFS的起始点
for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
if (rooms[i][j] == ) {
queue.offer(new int[]{i, j});
}
}
}
// 定义下一步的位置
int[][] dirs = {{-, }, {, }, {, -}, {, }};
// 开始BFS
while (!queue.isEmpty()) {
int[] top = queue.poll();
for (int k = ; k < dirs.length; k++) {
int x = top[] + dirs[k][];
int y = top[] + dirs[k][];
if (x >= && x < rows && y >= && y < cols && rooms[x][y] == Integer.MAX_VALUE) {
rooms[x][y] = rooms[top[]][top[]] + ;
queue.add(new int[]{x, y});
}
}
}
}
}
From:
http://buttercola.blogspot.com/2015/09/leetcode-walls-and-gates.html
https://segmentfault.com/a/1190000004184488
Walls and Gates的更多相关文章
- [Locked] Walls and Gates
Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...
- 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 ...
- 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 ...
随机推荐
- EF事务
var db = this.UnitOfWork as CodeFirstDbContext; using (var tan = db.Database.BeginTransaction()) { t ...
- [webgrid] – selecterow - (Get Selected Row from ASP.NET MVC 3 WebGrid)
Get Selected Row from ASP.NET MVC 3 WebGrid Abstract: The following article demonstrates how to get ...
- Node对象属性
1.Node对象属性一 * nodeName * nodeType * nodeValue * 使用dom解析html时候,需要ht ...
- http的六种请求方法
1.get: GET可以说是最常见的了,它本质就是发送一个请求来取得服务器上的某一资源.资源通过一组HTTP头和呈现数据(如HTML文本,或者图片或者视频等)返回给客户端.GET请求中,永远不会包含呈 ...
- 3步完成chrome切换搜索引擎
1.打开chrome://settings/,找到搜索 2.点击“管理搜索引擎”,出现弹窗. 增加搜索引擎,三个文本框分别输入:名称.快捷键.地址 3.在新的选项卡中,输入快捷键(如:github), ...
- HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
var divs = html.CssSelect("div"); //all div elementsvar nodes = html.CssSelect("div. ...
- Linux下,telnet命令如何退出
测试连接本地的memcached telnet 链接后是这样的: wangkongming@Vostro ~ $ telnet Trying 127.0.0.1... Connected to 127 ...
- Myeclipse中的web项目审查(jquery-2.1.1.min.js)出现错误
前言,本来在把web项目搞得好看一些,从网上下载了一个很炫酷的模板导入web中,无奈出现了错误,如下:
- nodejs开发指南读后感
nodejs开发指南读后感 阅读目录 使用nodejs创建http服务器; supervisor的使用及nodejs常见的调式代码命令了解; 了解Node核心模块; ejs模板引擎 Express 理 ...
- jelq
初级 The Newbie Routine 5 minutes hot wrap 5 minutes manual stretch (ten 30-second stretches) 10 minut ...