codeforces - 500A 图的遍历 Java
2017-08-16 20:25
302 查看
A. New Year Transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
If I can go to cell t using the transportation system, print "YES".
Otherwise, print "NO".
Examples
input
8 4 1 2 1 2 1 2 1
output
YES
input
8 5 1 2 1 2 1 1 1
output
NO
import java.util.Scanner; public class Main { @SuppressWarnings("resource") public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int m = sc.nextInt(); int arr[] = new int[n ]; for (int i = 1; i < arr.length; i++) { arr[i] = sc.nextInt(); }arr[0]=1; int mix = 0; for (int i = 0; i < arr.length; i=mix) { if(mix>arr.length-1){break;} if(arr[mix]==0){break;} mix=arr[i]+mix; if(mix==m){mix=Integer.MAX_VALUE;break;} } if (mix != Integer.MAX_VALUE) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
相关文章推荐
- java 深度遍历文件夹中的所有文件
- To Java程序员:切勿用普通for循环遍历LinkedList
- java Map之 遍历Map的四种方法及Map.Entry详解
- java通过反射遍历class内所有参数和方法
- 数据结构与算法Java版——图及其遍历
- Java中遍历Map的常用方法
- 图的存储(Java)以及遍历
- 算法:二叉树的先(根)序遍历、中(根)序遍历、后(根)序遍历(递归及压栈出栈实现的非递归方式)的java代码实现
- java遍历map所有元素
- Java中从前往后和从后往前遍历ArrayList
- Java集合的Stack、Queue、Map的遍历
- java map遍历的四种方法总结
- java遍历map最强大的一种方式
- Java HashMap 遍历方式性能探讨
- java list遍历方法对比
- java list遍历方法对比
- Java Map遍历的同时进行元素删除
- java list遍历方法对比
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写
- Java中遍历Map对象的4种方法