Santa Claus and Robot (Codeforces 752C)
Tags: Greedy, shortest paths

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.

Note: The illustrations to the first three tests are given below.  The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.

Sample Inputs:
4
RURD

6
RRULDD

26
RRRULURURUULULLLDLDDRDRDLD

3
RLL

4
LRLR

Sample Outputs:
2

2

7

2

4

Solution (Python):
def codeforces(path):
    dirs_x = 'LR'
    dirs_y = 'UD'
    points = 0
    current_x = None
    current_y = None
    for char in path:
        if char in dirs_x:
            if current_x is None:
                current_x = char
            elif current_x != char:
                points += 1
                current_x = char
                current_y = None
        elif char in dirs_y:
            if current_y is None:
                current_y = char
            elif current_y != char:
                points += 1
                current_y = char
                current_x = None
    return points + 1
_ = input()
path = input()
print(codeforces(path))

Solution (Python):
anti = {'R': 'L', 'L': 'R', 'U': 'D', 'D': 'U'}

def solve(seq):
    point_count = 1
    cur_2 = set()
    for c in seq:
        if anti[c] in cur_2:
            cur_2 = set()
            point_count += 1
        cur_2.add(c)
    return point_count
if __name__ == '__main__':
    input()
    print(solve(input()))

Solution (Python):
n = int(input())
p = [c for c in input()]
ud = None
lr = None
uddir = ['U', 'D']
lrdir = ['L', 'R']
i = 1
for c in p:
    if c in uddir:
        if ud and ud != c:
            i += 1
            ud = c
            lr = None
        elif not ud:
            ud = c
    elif lr and lr != c:
        i += 1
        ud = None
        lr = c
    elif not lr:
        lr = c
print(i)