Skip to content

Commit

Permalink
BOJ-EX: 2024. 9. 10. 오후 1:38:07
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhoJJang committed Sep 10, 2024
1 parent eba05f7 commit d3635ad
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
78 changes: 78 additions & 0 deletions 16948번: 데스 나이트/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* Problem Number: 16948 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahchjang <boj.kr/u/ahchjang> +#+ +#+ +#+ */
/* +#+ +#+ +#+ */
/* https://boj.kr/16948 #+# #+# #+# */
/* Solved: 2024/09/10 13:20:48 by ahchjang ### ### ##.kr */
/* */
/* ************************************************************************** */

import java.io.*;
import java.util.*;

public class Main {

static int[][] map;
static final int[] dx = new int[] { -2, -2, 0, 0, +2, +2 };
static final int[] dy = new int[] { -1, +1, -2, +2, -1, +1 };

static class Cord {
int r;
int c;

public Cord(int r, int c) {
this.r = r;
this.c = c;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int r1 = Integer.parseInt(st.nextToken());
int c1 = Integer.parseInt(st.nextToken());
int r2 = Integer.parseInt(st.nextToken());
int c2 = Integer.parseInt(st.nextToken());

map = new int[N][N];

for (int i = 0; i < N; i++) {
Arrays.fill(map[i], -1);
}

Queue<Cord> queue = new LinkedList<>();
Cord start = new Cord(r1, c1);
queue.add(start);
map[r1][c1] = 0;

while (!queue.isEmpty()) {
Cord cur = queue.poll();
int cx = cur.r;
int cy = cur.c;

if (cx == r2 && cy == c2)
break;

for (int i = 0; i < 6; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];

if (nx < 0 || nx >= N || ny < 0 || ny >= N)
continue;
if (map[nx][ny] != -1)
continue;

Cord next = new Cord(nx, ny);
map[nx][ny] = map[cx][cy] + 1;
queue.add(next);
}
}

System.out.println(map[r2][c2]);

}
}
38 changes: 38 additions & 0 deletions 16948번: 데스 나이트/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 16948번: 데스 나이트 - <img src="https://static.solved.ac/tier_small/10.svg" style="height:20px" /> Silver I

<!-- performance -->

<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->

<!-- end -->

## 문제

[문제 링크](https://boj.kr/16948)


<p>게임을 좋아하는 큐브러버는 체스에서 사용할 새로운 말 "데스 나이트"를 만들었다. 데스 나이트가 있는 곳이 (r, c)라면,&nbsp;(r-2, c-1), (r-2, c+1), (r, c-2), (r, c+2), (r+2, c-1), (r+2, c+1)로 이동할 수 있다.</p>

<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>

<p>데스 나이트는 체스판 밖으로 벗어날 수 없다.</p>



## 입력


<p>첫째 줄에 체스판의 크기 N(5 ≤ N ≤ 200)이 주어진다. 둘째 줄에 r<sub>1</sub>, c<sub>1</sub>, r<sub>2</sub>, c<sub>2</sub>가 주어진다.</p>



## 출력


<p>첫째 줄에 데스 나이트가 (r<sub>1</sub>, c<sub>1</sub>)에서 (r<sub>2</sub>, c<sub>2</sub>)로 이동하는 최소 이동 횟수를 출력한다. 이동할 수 없는 경우에는 -1을 출력한다.</p>



## 소스코드

[소스코드 보기](Main.java)

0 comments on commit d3635ad

Please sign in to comment.