Skip to content

Commit 28d9a89

Browse files
committed
[Silver I] Title: 데스 나이트, Time: 120 ms, Memory: 14784 KB -BaekjoonHub
1 parent 97b077b commit 28d9a89

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver I] 데스 나이트 - 16948
2+
3+
[문제 링크](https://www.acmicpc.net/problem/16948)
4+
5+
### 성능 요약
6+
7+
메모리: 14784 KB, 시간: 120 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2024년 12월 5일 10:34:46
16+
17+
### 문제 설명
18+
19+
<p>게임을 좋아하는 큐브러버는 체스에서 사용할 새로운 말 "데스 나이트"를 만들었다. 데스 나이트가 있는 곳이 (r, c)라면, (r-2, c-1), (r-2, c+1), (r, c-2), (r, c+2), (r+2, c-1), (r+2, c+1)로 이동할 수 있다.</p>
20+
21+
<p>크기가 N×N인 체스판과 두 칸 (r<sub>1</sub>, c<sub>1</sub>), (r<sub>2</sub>, c<sub>2</sub>)가 주어진다. 데스 나이트가 (r<sub>1</sub>, c<sub>1</sub>)에서 (r<sub>2</sub>, c<sub>2</sub>)로 이동하는 최소 이동 횟수를 구해보자. 체스판의 행과 열은 0번부터 시작한다.</p>
22+
23+
<p>데스 나이트는 체스판 밖으로 벗어날 수 없다.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 체스판의 크기 N(5 ≤ N ≤ 200)이 주어진다. 둘째 줄에 r<sub>1</sub>, c<sub>1</sub>, r<sub>2</sub>, c<sub>2</sub>가 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 데스 나이트가 (r<sub>1</sub>, c<sub>1</sub>)에서 (r<sub>2</sub>, c<sub>2</sub>)로 이동하는 최소 이동 횟수를 출력한다. 이동할 수 없는 경우에는 -1을 출력한다.</p>
32+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int r1,c1,r2,c2,N;
6+
static int[] dx = {-2, -2, 0, 0, 2, 2};
7+
static int[] dy = {-1, 1, -2, 2, -1, 1};
8+
static int answer = 0;
9+
static int visited[][];
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
13+
N = Integer.parseInt(br.readLine());
14+
15+
visited = new int[N][N];
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
for(int i =0; i<N; i++) {
18+
for(int j =0; j<N; j++) {
19+
visited[i][j] = -1;
20+
}
21+
}
22+
r1 = Integer.parseInt(st.nextToken());
23+
c1 = Integer.parseInt(st.nextToken());
24+
r2 = Integer.parseInt(st.nextToken());
25+
c2 = Integer.parseInt(st.nextToken());
26+
27+
bfs(r1,c1);
28+
if(visited[r2][c2] == 0 ){
29+
System.out.println(-1);
30+
}
31+
else
32+
System.out.println(visited[r2][c2]);
33+
}
34+
35+
static void bfs(int x, int y) {
36+
37+
Queue<int[] > queue = new LinkedList<>();
38+
39+
queue.add(new int[] {x,y});
40+
visited[x][y] =0;
41+
42+
while(!queue.isEmpty()) {
43+
int now[] = queue.poll();
44+
int nowx = now[0];
45+
int nowy = now[1];
46+
47+
if(nowx == r2 && nowy == c2) {
48+
break;
49+
}
50+
51+
for(int i =0; i<6; i++) {
52+
int nx = nowx + dx[i];
53+
int ny = nowy + dy[i];
54+
55+
if(nx >=0 && nx <N && ny >=0 && ny <N) {
56+
if(visited[nx][ny] == -1 || visited[nx][ny] > visited[nowx][nowy]+1) {
57+
queue.add(new int[] {nx,ny});
58+
visited[nx][ny] = visited[nowx][nowy]+1;
59+
}
60+
}
61+
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)