[Daily 1 question Day218] LC1091 The shortest path in binary matrix | BFS

The shortest path in a binary matrix [LC1091]

You are driving a taxi on na locations. nThis location is numbered from near to 1far n, and you want to 1drive to nto make money by picking up passenger orders. You can only move forward in the direction of increasing numbers, you cannot change the direction.

Passenger information is represented by a two-dimensional array whose subscript starts from 0 , where indicates that the passenger needs to go from location and is willing to pay a tip of yuan .ridesrides[i] = [starti, endi, tipi]istartienditipi

For every passenger you choose to take orders i, you can make a profit of endi - starti + tipi RMB. You can only accept at most one order at a time.

For you nand rides, please return the maximum .

**NOTE:** You can drop off a passenger at one spot and pick up another at the same spot.

  • train of thought

    Conventional BFS, using queues for BFS, records the number of rounds of search when searching. When a reachable node is found, enqueue it. If the end point is found, the current round number is returned directly; if the queue is empty and the end point is still visited, then -1 is returned;

    • After visiting a node, in order to avoid repeated visits, directly set the given node to -1
  • accomplish

    class Solution {
          
          
        public int shortestPathBinaryMatrix(int[][] grid) {
          
          
            // BFS
           int[][] dirs = {
          
          {
          
          0, 1}, {
          
          1, 0}, {
          
          1, 1}, {
          
          0, -1}, {
          
          -1, 0}, {
          
          -1, -1}, {
          
          1, -1}, {
          
          -1, 1}};// 8个方向?
           Deque<int[]> queue = new LinkedList<>();
           int n = grid.length;
           int count = 0;
           if (grid[0][0] == 0){
          
          
               queue.add(new int[]{
          
          0, 0});
           }
           while(!queue.isEmpty()){
          
          
               int size = queue.size();
               count++;
               for (int i = 0; i < size; i++){
          
          
                    int[] p = queue.poll();
                    int x = p[0], y = p[1];
                    if (x == n - 1 && y == n - 1) return count;
                    for (int[] dir : dirs){
          
          
                        int x1 = x + dir[0], y1 = y + dir[1];           
                        if (x1 >= 0 && y1 >= 0 && x1 <n && y1 < n && grid[x1][y1] != 1){
          
                           
                            queue.add(new int[]{
          
          x1, y1});
                            grid[x1][y1] = 1;
                        }
                    }
               }
               
           }
           return -1;
    
        }
    }
    
    • the complexity
      • Time complexity: O ( n 2 ) \mathcal{O}(n^2)O ( n2)
      • Space complexity: O ( n 2 ) \mathcal{O}(n^2)O ( n2)

Guess you like

Origin blog.csdn.net/Tikitian/article/details/130884199
Recommended