Problem p02823 (python)

Fixed submission:
n, a, b = map(int, input().split())

def jud(n,a,b):
    distance_dic_before_a = []
    distance_dic_before_b = []
    distance_dic_before_a.append(a)
    distance_dic_before_b.append(b)

    turn_count = 1
    while True:
        distance_dic_after_a = []
        for a_dir in distance_dic_before_a:
            a_dir_plus = a_dir + 1
            a_dir_minus = a_dir - 1
            if a_dir_plus > n:
                a_dir_plus = n
            if a_dir_minus == 0:
                a_dir_minus = 1
            distance_dic_after_a.append(a_dir_plus)
            distance_dic_after_a.append(a_dir_minus)
        distance_dic_before_a = distance_dic_after_a

        distance_dic_after_b = []
        for a_dir in distance_dic_before_b:
            a_dir_plus = a_dir + 1
            a_dir_minus = a_dir - 1
            if a_dir_plus > n:
                a_dir_plus = n
            if a_dir_minus == 0:
                a_dir_minus = 1
            distance_dic_after_b.append(a_dir_plus)
            distance_dic_after_b.append(a_dir_minus)
        distance_dic_before_b = distance_dic_after_b

        set_a = set(distance_dic_after_a)
        set_b = set(distance_dic_after_b)
        print(set_a, set_b)
        result_set = set_a & set_b
        if len(result_set) > 0:
            return turn_count

        turn_count +=1

def jud_2(n,a,b):
    if abs(a-b) % 2 == 0:
        return abs(a-b) // 2
 
    x = n - a
    y = b - 1
 
    if x > y:
        choice_1 = a-1+1 + (y-a) // 2
        if choice_1 > y:
            return y
        else:
            return choice_1
    else:
        choice_2 = n-b+1 + (x-(n-b+1)) // 2
        if choice_2 > x:
            return x
        else:
            return choice_2
# print(jud(n, a, b))
print(jud_2(n,a,b))

Buggy submission (verdict: Runtime Error):
import time

def fib(n):
    if n == 0 or n == 1:
        return 1
    
    return fib(n-1) + fib(n-2)

class Fib_memo:
    def __init__(self):
        self.fib_memo = {}

    def cal_fib(self, n):
        if n == 0 or n == 1:
            self.fib_memo[n] = 1
            return 1

        if n in self.fib_memo.keys():
            return self.fib_memo[n]
        
        self.fib_memo[n] = self.cal_fib(n-1) + self.cal_fib(n-2)
        return self.fib_memo[n]

def fib_dynamic(n):
    # 結果を保持する辞書
    cal_result = {}

    # 初期値の設定
    cal_result[0] = 1
    cal_result[1] = 1

    for i in range(2, n+1):
        cal_result[i] = cal_result[i-1] + cal_result[i-2]

    return cal_result[n]

# n = int(input())
cost_1 = []
cost_2 = []
cost_3 = []

max_n = 15
for n in range(1, max_n+1):
    print("n:{}".format(n))
    time_sta = time.perf_counter()
    fib(n)
    time_end = time.perf_counter()
    print("It takes {} seconds".format(time_end-time_sta))
    cost_1.append(time_end-time_sta)

    time_sta = time.perf_counter()
    fib_memo = Fib_memo()
    fib_memo.cal_fib(n)
    time_end = time.perf_counter()
    print("It takes {} seconds".format(time_end-time_sta))
    cost_2.append(time_end-time_sta)

    time_sta = time.perf_counter()
    fib_dynamic(n)
    time_end = time.perf_counter()
    print("It takes {} seconds".format(time_end-time_sta))
    cost_3.append(time_end-time_sta)

import matplotlib.pyplot as plt
x = [i for i in range(1, max_n+1)]
fig = plt.figure()
fig.subplots_adjust(left=0.15)
plt.plot(x, cost_1, label="Normal")
plt.plot(x, cost_2, label="Memo")
plt.plot(x, cost_3, label="Dynamic")
plt.xlim([0, max_n])
plt.xticks(x)
# 凡例の表示
plt.legend()
plt.title("Cost Time")
plt.xlabel("n")
plt.ylabel("time[s]")
# 画像保存
plt.savefig("./cal_time.png")