• 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);
    }
  • 相关阅读:
    μC/OS-III---I笔记5---多值信号量
    μC/OS-III---I笔记4---软件定时器
    μC/OS-III---I笔记3---时间管理
    μC/OS-III---I笔记2---实钟节拍
    μC/OS-III---I笔记1---概述
    Cortex-M系列内核 启动文件分析
    C语言中函数的调用方式
    const,volatile,static,typdef,几个关键字辨析和理解
    java注解细节
    java枚举细节
  • 原文地址:https://www.cnblogs.com/dilthey/p/8628984.html
Copyright © 2020-2023  润新知