Skip to content

Commit 98b761f

Browse files
committed
[Silver I] Title: 나이트의 이동, Time: 264 ms, Memory: 78040 KB -BaekjoonHub
1 parent 02ed759 commit 98b761f

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-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+
메모리: 78040 KB, 시간: 264 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2024년 11월 14일 13:29:55
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
static int visited[][];
6+
static int arr[][];
7+
static int dx[] ={-1, -2, -2,-1, 1,2,2,1};
8+
static int dy[] = {-2,-1,1,2,-2,-1,1,2};
9+
static int I,startX,startY,endX,endY;
10+
public static void main(String[] args) throws IOException {
11+
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
int T = Integer.parseInt(br.readLine());
15+
StringTokenizer st;
16+
StringBuilder sb = new StringBuilder();
17+
18+
for(int i=0;i <T; i++) {
19+
I = Integer.parseInt(br.readLine());
20+
arr = new int[I][I];
21+
st = new StringTokenizer(br.readLine());
22+
startX = Integer.parseInt(st.nextToken());
23+
startY = Integer.parseInt(st.nextToken());
24+
25+
st = new StringTokenizer(br.readLine());
26+
endX = Integer.parseInt(st.nextToken());
27+
endY = Integer.parseInt(st.nextToken());
28+
visited = new int[I][I];
29+
if(startX == endX && startY == endY) {
30+
sb.append(0).append("\n");
31+
}
32+
else
33+
sb.append(bfs(startX,startY)).append("\n");
34+
}
35+
System.out.println(sb);
36+
}
37+
38+
static int bfs(int x, int y) {
39+
40+
Queue<int[]> queue = new LinkedList<>();
41+
42+
queue.add(new int[] {x,y});
43+
visited[x][y]=0;
44+
45+
while(!queue.isEmpty()) {
46+
47+
int size = queue.size();
48+
49+
int now[] = queue.poll();
50+
int nowx = now[0];
51+
int nowy = now[1];
52+
53+
for(int i=0; i< 8; i++) {
54+
int nx = nowx + dx[i];
55+
int ny = nowy + dy[i];
56+
if(nx >=0&& nx <I && ny >=0 && ny <I) {
57+
if(visited[nx][ny] ==0) {
58+
visited[nx][ny] = visited[nowx][nowy]+1;
59+
queue.add(new int[]{nx,ny});
60+
if(nx == endX && ny == endY) {
61+
return visited[nx][ny];
62+
}
63+
}
64+
}
65+
}
66+
67+
}
68+
return -1;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)