首页 > 解决方案 > 如何在 python 程序上找到运行时错误错误

问题描述

所以我尝试用我的 python 代码解决https://open.kattis.com/problems/10kindsofpeople,我认为代码很好并且通过了 22/25 测试用例,但是在测试用例 23 中存在运行时错误。

代码在这里:

if __name__ == "__main__":

    
    def walk(arr,r1,c1,r2,c2,rows,cols, history):
        
        history['{0},{1}'.format(r1,c1)] = True

        # print('{},{}-{},{}'.format(r1,c1,r2,c2))
        if arr[r1][c1] == arr[r2][c2]:
            if r1 == r2 and c1 == c2:
                return True

            if r1-1 >= 0 and '{0},{1}'.format(r1-1, c1) not in history:
                atas = walk(arr, r1-1,c1,r2,c2,rows,cols,history)
            else:
                atas=False

            if r1+1 < rows and '{0},{1}'.format(r1+1, c1) not in history:
                bawah = walk(arr,r1+1,c1,r2,c2,rows,cols,history)
            else:
                bawah=False

            if c1-1 >= 0 and '{0},{1}'.format(r1, c1-1) not in history:
                kiri = walk(arr,r1,c1-1,r2,c2,rows,cols,history)
            else:
                kiri=False

            if c1+1 < cols and '{0},{1}'.format(r1, c1+1) not in history:
                kanan = walk(arr,r1,c1+1,r2,c2,rows,cols,history)
            else:
                kanan = False            

            # if one of them == true , there is a path to destination
            if atas or bawah or kiri or kanan:
                return True
            else:
                return False
        
        else:
            return False

    map = input()

    rows, cols = map.split(" ")    
    rows = int(rows)
    cols = int(cols)

    arr_row = []
    for i in range(int(rows)):
        str_inp = input()
        list_int = [int(i) for i in str_inp]
        arr_row.append(list_int)   
    
    coord_row=input()
    coord_pair=[]
    for i in range(int(coord_row)):

        r1,c1,r2,c2 = input().split(" ")

        coord_pair.append([r1,c1,r2,c2])


    # print(arr_row)
    for c in coord_pair:
        r1 = int(c[0]) - 1
        c1 = int(c[1]) - 1
        r2 = int(c[2]) - 1
        c2 = int(c[3]) - 1

        history = {}
        if arr_row[r1][c1] != arr_row[r2][c2]:
            print("neither")
        elif walk(arr_row, r1, c1, r2, c2, rows, cols, history):
            ret = 'binary' if arr_row[r1][c1] == 0 else 'decimal'
            print(ret)
        else:
            print('neither') 

我认为隐藏测试用例的输入有错误,如果有人能找到错误,我将不胜感激,谢谢

标签: pythonpython-3.x

解决方案


推荐阅读