You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

题目分析

  • 一共n个木桩,青蛙现在在第1个木桩上。
  • 每个木桩上面有一个数字,表示从此木桩出发,最远可以跳几个木桩。比如有数字2,表示一次最远可以跳2个木桩。
  • 1号木桩出发,跳到n号木桩上,问最少需要几步?
\large [2,1,5,1,3]
  • 不能只看眼前,不看将来,不是本次跳的越远越好,挑最远的跳,可能就略过了以后更有利的木桩。
  • 从当前木桩(x)出发,可以有几种选择呢?1<=?<=x种选择。

  • 可不可以向后跳呢
  • 可能需要一个状态记录的数组st,装st[3]=1
  • 最短路径采用广度优先遍历bfs

(1)输入n,N=110,a[N] (2) int st[N]; (3)


queue<PII> q;
//初始化,老大入队列
//老大已使用过

while(q.size()){
  auto t=q.front();
  q.pop();

  if(目标位置是终点){
      输出t.second
      break;
     }

  1到当前木桩上的数字进行遍历
  {
    for ( 1  t.first+1) {
            下一个位置 x = t.first + i;
            如果x走过了 continue;
            //填充到队列中,做为下一跳的起点
            q.push({x, 当前步数 + 1});
            标识x已使用过
        }
  }
}