강의로 돌아가기
이기태

파이썬 정확성 4번, 8번 (도와주세요)

코드가 새로고침이 안되네요
n = len(maps[0])
m = len(maps)

이고, 효율성까지 다 맞는데 정확성 4번, 8번만 틀리네요.. 질문하기 다 뒤져봐도 저 같은 케이스는 없는 것 같아서 도움을 요청해봅니다 ㅠ

작성중인 코드―solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import deque

def solution(maps):
    n = len(maps)
    m = len(maps[0])
    visited = [[False for col in range(len(maps[0]))] for row in range(len(maps))]
    visited[0][0] = True

    queue = deque()
    queue.append((0,0,1))

    while queue:
        yIndex, xIndex, distance = queue.popleft()
        if xIndex == n-1 and yIndex == m-1:
            return distance

        if 0<xIndex+1<n and visited[yIndex][xIndex+1] == False and maps[yIndex][xIndex+1] == 1:
            queue.append((yIndex, xIndex+1, distance+1))
            visited[yIndex][xIndex+1] = True
        if 0<yIndex+1<m and visited[yIndex+1][xIndex] == False and maps[yIndex+1][xIndex] == 1:
            queue.append((yIndex+1, xIndex, distance+1))
            visited[yIndex+1][xIndex] = True
        if 0<xIndex-1<n and visited[yIndex][xIndex-1] == False and maps[yIndex][xIndex-1] == 1:
            queue.append((yIndex, xIndex-1, distance+1))
            visited[yIndex][xIndex-1] = True
        if 0<yIndex-1<m and visited[yIndex-1][xIndex] == False and maps[yIndex-1][xIndex] == 1:
            queue.append((yIndex-1, xIndex, distance+1))
            visited[yIndex-1][xIndex] = True

    return -1
  • 때똥이

    방문 가능한 인덱스의 범위는 0 <= next_x < N 입니다

    때똥이―Aug 28, 2022 23:02
  • 이기태

    헉 감사합니당,, 바로 해결됐어요

    이기태―Aug 30, 2022 19:49
0 개의 답변
답변 쓰기
This input form supports markdown syntax. Please refer to 마크다운 가이드.