Help me solving this algorithm task:
You are developing a platformer game. The game field can be represented as an infinite strip of cells, each numbered with an integer as shown in the image below. Initially, the player is positioned at 0 and facing right.
Your program receives a sequence of commands — player movements. They can be of three types:
- F (Forward) is Move forward in the current direction
- R(Right) is Change the direction to left
- L(Left) is Change the direction to right
Important:
- It’s allowed to perform the same turn multiple times, for example, “LL”. After such a sequence of commands, the player will be facing left.
2)The original sequence cannot be a real sequence of commands.
You have found a vulnerability in the game and obtained a sequence of actions from your friend. You know that the intercepted sequence differs from the real one by exactly one symbol. Find all the positions where your friend could actually end up after performing all the actions.
Input format:
The first line of the input contains a single integer N (1 <= N <= 3 · 10^5) — the number of commands.
The second line contains N characters — the commands themselves. It is guaranteed that all characters belong to the set {“F”, “R”, “L”}.
Output format:
Output a single number — the number of different positions where the player could have ended up after performing all the actions.
It looks like this:
Game field
Some examples:
- Input:
3
RLF
Output:
4
- Input:
6
LRFFLR
Output:
6
I tried memorization and DFS, but I was getting an error due to too deep recursion or I was getting wrong answers. Can you help me with this? I want to solve this task faster that O(N2) and I want to spend less than 256 MB of memory.
I use Python. This is my last attempt:
from collections import deque
def solve(N, commands):
def get_new_state(pos, direction, command):
if command == 'F':
return (pos + (1 if direction == 1 else -1), direction)
elif command == 'R':
return (pos, 1 if direction == 1 else -1)
elif command == 'L':
return (pos, -1 if direction == 1 else -1)
positions = set()
initial_pos = 0
initial_direction = 1 # 1 for right, -1 for left
queue = deque([(initial_pos, initial_direction, 0, False)])
visited = set()
while queue:
pos, direction, idx, altered = queue.popleft()
if idx == N:
positions.add(pos)
continue
if (pos, direction, idx, altered) in visited:
continue
visited.add((pos, direction, idx, altered))
# Continue with the current command
new_pos, new_direction = get_new_state(pos, direction, commands[idx])
queue.append((new_pos, new_direction, idx + 1, altered))
if not altered:
# Try changing the current command
if commands[idx] == 'F':
for new_command in ['R', 'L']:
new_pos, new_direction = get_new_state(pos, direction, new_command)
queue.append((new_pos, new_direction, idx + 1, True))
elif commands[idx] == 'R':
for new_command in ['F', 'L']:
new_pos, new_direction = get_new_state(pos, direction, new_command)
queue.append((new_pos, new_direction, idx + 1, True))
elif commands[idx] == 'L':
for new_command in ['F', 'R']:
new_pos, new_direction = get_new_state(pos, direction, new_command)
queue.append((new_pos, new_direction, idx + 1, True))
return len(positions)
if __name__ == "__main__":
with open('input.txt', 'r') as file:
N = int(file.readline().strip())
commands = file.readline().strip()
result = solve(N, commands)
print(result)
Allody22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.