01 Matrix--广度优先遍历bfs--leetcode

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记录当前最短路径,如果递归到超过此深度,则放弃这条路径,将这条路径长度置为最大值。

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值