01 Matrix--广度优先遍历bfs--leetcode
2017-03-29 16:54
387 查看
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
题意:
给一个矩阵m*n,由0,1组成,求矩阵中的每个1到0的距离,求出的结果放在矩阵中相应的1所在的位置。
Input:
0 0 0
0 1 0
0 0 0
Output:
0 0 0
0 1 0
0 0 0
Example 2:
Input:
0 0 0
0 1 0
1 1 1
Output:
0 0 0
0 1 0
1 2 1
用到算法:广度遍历搜索算法(图的广度优先遍历)。
从所要搜索的点1开始向周围方向未访问过的点开始查找,上下左右四个点里如果不存在所需要的点0,再分别从这四个点处依次按上一步的步骤查找,看是否存在点0,否则继续查找,是则返回。使用currentCount记录当前最短路径,如果递归到超过此深度,则放弃这条路径,将这条路径长度置为最大值。
The distance between two adjacent cells is 1.
题意:
给一个矩阵m*n,由0,1组成,求矩阵中的每个1到0的距离,求出的结果放在矩阵中相应的1所在的位置。
Input:
0 0 0
0 1 0
0 0 0
Output:
0 0 0
0 1 0
0 0 0
Example 2:
Input:
0 0 0
0 1 0
1 1 1
Output:
0 0 0
0 1 0
1 2 1
用到算法:广度遍历搜索算法(图的广度优先遍历)。
从所要搜索的点1开始向周围方向未访问过的点开始查找,上下左右四个点里如果不存在所需要的点0,再分别从这四个点处依次按上一步的步骤查找,看是否存在点0,否则继续查找,是则返回。使用currentCount记录当前最短路径,如果递归到超过此深度,则放弃这条路径,将这条路径长度置为最大值。
public class Solution { int len_x, len_y, currentCount = 0, current_i = 0, current_j = 0; int[][] matrixArr; int[][] matrixArrCount; public List<List<Integer>> updateMatrix(List<List<Integer>> matrix) { List<List<Integer>> res = new ArrayList<List<Integer>>(); len_x = matrix.size(); len_y = matrix.get(0).size(); int i, j; matrixArr = new int[len_x][len_y]; matrixArrCount = new int[len_x][len_y]; boolean[][] visited = new boolean[len_x][len_y]; for (i = 0; i < len_x; i++) { for (j = 0; j < len_y; j++) { matrixArr[i][j] = matrix.get(i).get(j); } } for (i = 0; i < len_x; i++) { for (j = 0; j < len_y; j++) { if (matrixArr[i][j] == 1) { if (matrixArrCount[i][j] == 1) { continue; } current_i = i; current_j = j; matrixArrCount[i][j] = bfs(i, j, 1, visited); } } } for (i = 0; i < len_x; i++) { List<Integer> list = new ArrayList<Integer>(); for (j = 0; j < len_y; j++) { list.add(matrixArrCount[i][j]); } res.add(list); } return res; } public int bfs(int vi, int vj, int k, boolean[][] visited) { int count = 0, min = 0; if (k == len_x + len_y - 1 || (currentCount > 0 && k > currentCount)) { return 1; } if (vi > 0 && matrixArr[vi - 1][vj] == 0) { matrixArrCount[vi][vj] = 1; if (current_i == vi && current_j == vj) { currentCount = count; } return 1; } if (vi + 1 < len_x && matrixArr[vi + 1][vj] == 0) { matrixArrCount[vi][vj] = 1; if (current_i == vi && current_j == vj) { currentCount = count; } return 1; } if (vj > 0 && matrixArr[vi][vj - 1] == 0) { matrixArrCount[vi][vj] = 1; if (current_i == vi && current_j == vj) { currentCount = count; } return 1; } if (vj + 1 < len_y && matrixArr[vi][vj + 1] == 0) { matrixArrCount[vi][vj] = 1; if (current_i == vi && current_j == vj) { currentCount = count; } return 1; } if (currentCount > 0 && k > currentCount) { return 1; } // adjacent cells is not 0,visit the next adjacent cells if (vi > 0 && matrixArr[vi - 1][vj] == 1 && !visited[vi - 1][vj]) { visited[vi][vj] = true; min = 1 + bfs(vi - 1, vj, k + 1, visited); visited[vi][vj] = false; if (count == 0) { count = min; } else if (count > min) { count = min; } if (current_i == vi && current_j == vj) { currentCount = count; } } if (vi + 1 < len_x && matrixArr[vi + 1][vj] == 1 && !visited[vi + 1][vj]) { visited[vi][vj] = true; min = 1 + bfs(vi + 1, vj, k + 1, visited); visited[vi][vj] = false; if (count == 0) { count = min; } else if (count > min) { count = min; } if (current_i == vi && current_j == vj) { currentCount = count; } } if (vj > 0 && matrixArr[vi][vj - 1] == 1 && !visited[vi][vj - 1]) { visited[vi][vj] = true; min = 1 + bfs(vi, vj - 1, k + 1, visited); visited[vi][vj] = false; if (count == 0) { count = min; } else if (count > min) { count = min; } if (current_i == vi && current_j == vj) { currentCount = count; } } if (vj + 1 < len_y && matrixArr[vi][vj + 1] == 1 && !visited[vi][vj + 1]) { visited[vi][vj] = true; min = 1 + bfs(vi, vj + 1, k + 1, visited); visited[vi][vj] = false; if (count == 0) { count = min; } else if (count > min) { count = min; } if (current_i == vi && current_j == vj) { currentCount = count; } } if (count == 0) { count = len_x + len_y - 2; } return count; } }
相关文章推荐
- 广度优先遍历 之 LeetCode 542. 01 Matrix
- Leetcode 之BFS广度优先遍历
- leetcode 388. Longest Absolute File Path 最长长度文件路径 + 广度优先遍历BFS
- leetcode 637. Average of Levels in Binary Tree 二叉树每层平均值+广度优先遍历BFS
- leetcode 515. Find Largest Value in Each Tree Row 二叉树每一层最大值+广度优先遍历BFS
- leetcode 116. Populating Next Right Pointers in Each Node BFS广度优先遍历
- leetcode 752. Open the Lock 开锁 + 十分典型的广度优先遍历BFS
- 邻接矩阵深度优先和广度优先遍历(DFS和BFS)
- 广度优先遍历BFS的图算法
- Java实现利用广度优先遍历(BFS)计算最短路径的方法
- BFS广度优先遍历
- DFS(深度优先遍历)和 BFS(广度优先遍历)
- 算法学习 - 图的广度优先遍历(BFS) (C++)
- 利用广度优先遍历(BFS)计算最短路径 - Java实现
- java学习笔记之图的遍历(广度优先搜索BFS)
- 图的遍历之BFS广度优先遍历C++实现
- Leetcode - 广度优先遍历
- 【广度优先遍历】leetcode - Word Ladder II
- 邻接矩阵存储的无向图深度优先(DFS)广度优先(BFS)遍历
- Java图以及BFS广度优先遍历