Leetcode 286. Walls and Gates
2016-01-29 05:45
363 查看
You are given a m x n 2D grid initialized with these three possible values.
you may assume that the distance to a gate is less than
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with
For example, given the 2D grid:
After running your function, the 2D grid should be:
public class Solution {
public void wallsAndGates(int[][] rooms) {
if(rooms == null || rooms.length == 0) {
return;
}
Queue<int[]> queue = new LinkedList<>();
for(int i = 0; i < rooms.length; i++) {
for(int j = 0; j < rooms[0].length; j++) {
if(rooms[i][j] == 0) {
queue.add(new int[]{i, j});
}
}
}
while(!queue.isEmpty()) {
int[] gate = queue.poll();
int row = gate[0], col = gate[1];
if(row > 0 && rooms[row - 1][col] == Integer.MAX_VALUE) {
rooms[row - 1][col] = rooms[row][col] + 1;
queue.add(new int[]{row - 1, col});
}
if(row < rooms.length - 1 && rooms[row + 1][col] == Integer.MAX_VALUE) {
rooms[row + 1][col] = rooms[row][col] + 1;
queue.add(new int[]{row + 1, col});
}
if(col > 0 && rooms[row][col - 1] == Integer.MAX_VALUE) {
rooms[row][col - 1] = rooms[row][col] + 1;
queue.add(new int[]{row, col - 1});
}
if(col < rooms[0].length - 1 && rooms[row][col + 1] == Integer.MAX_VALUE) {
rooms[row][col + 1] = rooms[row][col] + 1;
queue.add(new int[]{row, col + 1});
}
}
}
}This approach is to use the BFS search, this is quite reasonable, because it needs to return the maximum value to the gate position
-1- A wall or an obstacle.
0- A gate.
INF- Infinity means an empty room. We use the value
231 - 1 = 2147483647to represent
INFas
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 -10 -1 3 4
public class Solution {
public void wallsAndGates(int[][] rooms) {
if(rooms == null || rooms.length == 0) {
return;
}
Queue<int[]> queue = new LinkedList<>();
for(int i = 0; i < rooms.length; i++) {
for(int j = 0; j < rooms[0].length; j++) {
if(rooms[i][j] == 0) {
queue.add(new int[]{i, j});
}
}
}
while(!queue.isEmpty()) {
int[] gate = queue.poll();
int row = gate[0], col = gate[1];
if(row > 0 && rooms[row - 1][col] == Integer.MAX_VALUE) {
rooms[row - 1][col] = rooms[row][col] + 1;
queue.add(new int[]{row - 1, col});
}
if(row < rooms.length - 1 && rooms[row + 1][col] == Integer.MAX_VALUE) {
rooms[row + 1][col] = rooms[row][col] + 1;
queue.add(new int[]{row + 1, col});
}
if(col > 0 && rooms[row][col - 1] == Integer.MAX_VALUE) {
rooms[row][col - 1] = rooms[row][col] + 1;
queue.add(new int[]{row, col - 1});
}
if(col < rooms[0].length - 1 && rooms[row][col + 1] == Integer.MAX_VALUE) {
rooms[row][col + 1] = rooms[row][col] + 1;
queue.add(new int[]{row, col + 1});
}
}
}
}This approach is to use the BFS search, this is quite reasonable, because it needs to return the maximum value to the gate position
相关文章推荐
- Leetcode 296 Best Meeting Point
- [Regular Expressions] Introduction
- Leetcode 252 Meeting Rooms
- hdu 1806 RMQ
- hdu 1806 RMQ
- myeclipse 常规web项目创建
- Balanced Binary Tree -- LeetCode
- xfire客户端调用cxf服务端,一直报错
- how to get current path in qt
- 【Leetcode】Copy List with Random Pointer
- ASP.Net中无刷新执行Session身份验证
- LeetCode_40 Combination Sum II
- MSB&LSB与Endianness
- LeetCode_39 Combination Sum
- jquery插件理解看这
- LeetCode_37 Sudoku Solver
- how to add external library in qt under ubuntu
- Group Anagrams -- LeetCode
- 字符串转化为十六进制
- 音乐播放器中的跳动的平衡器