package org.wangyi; import java.util.*; public class Test01 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] jump=new int[n]; for(int i=0;i<n;i++){ jump[i]=sc.nextInt(); } int res=bfs(jump); System.out.println(res); } public static int bfs(int[] jump){ Queue<Integer> queue=new LinkedList<>(); queue.offer(0); int cnt=0; while (!queue.isEmpty()) { int size=queue.size(); for(int k=0;k<size;k++){ Integer node = queue.poll(); if(node>=jump.length){ return cnt; } for(int i=0;i<node;i++){ queue.offer(i); } queue.offer(node+jump[node]); } cnt++; } return cnt; } }
点赞 4

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务