码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 500A. New Year Transportation

时间:2015-01-01 00:02:02      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://codeforces.com/problemset/problem/500/A

题目意思:给出 n-1 个 cell,每个 cell 有一个值 ai,表示在这个编号为 i 的 cell,能到达i + ai 的cell,但不能反过来,即从 i+ai 到达 i 这个 cell。问从第一个cell 开始,是否可以到达 t 这个cell。

  第一次过不了pretest 是因为没有考虑到,如果 t = 1的情况,后来被人 hack 之后就不知道原因了。。。原来是因为第 n 个 cell,默认是 0,应该赋予一个很大的数值!!注意题目只给出 1 ~ n-1 cell 的 ai , 是没有给出 第 n 个的!!!所以要设值,否则代码中的while 会变成死循环的。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 3e4 + 10;
 9 
10 int p[maxn];
11 
12 int main()
13 {
14     #ifndef ONLINE_JUDGE
15         freopen("in.txt", "r", stdin);
16     #endif // ONLINE_JUDGE
17     int n, t;
18     while (scanf("%d%d", &n, &t) != EOF)
19     {
20         for (int i = 1; i <= n-1; i++)
21             scanf("%d", &p[i]);
22         p[n] = maxn;         // 这个很关键!
23         bool flag = false;
24         int i = 1;
25         int step = 1;        // 初始化为1方便进入while循环的if判断(有可能t==1)
26         while (step <= n)
27         {
28             if (step == t)
29             {
30                 flag = true;
31                 break;
32             }
33             step += p[i];
34             i = step;
35         }
36         printf("%s\n", flag ? "YES" : "NO");
37     }
38     return 0;
39 }

 

codeforces 500A. New Year Transportation

标签:

原文地址:http://www.cnblogs.com/windysai/p/4196857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!