【Lintcode】1563. Shortest path to the destination

题目地址:

https://www.lintcode.com/problem/shortest-path-to-the-destination/description

给定一个二维矩阵,只含 0 0 0 1 1 1 2 2 2 1 1 1代表障碍物, 2 2 2代表终点,要求从 ( 0 , 0 ) (0,0) (0,0)出发,问最少走多少步能到达终点。不允许走到障碍物上。若走不到则返回 − 1 -1 1

思路是双向BFS。代码如下:

import java.util.*;

public class Solution {
    class Pair {
        int x, y;
        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        @Override
        public boolean equals(Object o) {
            Pair pair = (Pair) o;
            return x == pair.x && y == pair.y;
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }
    /**
     * @param targetMap:
     * @return: nothing
     */
    public int shortestPath(int[][] targetMap) {
        // Write your code here
        Queue<Pair> beginQueue = new ArrayDeque<>(), endQueue = new ArrayDeque<>();
        Set<Pair> beginSet = new HashSet<>(), endSet = new HashSet<>();
        Pair s = new Pair(0, 0);
        beginQueue.offer(s);
        beginSet.add(s);
        Pair e = new Pair(0, 0);
        for (int i = 0; i < targetMap.length; i++) {
            for (int j = 0; j < targetMap[0].length; j++) {
                if (targetMap[i][j] == 2) {
                    e.x = i;
                    e.y = j;
                    endQueue.offer(e);
                    endSet.add(e);
                    break;
                }
            }
            if (!endQueue.isEmpty()) {
                break;
            }
        }
        
        int res = 0;
        while (!beginQueue.isEmpty() && !endQueue.isEmpty()) {
            int beginSize = beginQueue.size(), endSize = endQueue.size();
            
            res++;
            for (int i = 0; i < beginSize; i++) {
                Pair cur = beginQueue.poll();
                for (Pair next : getNexts(cur, targetMap)) {
                    if (endSet.contains(next)) {
                        return res;
                    }
                    if (beginSet.add(next)) {
                        beginQueue.offer(next);
                    }
                }
            }
            
            res++;
            for (int i = 0; i < endSize; i++) {
                Pair cur = endQueue.poll();
                for (Pair next : getNexts(cur, targetMap)) {
                    if (beginSet.contains(next)) {
                        return res;
                    }
                    if (endSet.add(next)) {
                        endQueue.offer(next);
                    }
                }
            }
        }
        
        return -1;
    }
    
    private List<Pair> getNexts(Pair cur, int[][] matrix) {
        List<Pair> nexts = new ArrayList<>();
        int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        for (int i = 0; i < dirs.length; i++) {
            int nextX = cur.x + dirs[i][0], nextY = cur.y + dirs[i][1];
            if (0 <= nextX && nextX < matrix.length && 0 <= nextY && nextY < matrix[0].length && matrix[nextX][nextY] != 1) {
                nexts.add(new Pair(nextX, nextY));
            }
        }
        
        return nexts;
    }
}

时空复杂度 O ( m n ) O(mn) O(mn)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值