New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.
So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell(i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to celli using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.
Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.
The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.
The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.
If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".
水题,不过我突然觉得喜欢我写东西格式。cherry今天来了,好兴奋,坐等顺丰大哥。
#include<iostream>
#define MAXN 30003
using namespace std;
int cell[MAXN];
int main()
{
int n,t,q;
cin>>n>>t;
for(int i=1;i<n;i++)cin>>cell[i];
q=1;
while(q<t)
{
q=cell[q]+q;
if(q==t)cout<<"YES"<<endl;
}
if(q!=t)cout<<"NO"<<endl;
return 0;
}
文章探讨了如何在一个由n个细胞组成的Line World中,通过特定的运输系统,从一个细胞到达另一个指定细胞的过程。通过输入包含细胞总数和目标细胞位置,以及一系列连接两个细胞的运输系统参数,作者展示了如何判断是否能够通过这些系统到达目标细胞。此题旨在解决路径查找问题,提供了一种算法解决方案。
528

被折叠的 条评论
为什么被折叠?



