Walls and Gates -- LeetCode

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

思路:BSF。

 1 class Solution {
 2 public:
 3     void wallsAndGates(vector<vector<int>>& rooms) {
 4         const int inf = INT_MAX;
 5         queue<pair<int, int> > q;
 6         int height = rooms.size();
 7         int width = height > 0 ? rooms[0].size() : 0;
 8         for (int i = 0; i < height; i++)
 9             for (int j = 0; j < width; j++)
10                 if (rooms[i][j] == 0) q.push(make_pair(i, j));
11         int rowChange[] = {0, -1, 0, 1};
12         int colChange[] = {1, 0, -1, 0};
13         while (!q.empty()) {
14             pair<int, int> cur = q.front();
15             q.pop();
16             int row = get<0>(cur), col = get<1>(cur);
17             for (int i = 0; i < 4; i++) {
18                 int nextRow = row + rowChange[i];
19                 int nextCol = col + colChange[i];
20                 if (nextRow > -1 && nextRow < height &&
21                     nextCol > -1 && nextCol < width && rooms[nextRow][nextCol] == inf) {
22                         rooms[nextRow][nextCol] = rooms[row][col] + 1;
23                         q.push(make_pair(nextRow, nextCol));
24                     }
25             }
26         }
27     }
28 };
时间: 2024-08-09 10:34:50

Walls and Gates -- LeetCode的相关文章

[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 value 231 - 1 = 2147483647 to represent INF as you may assume that the

LeetCode Walls and Gates

原题链接在这里:https://leetcode.com/problems/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 value 231 - 1 = 2147483647 t

LeetCode 286: Walls and Gates

Note: Need to skipp the 0 element update room but cannot skip BFS from that point class Solution { public void wallsAndGates(int[][] rooms) { if (rooms.length == 0 || rooms[0].length == 0) { return; } for (int i = 0; i < rooms.length; i++) { for (int

[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 obstacle. 0 - A gate. 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 ga

286 Walls and Gates

You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle. - A gate. 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

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 value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a ga

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

【LeetCode】BFS(共43题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较对象是 左子树的左儿子和右子树的右儿子, 左子树的右儿子和右子树的左儿子.不要搞错. // 直接中序遍历的话会有错的情况,最蠢的情况是数字标注改一改.. 1 /** 2

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷