Description
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 - 1portals, 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 cell i 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.
Output
If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".
Hint
In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.
In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.
真的要气死人了~~~
题意:给你【n】个cell, 然后再给你一个要到达的cell【t】,再给你n-1一个数,能到达的系统规则定义为:i->i+a[i],而且不能反向,只是单方向。判断是否能到达cell【t】。
可以用并查集解答,判断是否属于同一个老大,但是简单模拟一遍就AC啦!
eample 1:
8 4
1 2 1 2 1 2 1
1->2
2->4
3->4
4->6
5->6
6->8
7->8
所以最终从cell【1】能到达1, 2, 4, 6.题上好像没有6不知道为啥?
eample 2:
8 5
1 2 1 2 1 1 1
1->2
2->4
3->4
4->6
5->6
6->7
7->8
所以最终从cell【1】能到达1, 2, 4, 6, 7, 8.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<algorithm>
using namespace std;
const int maxn=110000;
const int INF=0x3f3f3f3f;
int a[50000];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<n;i++)
scanf("%d",&a[i]);
int i;
for(i=1;i<=n;i+=a[i])
{
if(i>=m)break;
}
if(i==m)puts("YES");
else puts("NO");
}
return 0;
}