[leetcode] LCP 09. Minimum number of jumps (search, bfs)

Title description

Insert picture description hereTitle link: https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu/

Problem-solving ideas:

  • Directly use bfs to simulate the jumping process and optimize it appropriately.
  • In the process of bfs, every time a position is reached, all positions that can be reached in the next step are added to the queue until it jumps to a position greater than or equal to N.
  • Optimization: (1) For the coordinate i, if i has been skipped, there is no need to jump again-because the number of steps must be more to jump again. So there is no need to add i to the queue repeatedly. (2) Every time a new position i is reached, it is necessary to judge whether [0,i-1] has been skipped. This enumeration will cause a lot of time complexity. When we jump to position j at a certain time, we will "jump back" all the positions of [0,j-1], so when we are at position i next time, we only need to check whether [j,i-1] is skipped. In other words, you only need to record the "maximum j" (denoted as low) every time. Every time you consider the "next coordinate", you only need to judge from low.
  • This question can be analogous to the question of walking a maze on a square.

AC code

//Java代码
//Node表示某一"位置","位置"的属性有:跳跃的次数step、当前坐标position、以及下一步能往右跳所能到达的坐标。
//此处所说的坐标是数组下标
class Node{
    
    
  int step;    //跳跃次数
  int position;  //所在坐标
  int next;     //从当前坐标往后跳可以到达的坐标
  //next可以用来判断下一个位置是否超过N,以简化代码
  public Node(int s,int p,int n){
    
    
  this.step=s;
  this.position=p;
  this.next=n;    
  }
}
class Solution {
    
    
    public int minJump(int[] jump) {
    
    
    int N=jump.length; 
    int vis[]=new int[N];  //vis[i]等于1表示坐标i已经被跳到过
    Queue<Node>queue=new LinkedList<>(); //存储“位置”的队列
    int  s=0,p=0,n=jump[0],low=1;
    queue.offer(new Node(s,p,n));   //起点加入队列
    vis[0]=1;
    while(!queue.isEmpty()){
    
    
    Node now=queue.remove();
     s=now.step;
     p=now.position;
     n=now.next;
    if(n>=N)  return s+1;   //到达大于N的位置时直接返回步数
    //将所有能到达且还没有“跳到过”的位置加入队列
    else{
    
    
    for(int j=low;j<p;j++)    
    if(vis[j]==0){
    
    
    queue.offer(new Node(s+1,j,j+jump[j]));
    vis[j]=1;  
    } 
    if(vis[n]==0){
    
    
    queue.offer(new Node(s+1,n,n+jump[n]));
    vis[n]=1;    
    }   
    }
    low=Math.max(low,p);    //更新
    }
    return s;
    }
}

JAVA queue summary:

Using java queue for the first time, summarize a few common methods:

add()
inserts the specified element into this queue (if it is immediately feasible and does not violate the capacity limit), returns true on success, and throws an IllegalStateException if there is currently no available space.

element()
gets, but does not remove the head of this queue.

offer()
inserts the specified element into this queue (if it is immediately feasible and does not violate the capacity limit). When using a capacity-limited queue, this method is usually better than add(), which may not be able to insert elements, but just Throw an exception.

peek()
gets but does not remove the head of this queue; if this queue is empty, it returns null.

poll()
gets and removes the head of this queue, and returns null if this queue is empty.

remove()
gets and removes the head of this queue.

Queue declaration:
Queue<element type>queue=new LinkedList<>();

Previous blog: [Luogu] P1182 Number Sequence Section II (Two Points Answer)

Guess you like

Origin blog.csdn.net/IAMLSL/article/details/114489798
Recommended