Finding All Possible Player Positions After Correcting a Single Command in a Sequence

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:

  1. F (Forward) is Move forward in the current direction
  2. R(Right) is Change the direction to left
  3. L(Left) is Change the direction to right

Important:

  1. 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:

  1. Input:
   3 
   RLF

Output:

4
  1. 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)

New contributor

Allody22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật