doctscoder
하고싶은일있는개발
doctscoder
전체 방문자
오늘
어제
  • 분류 전체보기 (305)
    • 코테용 문제풀이 (304)
      • 백준 (272)
      • 알고스팟 (32)
    • 공부계획 (1)

최근 글

hELLO · Designed By 정상우.
doctscoder

하고싶은일있는개발

코테용 문제풀이/백준

나이트의 이동 풀이

2023. 2. 7. 20:08

문제 링크: https://www.acmicpc.net/problem/7562

백준 알고리즘 기초 2/2 600에서 9번째 - 7562번 나이트의 이동을 풀어보았다.

 

풀이: bfs를 쓰는 문제이다.

 

C++

 

Python

from collections import deque

def bfs(a,b,c,d):
	dx=[-1,1,2,2,1,-1,-2,-2]
	dy=[2,2,1,-1,-2,-2,-1,1]
	queue=deque()
	queue.append((a,b))
	while queue:
		x,y=queue.popleft()
		if x==c and y==d:
			return graph[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 graph[nx][ny]==0:
				graph[nx][ny]=graph[x][y]+1
				queue.append((nx,ny))

t=int(input())
for _ in range(t):
	l=int(input())
	cx,cy=map(int,input().split())
	ox,oy=map(int,input().split())
	graph=[[0]*l for i in range(l)]
	graph[cx][cy]=1
	print(bfs(cx,cy,ox,oy))

Java

 

저작자표시 비영리 변경금지 (새창열림)

'코테용 문제풀이 > 백준' 카테고리의 다른 글

서울 지하철 2호선 풀이  (0) 2023.02.07
Two Dots 풀이  (0) 2023.02.07
토마토 풀이  (0) 2023.02.07
미로 탐색 풀이  (0) 2023.02.07
섬의 개수 풀이  (0) 2023.02.02
    '코테용 문제풀이/백준' 카테고리의 다른 글
    • 서울 지하철 2호선 풀이
    • Two Dots 풀이
    • 토마토 풀이
    • 미로 탐색 풀이
    doctscoder
    doctscoder
    코딩 관련 공부를 적어놓는 블로그

    티스토리툴바