-
[Baekjoon Online Judge] 7562번: 나이트의 이동문제 풀이/Baekjoon Online Judge 2021. 2. 25. 16:06
7562번: 나이트의 이동
체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수
www.acmicpc.net
요구사항
- 체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수 있을까?
입력
- 입력의 첫째 줄에는 테스트 케이스의 개수가 주어진다.
- 각 테스트 케이스는 세 줄로 이루어져 있다. 첫째 줄에는 체스판의 한 변의 길이 l(4 ≤ l ≤ 300)이 주어진다. 체스판의 크기는 l × l이다. 체스판의 각 칸은 두 수의 쌍 {0, ..., l-1} × {0, ..., l-1}로 나타낼 수 있다. 둘째 줄과 셋째 줄에는 나이트가 현재 있는 칸, 나이트가 이동하려고 하는 칸이 주어진다.
출력
- 각 테스트 케이스마다 나이트가 최소 몇 번만에 이동할 수 있는지 출력한다.
나이트가 이동할 수 있는 좌표의 개수는 총 8개로, x 배열과 y 배열로 이동할 수 있는 좌표를 미리 생성해둔다. 기존에 해결하였던 bfs문제와 동일하게 너비 탐색을 하며 도착 좌표 x, y에 닿을 때 까지 반복한다. 이동한 x, y의 좌표가 endX, endY의 좌표와 같으면 도착 좌표라고 가정하고, 기존까지 움직였던 count에 + 1을 해주어 반환해준다.
나이트의 이동 좌표와 반환 시점을 잘 설정해주면 쉽게 해결할 수 있었다.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class Baekjoon7562 { static int[][] board; static boolean[][] visited; static int l, startX, startY, endX, endY; static int[] xMove = {-2, -1, 1, 2, 2, 1, -1, -2}; static int[] yMove = {1, 2, 2, 1, -1, -2, -2, -1}; static class Location { int x, y; public Location(int x, int y) { this.x = x; this.y = y; } } private static int bfs(int startX, int startY) { if (startX == endX && startY == endY) return 0; Queue<Location> queue = new LinkedList<>(); queue.add(new Location(startX, startY)); visited[startX][startY] = true; while (!queue.isEmpty()) { Location location = queue.poll(); int pollX = location.x; int pollY = location.y; for (int i = 0; i < 8; i++) { int x = pollX + xMove[i]; int y = pollY + yMove[i]; if (x == endX && y == endY) return board[pollX][pollY] + 1; if (isLocation(x, y)) { queue.add(new Location(x, y)); visited[x][y] = true; board[x][y] = board[pollX][pollY] + 1; } } } return board[endX][endY]; } private static boolean isLocation(int x, int y) { if (x >= 0 && y >= 0 && x < l && y < l && !visited[x][y]) return true; return false; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < n; i++) { l = Integer.parseInt(bufferedReader.readLine()); // 체스판 한 변의 길이 board = new int[l][l]; visited = new boolean[l][l]; String[] input = bufferedReader.readLine().split(" "); startX = Integer.parseInt(input[0]); startY = Integer.parseInt(input[1]); input = bufferedReader.readLine().split(" "); endX = Integer.parseInt(input[0]); endY = Integer.parseInt(input[1]); int count = bfs(startX, startY); System.out.println(count); } bufferedReader.close(); } }
'문제 풀이 > Baekjoon Online Judge' 카테고리의 다른 글
[Baekjoon Online Judge] 2805번: 나무 자르기 (0) 2021.02.25 [Baekjoon Online Judge] 1920번: 수 찾기 (0) 2021.02.25 [Baekjoon Online Judge] 10026번: 적록색약 (0) 2021.02.25 [Baekjoon Online Judge] 7569번: 토마토 (0) 2021.02.22 [Baekjoon Online Judge] 2583번: 영역 구하기 (0) 2021.02.22