Prosty kod i zawiecha w teście

0

Witam :) Mam kod do programu, który wyszukuje w macierzy ścieżkę z punktu1 do 2 złożoną z jednakowych cyfr:

def can_pass(matrix, first, second):
    x = first[0]
    y = first[1]
    i = second[0]
    j = second[1]
    w = matrix[i][j]
    q = str(first[0])+str(first[1]) + ', '
    last = "S"
    start = matrix[x][y]
    goal = matrix[i][j]
    while not (x == i  and y == j):
        if last == "S":
            if matrix[x][y - 1] == w:
                last = "W"
                y -= 1
                q += str(x)+str(y) + ', '
            else:
                if matrix[x+1][y] == w:
                    last = "S"
                    x += 1
                    q += str(x)+str(y)+', '
                else:
                    if matrix[x][y+1] == w:
                        last = "E"
                        y += 1
                        q += str(x)+str(y)+', '
                    else:
                        if matrix[x-1][y] == w and str(x-1)+str(y) not in q:
                            last = "N"
                            x -= 1
                            q += str(x)+str(y)+', '
                        else:
                            return False
        if last == "W":
            if matrix[x-1][y] == w:
                last = "N"
                x -= 1
                q += str(x)+str(y)+', '
            else:
                if matrix[x][y-1] == w :
                    last = "W"
                    y -=  1
                    q += str(x)+str(y)+', '
                else:
                    if matrix[x+1][y] == w:
                        last = "S"
                        x += 1
                        q += str(x)+str(y)+', '
                    else:
                        if matrix[x][y+1] == w and str(x)+str(y+1) not in q:
                            last = "E"
                            y += 1
                            q += str(x)+str(y)+', '
                        else:
                            return False                                
        if last == "E":
            if matrix[x+1][y] == w:
                last = "S"
                x += 1
                q += str(x)+str(y)+', '
            else:
                if matrix[x][y+1] == w :
                    last = "E"
                    y += 1
                    q += str(x)+str(y)+', '
                else:
                    if matrix[x-1][y] == w:
                        last = "N"
                        x -= 1
                        q += str(x)+str(y)+', '
                    else:
                        if matrix[x][y-1] == w and str(x)+str(y-1) not in q:
                            last = "W"
                            y -= 1
                            q += str(x)+str(y)+', '
                        else:
                            return False
        if last == "N":
            if matrix[x][y+1] == w:
                last = "E"
                y += 1
                q += str(x)+str(y)+', '
            else:
                if matrix[x-1][y] == w :
                    last = "N"
                    x -= 1
                    q += str(x)+str(y)+', '
                else:
                    if matrix[x][y-1] == w:
                        last = "W"
                        y -= 1
                        q += str(x)+str(y)+', '
                    else:
                        if matrix[x+1][y] == w and str(x+1)+str(y) not in q:
                            last = "S"
                            x += 1
                            q += str(x)+str(y)+', '
                        else:
                            return False   
    return True 

2 testy przechodzi ok, przy trzecim dla danych: can_pass(((0,0),(0,0)), (0,0), (1,1)) łapie zawiechę. Jest "niezdecydowany" którą ścieżkę wybrać? Co poprawić? To moje początki więc proszę o wyrozumiałość ;)

0

Użyj debugera. Nikt tego za ciebie nie zrobi. Prześledź wykonanie linia po linii i zobacz co się dzieje.

1 użytkowników online, w tym zalogowanych: 0, gości: 1