Problem p03488 (python)

Fixed submission:
S = input()
x, y = map(int, input().split(' '))

F = [len(s) for s in S.split('T')]

memo_x = [set((F[0], ))]
memo_y = [set((0, ))]

for i, f in enumerate(F[2::2]):
    next_memo_x = set()
    for prev_x in memo_x[i]:
        next_memo_x.add(prev_x + f)
        next_memo_x.add(prev_x - f)
    memo_x.append(next_memo_x)

for i, f in enumerate(F[1::2]):
    next_memo_y = set()
    for prev_y in memo_y[i]:
        next_memo_y.add(prev_y + f)
        next_memo_y.add(prev_y - f)
    memo_y.append(next_memo_y)

if x in memo_x[-1] and y in memo_y[-1]:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
S = input()
x, y = map(int, input().split(' '))

move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
direction = 0

position = set((0, 0))
state = [((0, 0), 0)]

can_move = False
idx = 0

for i, s in enumerate(S):
    rest_inst_num = len(S) - (i)
    next_position = set()
    next_state = []
    for p, d in state:
        xx, yy = p
        dist2goal = abs(xx - x) + abs(yy - y)
        if dist2goal > rest_inst_num:
            continue
        if s == 'T':
            next_position.add(p)
            next_state.append((p, d + 1))
            next_state.append((p, d - 1))
        else:
            mxx, myy = move[d]
            np = (xx + mxx, yy + myy)
            next_position.add(np)
            next_state.append((np, d))
    position = next_position
    state = next_state

if (x, y) in position:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
S = input()
x, y = map(int, input().split(' '))

move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
direction = 0

position = set((0, 0))
state = [((0, 0), 0)]

can_move = False
idx = 0

for i, s in enumerate(S):
    rest_inst_num = len(S) - (i + 1)
    next_position = set()
    next_state = []
    for p, d in state:
        xx, yy = p
        dist2goal = abs(xx - x) + abs(yy - y)
        if dist2goal > rest_inst_num:
            continue
        if s == 'T':
            next_position.add(p)
            next_state.append((p, d + 1))
            next_state.append((p, d - 1))
        else:
            mxx, myy = move[d]
            np = (xx + mxx, yy + myy)
            next_position.add(np)
            next_state.append((np, d))
    position = next_position
    state = next_state
    snum += len(state)

if (x, y) in position:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
S = input()
x, y = map(int, input().split(' '))

move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
direction = 0

position = set((0, 0))
state = [((0, 0), 0)]

can_move = False
idx = 0

for s in S:
    next_position = set()
    next_state = []
    for p, d in state:
        if s == 'T':
            next_position.add(p)
            next_state.append((p, d + 1))
            next_state.append((p, d - 1))
        else:
            xx, yy = p
            mxx, myy = move[d]
            np = (xx + mxx, yy + myy)
            next_position.add(np)
            next_state.append((np, d))
    position = next_position
    state = next_state

if (x, y) in position:
    print('Yes')
else:
    print('No')