loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
s, p, c, n = [int(input()) for _ in range(4)]
red, blue = 'r', 'b'
gap = '_'

m = p
board = [red] * p + [gap] * (s - 2 * p) + [blue] * p

def show(b):
    print("".join(b))

def move(i, board):
    if board[i] == red:
        # Move right
        if i+1 < s and board[i+1] == gap:
            board[i], board[i+1] = board[i+1], board[i]
        elif i+2 < s and board[i+2] == gap:
            board[i], board[i+2] = board[i+2], board[i]
    elif board[i] == blue:
        # Move left
        if i-1 >= 0 and board[i-1] == gap:
            board[i], board[i-1] = board[i-1], board[i]
        elif i-2 >= 0 and board[i-2] == gap:
            board[i], board[i-2] = board[i-2], board[i]

for x in range(n):
    # Left moves
    count = 0
    for i in range(s):
        if board[i] != gap:
            count += 1
        if count == m:
            move(i, board)
            break
    
    # Right moves
    count = 0
    for i in range(s-1,-1,-1):
        if board[i] != gap:
            count += 1
        if count == m:
            move(i, board)
            break

    m += c
    if m > p:
        m -= p
    
    if x == 0:
        show(board)
show(board)