반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- vocabulary
- homogeneous
- 영어회화
- SW역량테스트
- 문제풀이
- 미방
- speech
- English
- ODEs
- Conversation
- Ode
- 공수 문제풀이
- Python
- 공업수학
- Nonhomogeneous ODEs
- 삼성SW역량테스트
- 코딩테스트
- 스피치 연습
- kreyszig
- 백준
- 공수
- Problem set 2.7
- 공수1
- Problem Set 1.4
- Advanced Engineering Mathematics
- 공학수학
- 미분방정식
- 비제차 상미분 방정식
- Problem set 1.5
- Homogeneous ODEs
Archives
- Today
- Total
한걸음
백준 7562 : 나이트의 이동 본문
반응형
https://www.acmicpc.net/problem/7562
https://github.com/HPYoo/swcodingtest/blob/main/prob7562.py
1. 아주 쉬운 문제
나이트의 이동가능 경우의 수를 지정해주고 BFS 로 길을 찾으면 된다. 시작점과 도착점이 같으면 바로 0출력
사소한 실수하지말고 차근차근하면 아주 쉽게 풀림
2. 전체코드
메모리 : 131788 KB 시간 : 420 ms , 풀이시간 : 20 min
from collections import deque
test_case = int(input())
# 상, 하, 좌, 우
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# 상상우, 우우상, 우우하, 하하우, 하하좌, 좌좌하, 좌좌상, 상상좌
k_path = [[0, 0, 3], [3, 3, 0], [3, 3, 1], [1, 1, 3], [1, 1, 2], [2, 2, 1], [2, 2, 0], [0, 0, 2]]
result = []
for test in range(test_case):
N = int(input())
sx, sy = map(int, input().split())
ax, ay = map(int, input().split())
visited = [[False for _ in range(N)] for _ in range(N)]
q = deque([[sx, sy, 0]])
visited[sx][sy] = True
if sx == ax and sx == ay :
result.append(0)
else :
while q :
x, y, cnt = q.popleft()
if x == ax and y == ay :
result.append(cnt)
break
for path in k_path :
nx = x + dx[path[0]] + dx[path[1]] + dx[path[2]]
ny = y + dy[path[0]] + dy[path[1]] + dy[path[2]]
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
visited[nx][ny] = True
q.append([nx, ny, cnt + 1])
for i in range(test_case):
print(result[i])
반응형
'Coding Test' 카테고리의 다른 글
백준 2210 : 숫자판 점프 (1) | 2023.12.06 |
---|---|
백준 1986 : 체스 (1) | 2023.12.05 |
백준 2234 : 성곽 (3) | 2023.11.29 |
백준 2468 : 안전영역 (2) | 2023.11.25 |
백준 2589 : 보물섬 (2) | 2023.11.24 |