파이썬 시간이 줄어드는 요소들 정리한거

2021. 7. 8. 09:32·코딩테스트/Python

1. 테두리검사는 def로 안하는게 좋은듯.

(다른사람꺼 코드)

# 나이트의 이동
# https://www.acmicpc.net/problem/7562

# pypy3 -> 통과
# python3 -> 시간초과

import sys
from collections import deque

input = sys.stdin.readline
adjacent_indices = ((-2, -1), (-1, -2), (1, -2), (2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1))


def is_valid_index(board, r, c):
    if r < 0 or c < 0:
        return False

    if len(board) <= r:
        return False

    if len(board[r]) <= c:
        return False

    return True


def solve():
    l = int(input())
    s_c, s_r = map(int, input().split())
    d_c, d_r = map(int, input().split())

    if s_c == d_c and s_r == d_r:
        return 0

    board = [[-1] * l for __ in range(l)]
    board[s_r][s_c] = 0

    answer = 0
    queue = deque()
    queue.append((s_r, s_c))

    while len(queue) > 0:
        cur_r, cur_c = queue.popleft()

        for adjacent_index in adjacent_indices:
            next_r = cur_r + adjacent_index[0]
            next_c = cur_c + adjacent_index[1]

            if is_valid_index(board, next_r, next_c):
                if board[next_r][next_c] == -1:
                    board[next_r][next_c] = board[cur_r][cur_c] + 1

                    if next_r == d_r and next_c == d_c:
                        return board[next_r][next_c]

                    queue.append((next_r, next_c))

    print(answer)


t = int(input())

for _ in range(t):
    answer = solve()
    print(answer)
# 나이트의 이동
# https://www.acmicpc.net/problem/7562

import sys
from collections import deque

input = sys.stdin.readline
adjacent_indices = ((-2, -1), (-1, -2), (1, -2), (2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1))


# def is_valid_index(board, r, c):
#     if r < 0 or c < 0:
#         return False

#     if len(board) <= r:
#         return False

#     if len(board[r]) <= c:
#         return False

#     return True


def solve():
    l = int(input())
    s_c, s_r = map(int, input().split())
    d_c, d_r = map(int, input().split())

    if s_c == d_c and s_r == d_r:
        return 0

    board = [[-1] * l for __ in range(l)]
    board[s_r][s_c] = 0

    answer = 0
    queue = deque()
    queue.append((s_r, s_c))

    while len(queue) > 0:
        cur_r, cur_c = queue.popleft()

        for adjacent_index in adjacent_indices:
            next_r = cur_r + adjacent_index[0]
            next_c = cur_c + adjacent_index[1]

            if 0 <= next_r < l and 0 <= next_c < l:
                if board[next_r][next_c] == -1:
                    board[next_r][next_c] = board[cur_r][cur_c] + 1

                    if next_r == d_r and next_c == d_c:
                        return board[next_r][next_c]

                    queue.append((next_r, next_c))

    print(answer)


t = int(input())

for _ in range(t):
    answer = solve()
    print(answer)

 

'코딩테스트 > Python' 카테고리의 다른 글

1335. Minimum Difficulty of a Job Schedule 풀이 python  (1) 2023.12.30
codility의 PrettyTiling 문제를 보며 느낀점  (0) 2023.02.08
백준 input = sys.stdin.readline 차이  (0) 2021.07.02
'코딩테스트/Python' 카테고리의 다른 글
  • 1335. Minimum Difficulty of a Job Schedule 풀이 python
  • codility의 PrettyTiling 문제를 보며 느낀점
  • 백준 input = sys.stdin.readline 차이
용나리
용나리
  • 용나리
    티스토리 블로그
    용나리
  • 전체
    오늘
    어제
    • 분류 전체보기 (334)
      • 과거의 것들 (93)
        • AI Tech boostcamp (92)
      • 생각정리(고찰) (2)
      • 기술 글 (1)
      • 코딩테스트 (4)
        • C++ (0)
        • Python (4)
      • CS (121)
        • 컴퓨터 시스템 (4)
        • 코틀린 인 액션 (13)
        • 김영한 스프링 강의 (104)
      • 일지 남기기용 (11)
        • 운동 (10)
      • 개발 배포 해보기 (1)
      • 프로그래밍 언어 및 기타 (32)
        • Spring Boot (9)
        • Python (9)
        • Kotlin (1)
        • Flutter (2)
        • SQL (4)
        • Docker (3)
        • 공통 (4)
      • os (4)
        • Linux (4)
      • 기술 (17)
        • PyTorch (6)
        • Computer Vision (6)
        • NLP (1)
        • 기타 (4)
      • 제품 후기 (0)
      • 게임 (0)
        • Human Resource Machine (0)
      • 강의 (26)
        • fullstackdeeplearning_sprin.. (9)
        • 부캠 안드로이드 학습정리 (17)
      • 개인 메모 (10)
      • IT 기타 (5)
      • 논문 읽기 연습 (5)
        • Computer Vision (1)
        • NLP (0)
        • 공통 (2)
        • 그냥 메모 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬 실행경로
    pip install killed
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
용나리
파이썬 시간이 줄어드는 요소들 정리한거
상단으로

티스토리툴바