LeetCode 286. Walls and Gates
来源:互联网 发布:天族气功师捏脸数据 编辑:程序博客网 时间:2024/05/22 09:05
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
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
思路:
1. 对二维的规则结构,用dfs遍历。
void wallsAndGates(vector<vector<int>>& rooms) { //dfs int m=rooms.size(); if(m==0) return; vector<vector<int>> dir={{-1,0},{1,0},{0,-1},{0,1}};//注意区别{}和()的使用,{16,2,77,29}或second (4,100) for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(rooms[i][j]==0) dfs(rooms,i,j,m,n,dir,0); } }}void dfs(vector<vector<int>>& rooms, int i,int j, int m,int n,vector<vector<int>>&dir,int depth){ if(i>=m||i<0||j>=n||j<0||rooms[i][j]<depth) return; rooms[i][j]=depth; for(auto&d:dir){ dfs(rooms,i+d[0],j+d[1],m,n,dir,depth+1); }}
0 0
- Leetcode 286.Walls and Gates
- LeetCode 286. Walls and Gates
- Leetcode 286. Walls and Gates
- LeetCode 286. Walls and Gates
- *LeetCode-Walls and Gates
- Leetcode: Walls and Gates
- [leetcode] 286. Walls and Gates 解题报告
- [Leetcode] 286. Walls and Gates 解题报告
- 286. Walls and Gates
- 286. Walls and Gates
- leetcode 286: Walls and Gates
- [Leetcode 286]: Walls and Gates
- LeetCode 286. Walls and Gates(墙与门)
- LeetCode 题解(259) : Walls and Gates
- [LeetCode286]Walls and Gates
- Walls and Gates
- Walls and Gates
- Walls and Gates
- linux 任务后台bg和fg命令
- 并查集简单实现
- BZOJ 3670 [Noi2014] 动物园 KMP
- windows mysql 自动备份的几种方法
- java extends
- LeetCode 286. Walls and Gates
- 几种方式按home,几种方式打开应用,速度比较;
- 电源设计中电容的工作原理及选用
- java 历史版本下载
- Boost.Asio学习之总结
- java反射机制初探
- 笔记︱基于网络节点的node2vec、论文、算法python实现
- 根据内核Oops 定位代码
- 【java学习】JVM学习