Skip to content

Commit 9ec20c5

Browse files
committed
[Silver I] Title: 나이트의 이동, Time: 264 ms, Memory: 74044 KB -BaekjoonHub
1 parent 2189ed1 commit 9ec20c5

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver I] 나이트의 이동 - 7562
2+
3+
[문제 링크](https://www.acmicpc.net/problem/7562)
4+
5+
### 성능 요약
6+
7+
메모리: 74044 KB, 시간: 264 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 그래프 이론, 그래프 탐색, 최단 경로, 격자 그래프
12+
13+
### 제출 일자
14+
15+
2025년 12월 7일 21:11:25
16+
17+
### 문제 설명
18+
19+
<p>체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수 있을까?</p>
20+
21+
<p><img alt="" src="https://www.acmicpc.net/upload/images/knight.png" style="height:172px; width:175px"></p>
22+
23+
### 입력
24+
25+
<p>입력의 첫째 줄에는 테스트 케이스의 개수가 주어진다.</p>
26+
27+
<p>각 테스트 케이스는 세 줄로 이루어져 있다. 첫째 줄에는 체스판의 한 변의 길이 l(4 ≤ l ≤ 300)이 주어진다. 체스판의 크기는 l × l이다. 체스판의 각 칸은 두 수의 쌍 {0, ..., l-1} × {0, ..., l-1}로 나타낼 수 있다. 둘째 줄과 셋째 줄에는 나이트가 현재 있는 칸, 나이트가 이동하려고 하는 칸이 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>각 테스트 케이스마다 나이트가 최소 몇 번만에 이동할 수 있는지 출력한다.</p>
32+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
private static int[][] d = {{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
6+
private static int l;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
int t = Integer.parseInt(br.readLine());
12+
13+
StringBuilder sb = new StringBuilder();
14+
for(int test=0; test<t; test++) {
15+
l = Integer.parseInt(br.readLine());
16+
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
int[] start = new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
19+
20+
st = new StringTokenizer(br.readLine());
21+
int[] end = new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
22+
23+
int result = bfs(start, end);
24+
25+
sb.append(result).append("\n");
26+
}
27+
28+
System.out.println(sb.toString().trim());
29+
}
30+
31+
private static int bfs(int[] start, int[] end) {
32+
Queue<int[]> queue = new LinkedList<>();
33+
boolean[][] visited = new boolean[l][l];
34+
35+
queue.offer(new int[] {start[0], start[1], 0});
36+
visited[start[0]][start[1]] = true;
37+
38+
while(!queue.isEmpty()) {
39+
int[] info = queue.poll();
40+
int x = info[0];
41+
int y = info[1];
42+
int count = info[2];
43+
44+
if(x == end[0] && y == end[1]) return count;
45+
46+
for(int i=0; i<d.length; i++) {
47+
int nx = x + d[i][0];
48+
int ny = y + d[i][1];
49+
if(nx<0 || nx>=l || ny<0 || ny>=l || visited[nx][ny]) continue;
50+
51+
visited[nx][ny] = true;
52+
queue.offer(new int[] {nx, ny, count+1});
53+
}
54+
}
55+
56+
return -1;
57+
}
58+
}

0 commit comments

Comments
 (0)