Problem p02692 (python)

Fixed submission:
from collections import deque
import sys
sys.setrecursionlimit(500000)
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i, '')

def f(x, dic):
    if x[4] == '':
        return 1
    else:
        if f(dic[x], dic):
            print(x[4])
            return 1

N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0
dic = {}
que = deque([(A, B, C, i, '')])
while len(que) != 0:
    x = que.pop()
    i = x[3]
    if x[3] == N:
        print('Yes')
        f(x, dic)
        exit()
    if S[i] == 'AB':
        if x[1] > 0:
            newx = (x[0]+1, x[1]-1, x[2], x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1]+1, x[2], x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'BC':
        if x[2] > 0:
            newx = (x[0], x[1]+1, x[2]-1, x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
        if x[1] > 0:
            newx = (x[0], x[1]-1, x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'AC':
        if x[2] > 0:
            newx = (x[0]+1, x[1], x[2]-1, x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1], x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    else:
        print('error')
print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i, '')

def f(x, dic):
    if x[4] == '':
        return 1
    else:
        if f(dic[x], dic):
            print(x[4])
            return 1

N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0
dic = {}
que = deque([(A, B, C, i, '')])
while len(que) != 0:
    x = que.pop()
    i = x[3]
    if x[3] == N:
        print('Yes')
        f(x, dic)
        exit()
    if S[i] == 'AB':
        if x[1] > 0:
            newx = (x[0]+1, x[1]-1, x[2], x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1]+1, x[2], x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'BC':
        if x[2] > 0:
            newx = (x[0], x[1]+1, x[2]-1, x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
        if x[1] > 0:
            newx = (x[0], x[1]-1, x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'AC':
        if x[2] > 0:
            newx = (x[0]+1, x[1], x[2]-1, x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1], x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    else:
        print('error')
print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i)
que = deque()
def func(x, S):
    i = x[3]
    if x[3] == N:
        print('Yes')
        return 1
    if S[i] == 'AB':
        if x[1] > 0:
            result = func((x[0]+1, x[1]-1, x[2], x[3]+1), S)
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1]+1, x[2], x[3]+1), S)
            if result:
                que.append('B')
                return 1
    elif S[i] == 'BC':
        if x[2] > 0:
            result = func((x[0], x[1]+1, x[2]-1, x[3]+1), S)
            if result:
                que.append('B')
                return 1

        if x[1] > 0:
            result = func((x[0], x[1]-1, x[2]+1, x[3]+1), S)
            if result:
                que.append('C')
                return 1
    elif S[i] == 'AC':
        if x[2] > 0:
            result = func((x[0]+1, x[1], x[2]-1, x[3]+1), S)
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1], x[2]+1, x[3]+1), S)
            if result:
                que.append('C')
                return 1
    return 0


N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0

result = func((A, B, C, i), S)
if result == 1:
    while len(que) > 0:
        print(que.pop())
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i)
que = deque()
def func(x):
    i = x[3]
    if x[3] == N:
        print('Yes')
        return 1
    if S[i] == 'AB':
        if x[1] > 0:
            result = func((x[0]+1, x[1]-1, x[2], x[3]+1))
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1]+1, x[2], x[3]+1))
            if result:
                que.append('B')
                return 1
    elif S[i] == 'BC':
        if x[2] > 0:
            result = func((x[0], x[1]+1, x[2]-1, x[3]+1))
            if result:
                que.append('B')
                return 1

        if x[1] > 0:
            result = func((x[0], x[1]-1, x[2]+1, x[3]+1))
            if result:
                que.append('C')
                return 1
    elif S[i] == 'AC':
        if x[2] > 0:
            result = func((x[0]+1, x[1], x[2]-1, x[3]+1))
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1], x[2]+1, x[3]+1))
            if result:
                que.append('C')
                return 1
    return 0


N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0

result = func((A, B, C, i))
if result == 1:
    while len(que) > 0:
        print(que.pop())
else:
    print('No')