Walls and Gates

来源:互联网 发布:迅雷7mac官方下载 编辑:程序博客网 时间:2024/05/25 20:01

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  INFINF INF INF  -1INF  -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

思路:这题算是比较好的面试题目,因为可以考到很多东西,而且好像谁都会写,但是每个人写出来的代码确实有差别,有很强的区分度。

首先我自己的思路就是每次以INF为起点,开始进行DFS搜索,因为 value是自己身边的四个值的最小值而来,所以我是从INF搜到门0,所以dfs需要有返回值,而且到门了,就返回0,如果已经访问过了,那就返回访问过的值,而且如果rooms里面有值>0 && < Integer.MAX_VALUE,你也要返回。visited是用来剪枝用的,否则会超时。恩,如果面试,我可能会写出这个版本,因为这个版本比较符合直觉。但是网上搜了一下,有更加简洁的版本和思考方法。如思路2.

public class Solution {     public void wallsAndGates(int[][] rooms) {         if(rooms == null || rooms.length == 0) return;         int m = rooms.length;         int n = rooms[0].length;         boolean[][] visited = new boolean[m][n];         for(int i=0; i<m; i++){             for(int j=0; j<n; j++){                 if(rooms[i][j] == Integer.MAX_VALUE){                     rooms[i][j] = collect(rooms, i, j, visited);                 }             }         }     }          public int collect(int[][] rooms, int i, int j, boolean[][] visited){         if(i<0 || i>=rooms.length || j<0 || j>=rooms[0].length || rooms[i][j] == -1){             return Integer.MAX_VALUE;         }                  if(rooms[i][j] == 0) return 0;         if(rooms[i][j] > 0 && rooms[i][j] != Integer.MAX_VALUE || visited[i][j]){             return rooms[i][j];         }                  visited[i][j] = true;         int lv = collect(rooms, i, j-1, visited);         int rv = collect(rooms, i, j+1, visited);         int tv = collect(rooms, i-1, j, visited);         int bv = collect(rooms, i+1, j, visited);         visited[i][j] = false;         int min = Math.min(lv, Math.min(rv, Math.min(tv, bv)));         return min == Integer.MAX_VALUE ? Integer.MAX_VALUE : min+1;     } }
思路2:从门开始,一步步扩展, 步数+1。所有step> rooms[i][j]  的跳过,只把rooms[i][j] > step的,更新。因为-1是墙,所以所有的-1都小于0(因为起始都是0),所以所有的 -1都被跳过了,然后一步步更新INF。

public class Solution {     public void wallsAndGates(int[][] rooms) {         if(rooms == null || rooms.length == 0) return;         int m = rooms.length;         int n = rooms[0].length;         for(int i=0; i<m; i++){             for(int j=0; j<n; j++){                 if(rooms[i][j] == 0){                     dfs(rooms, i, j, 0);                 }             }         }     }          public void dfs(int[][] rooms, int i, int j, int step){         if(i<0 || i>=rooms.length || j<0 || j>=rooms[0].length || step > rooms[i][j]){             return;         }         rooms[i][j] = step;         dfs(rooms, i+1, j, step+1);         dfs(rooms, i-1, j, step+1);         dfs(rooms, i, j-1, step+1);         dfs(rooms, i, j+1, step+1);     } }


0 0
原创粉丝点击