Algorithm
[Programmers] 리코쳇 로봇 (JAVA)
KMSEOP
2023. 9. 3. 00:17
728x90
문제 설명
리코쳇 로봇이라는 보드게임이 있습니다.
이 보드게임은 격자모양 게임판 위에서 말을 움직이는 게임으로, 시작 위치에서 목표 위치까지 최소 몇 번만에 도달할 수 있는지 말하는 게임입니다.
이 게임에서 말의 움직임은 상, 하, 좌, 우 4방향 중 하나를 선택해서 게임판 위의 장애물이나 맨 끝에 부딪힐 때까지 미끄러져 이동하는 것을 한 번의 이동으로 칩니다.
다음은 보드게임판을 나타낸 예시입니다.
...D..R
.D.G...
....D.D
D....D.
..D....
여기서 "."은 빈 공간을, "R"은 로봇의 처음 위치를, "D"는 장애물의 위치를, "G"는 목표지점을 나타냅니다.
위 예시에서는 "R" 위치에서 아래, 왼쪽, 위, 왼쪽, 아래, 오른쪽, 위 순서로 움직이면 7번 만에 "G" 위치에 멈춰 설 수 있으며, 이것이 최소 움직임 중 하나입니다.
게임판의 상태를 나타내는 문자열 배열 board가 주어졌을 때, 말이 목표위치에 도달하는데 최소 몇 번 이동해야 하는지 return 하는 solution함수를 완성하세요. 만약 목표위치에 도달할 수 없다면 -1을 return 해주세요.
제한 사항
- 3 ≤ board의 길이 ≤ 100
- 3 ≤ board의 원소의 길이 ≤ 100
- board의 원소의 길이는 모두 동일합니다.
- 문자열은 ".", "D", "R", "G"로만 구성되어 있으며 각각 빈 공간, 장애물, 로봇의 처음 위치, 목표 지점을 나타냅니다.
- "R"과 "G"는 한 번씩 등장합니다.
Solution
import java.util.LinkedList;
import java.util.Queue;
import java.util.stream.IntStream;
public class 리코쳇로봇 {
public int solution(String[] board) {
Position startPosition = null;
Position endPosition = null;
char[][] graph = new char[board.length][board[0].length()];
for (int y : IntStream.range(0, board.length).toArray()) {
for (int x : IntStream.range(0, board[0].length()).toArray()) {
char currentChar = board[y].charAt(x);
graph[y][x] = currentChar;
if (currentChar == 'R') {
startPosition = new Position(x, y, 0);
} else if (currentChar == 'G') {
endPosition = new Position(x, y, 0);
}
}
}
return bfs(graph, startPosition, endPosition);
}
private int bfs(char[][] graph, Position startPosition, Position endPosition) {
int[] xD = { -1, 0, 1, 0 };
int[] yD = { 0, 1, 0, -1 };
int maxHeight = graph.length;
int maxWidth = graph[0].length;
boolean[][] visitedPosition = new boolean[maxHeight][maxWidth];
Queue<Position> queue = new LinkedList<Position>();
queue.add(startPosition);
visitedPosition[startPosition.y][startPosition.x] = true;
while (!queue.isEmpty()) {
Position currentPosition = queue.poll();
int x = currentPosition.x;
int y = currentPosition.y;
if (x == endPosition.x && y == endPosition.y) {
return currentPosition.count;
}
for (int i : IntStream.range(0, xD.length).toArray()) {
int currentX = x;
int currentY = y;
int nextX = currentX + xD[i];
int nextY = currentY + yD[i];
while (nextX >= 0 && nextX < maxWidth && nextY >= 0 && nextY < maxHeight && graph[nextY][nextX] != 'D') {
currentX += xD[i];
currentY += yD[i];
nextX += xD[i];
nextY += yD[i];
}
if (!visitedPosition[currentY][currentX]) {
visitedPosition[currentY][currentX] = true;
queue.add(new Position(currentX, currentY, currentPosition.count + 1));
}
}
}
return -1;
}
static class Position {
public int x;
public int y;
public int count;
Position (int x, int y, int count) {
this.x = x;
this.y = y;
this.count = count;
}
}
}
728x90