训练赛(28)—— 计蒜客 45724 Jumping Frog

题目链接https://vjudge.net/contest/386543#problem/C

 

题目大意是有一只青蛙,每次可跳过d格(或更少),求从起点到终点至少需要跳几次。

如果 i + d + 1 没有超过终点并且不是障碍物,就直接跳到 i + d + 1上。否则从i + d + 1

开始,从后往前检查,找到遇到的第一个非障碍物,跳到那个位置上。如果找不到这样

的位置,说明无法跳到终点。

 

ac代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 using namespace std;
11 typedef long long ll;
12 
13 int main()
14 {
15     int t;
16     cin >> t;
17     for (int Case = 1; Case <= t; Case++)
18     {
19         int c, d;
20         string s;
21         cin >> c >> d >> s;
22         int cnt = 0;
23         int flag = 0;
24         for (int i = 0; i < c - 1;)
25         {
26 
27             if (i + d + 1 < c && s[i + d + 1] == '.')
28             {
29                 cnt++;
30                 i = i + d + 1;
31             }
32             else
33             {
34                 cnt++;
35                 for (int j = i + d + 1; j > i; j--)
36                 {
37                     if (j < c && s[j] == '.')
38                     {
39                         i = j;
40                         flag = 0;
41                         break;
42                     }
43                     flag = 1;
44                 }
45             }
46             if (flag) break;
47         }
48         cout << "Day #" << Case << endl;
49         cout << c << " " << d << endl;
50         cout << s << endl;
51         if (flag) cout << "0" << endl;
52         else cout << cnt << endl;
53         cout << endl;
54     }
55     return 0;
56 }

 

posted @ 2020-08-02 14:43  章楠雨  阅读(205)  评论(0编辑  收藏  举报