博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
286. Walls and Gates
阅读量:5115 次
发布时间:2019-06-13

本文共 2760 字,大约阅读时间需要 9 分钟。

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   将 gate 做dfs 或者存入queue
public void wallsAndGates(int[][] rooms) {        for(int i = 0; i< rooms.length; i++){            for(int j = 0; j < rooms[0].length; j ++){                if(rooms[i][j] == 0)                    dfs(rooms, i, j, 0);            }        }    }    public void dfs(int[][] rooms, int i , int j, int deep){        rooms[i][j] = deep;        if(i < rooms.length - 1 && rooms[i+1][j] > deep)            dfs(rooms, i + 1, j, deep +1);                if(j < rooms[0].length - 1 && rooms[i][j + 1] > deep)           dfs(rooms, i, j + 1, deep +1);       if(j > 0 && rooms[i][j - 1] > deep)            dfs(rooms, i, j - 1, deep +1);               if(i > 0 && rooms[i-1][j] > deep)            dfs(rooms, i - 1, j, deep +1);    }

BFS

class Node{    int x;    int y;    public Node(int x, int y){        this.x = x;        this.y = y;    }}private  int inf = Integer.MAX_VALUE;    public  void wallsAndGates(int[][] nums){        Queue
queue = new LinkedList<>(); for(int i = 0 ; i < nums.length; i++){ for(int j = 0; j < nums[0].length; j ++){ if(nums[i][j] == 0){ Node x = new Node(i,j); queue.add(x); } } } bfs(queue, nums); } public void bfs(Queue
queue, int[][] nums){ while(!queue.isEmpty()){ Node root = queue.poll(); int x = root.x; int y = root.y; if(x < nums.length - 1 && nums[x+1][y] == inf){ nums[x+1][y] = nums[x][y] + 1; queue.add(new Node(x+1,y)); } if(x > 0 && nums[x-1][y] == inf){ nums[x-1][y] = nums[x][y] + 1; queue.add(new Node(x-1,y)); } if(y < nums[0].length - 1 && nums[x][y+1] == inf){ nums[x][y+1] = nums[x][y] + 1; queue.add(new Node(x,y+1)); } if(y > 0 && nums[x][y-1] == inf){ nums[x][y-1] = nums[x][y] + 1; queue.add(new Node(x,y-1)); } } }}

 

转载于:https://www.cnblogs.com/joannacode/p/5947919.html

你可能感兴趣的文章
Android 获取网络链接类型
查看>>
linux中启动与终止lnmp的脚本
查看>>
gdb中信号的处理[转]
查看>>
LeetCode【709. 转换成小写字母】
查看>>
如何在Access2007中使用日期类型查询数据
查看>>
Jzoj4757 树上摩托
查看>>
CF992E Nastya and King-Shamans(线段树二分+思维)
查看>>
第一个Java Web程序
查看>>
树状数组_一维
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
linux install ftp server
查看>>
嵌入式软件设计第8次实验报告
查看>>
算法和数据结构(三)
查看>>
Ubuntu下的eclipse安装subclipse遇到没有javahl的问题...(2天解决了)
查看>>
alter database databasename set single_user with rollback IMMEDIATE 不成功问题
查看>>
Repeater + Resources 列表 [原创][分享]
查看>>
WCF揭秘——使用AJAX+WCF服务进行页面开发
查看>>
【题解】青蛙的约会
查看>>
IO流
查看>>
mybatis调用存储过程,获取返回的游标
查看>>