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
- 노코드AI
- 혼공머신러닝딥러닝
- 삼성SDS Brigthics
- 직원 이직여부
- 모델링
- Brigthics
- 브라이틱스 서포터즈
- 캐글
- 혼공
- 혼공학습단
- Brightics를 이용한 분석
- 브라이틱스
- Brightics Studio
- 혼공머신
- 개인 의료비 예측
- 삼성 SDS Brigthics
- Brigthics를 이용한 분석
- Brightics
- 영상제작기
- 삼성 SDS
- 삼성SDS
- 삼성SDS Brightics
- 직원 이직률
- Brigthics Studio
- 포스코 청년
- 포스코 아카데미
- 데이터 분석
- 데이터분석
- 팀 분석
- 추천시스템
Archives
- Today
- Total
데이터사이언스 기록기📚
[백준/Python] 7562번(그래프, BFS)_나이트의 이동 본문
📌문제 유형
그래프 이론, 그래프 탐색, BFS(실버1)
📌문제
7562번: 나이트의 이동
체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수
www.acmicpc.net
📌나의 문제풀이
from collections import deque
t = int(input())
for _ in range(t):
l = int(input())
maps = [[0 for _ in range(l)] for _ in range(l)]
s_x, s_y = map(int,input().split())
e_x, e_y = map(int,input().split())
q = deque()
q.append([s_x, s_y])
maps[s_x][s_y] = 1
cnt = 0
while q:
x, y = q.popleft()
dx = [-2,-2,-1,-1,1,1,2,2]
dy = [-1,1,-2,2,-2,2,-1,1]
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < l and 0 <= ny < l:
if maps[nx][ny] == 0:
q.append([nx,ny])
maps[nx][ny] = maps[x][y] + 1
print(maps[e_x][e_y] - 1)
📌 다른사람의 문제풀이
from collections import deque
import sys
input = sys.stdin.readline
t = int(input().rstrip())
def bfs() :
dx = [-1, 1, 2, 2, 1, -1, -2, -2]
dy = [2, 2, 1, -1, -2, -2, -1, 1]
q = deque()
q.append((startX, startY))
while q :
x, y = q.popleft()
if x == endX and y == endY :
return matrix[x][y] -1
for i in range(8) :
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<l and 0<=ny<l and matrix[nx][ny] == 0 :
matrix[nx][ny] = matrix[x][y] + 1
q.append((nx,ny))
for _ in range(t) :
l = int(input().rstrip())
startX, startY = map(int, input().rstrip().split())
endX, endY = map(int, input().rstrip().split())
matrix = [[0]*l for _ in range(l)]
matrix[startX][startY] = 1
print(bfs())
📌 리뷰
- BFS는 '현재 그래프 값 + 1'로 만들어서 탐색하는 경우가 많다는 것을 잊지말기!
- maps[nx][ny] + 1을 답지 설명보고 깨달음...!
728x90
'Coding Test > 백준(Python)' 카테고리의 다른 글
[백준/Python] 1931번(그리디, 정렬)_회의실 배정 (0) | 2023.05.11 |
---|---|
[백준/Python] 17615번(그리디)_볼 모으기 (0) | 2023.05.09 |
[백준/Python] 2667번(그래프, DFS, BFS)_단지 번호 붙이기 (0) | 2023.05.07 |
[백준/Python] 1138번(구현)_한 줄로 서기 (0) | 2023.05.06 |
[백준/Python] 2583번(그래프, BFS, DFS)_영역 구하기 (0) | 2023.04.28 |
Comments