나이트의 이동

ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ넘나 감동 ㅠㅠㅠ

에러가 났던 이유는 두코드의 차이 때문이다.

visited = [[0]*col]*col # 같은것을 col번 복사.
visited = [[0]*col for _ in range(col)] # 같은 내용이지만 매번 다른 col개의 공간

전체코드

# 3 캐이스
# 8 체스판의 크기
# 0 0 시작
# 7 0 도착

# 100 체스판의 크기
# 0 0 시작
# 30 50 도착

# 10 체스판의 크기
# 1 1 시작
# 1 1 도착

# 출력
# 5
# 28
# 0


case = int(input()) # 3
move = [[-1,2],[1,2],[-1,-2],[1,-2],[2,1],[-2,1],[2,-1],[-2,-1]]

def bfs(start,end,visited):
  todo = [start]

  while todo:
    curr = todo.pop(0)
    x = curr[0]
    y = curr[1]

    now = visited[x][y]
    if curr == end:
      print(now)
      break

    for i in range(len(move)):
      wx = x + move[i][0]
      wy = y + move[i][1]

      if wx >= size or wy >= size or wx < 0 or wy < 0:
        continue

      if visited[wx][wy] == 0 and [wx,wy] != start:
        visited[wx][wy] = now + 1
        todo.append([wx,wy])

for c in range(case):
  size = int(input()) # 8
  visited = [[0]*size for _ in range(size)]

  start = [int(i) for i in input().split()]
  end = [int(i) for i in input().split()]

  bfs(start,end,visited)

results matching ""

    No results matching ""