zoukankan      html  css  js  c++  java
  • Codeforces 752C

    题目链接:http://codeforces.com/problemset/problem/752/C

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.

    While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.

    Input

    The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.

    Output

    The only line of input should contain the minimum possible length of the sequence.

    Examples
    input
    Copy
    4
    RURD
    output
    2
    input
    Copy
    6
    RRULDD
    output
    2
    input
    Copy
    26
    RRRULURURUULULLLDLDDRDRDLD
    output
    7
    input
    Copy
    3
    RLL
    output
    2
    input
    Copy
    4
    LRLR
    output
    4
     
    题意:

    若圣诞老人要按顺序送m个礼物(每个礼物都有一个坐标),则他会在任意两个礼物间走最短的路线到达;

    现在只知道圣诞老人送礼物的所走的路径,要求圣诞老人最少送了多少个礼物。

    题解:

    首先,从某一点出发(这一点可以是某个礼物,也可以是起点),

    一旦圣诞老人往L走了一步,最短路径就不允许他走R(同样的道理,一旦走了L,就不能再走D);

    所以我们只要定义一个flag[1:4]的数组,从某一起点记录下他是否有走L、R、U、D,一旦出现矛盾(L-R,U-D)……

      则必然到达了一个礼物点,ans+=1;

      then,清零flag数组,表示这个礼物点是新起点,再重新记录……

    反复循环,直到走完;

    最后终点的时候再加上一个礼物即可。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    
    const int MAX=(int)(2e5+10);
    
    int n,ans;
    char Move[MAX];
    bool flag[5];
    
    int main()
    {
        scanf("%d",&n);
        scanf("%s",Move);
        ans=0;
        for(int i=0;i<n;i++)
        {
            if(Move[i]=='L')
            {
                if(flag[2])
                {
                    ans++;
                    flag[1]=flag[2]=flag[3]=flag[4]=0;
                }
                flag[1]=1;
            }
            else if(Move[i]=='R')
            {
                if(flag[1])
                {
                    ans++;
                    flag[1]=flag[2]=flag[3]=flag[4]=0;
                }
                flag[2]=1;
            }
            else if(Move[i]=='U')
            {
                if(flag[4])
                {
                    ans++;
                    flag[1]=flag[2]=flag[3]=flag[4]=0;
                }
                flag[3]=1;
            }
            else //if(Move[i]=='D')
            {
                if(flag[3])
                {
                    ans++;
                    flag[1]=flag[2]=flag[3]=flag[4]=0;
                }
                flag[4]=1;
            }
        }
        printf("%d
    ",++ans);
    }
  • 相关阅读:
    Web后台项目学习2(代码部分)
    Web后台项目学习
    JDBC
    爬虫视频
    asyncio
    一篇做实验的随笔
    day14
    day13
    html+css 习题
    js-练习题
  • 原文地址:https://www.cnblogs.com/dilthey/p/8628984.html
Copyright © 2011-2022 走看看