230922 리코쳇 로봇

Jongleee·2023년 9월 22일
0

TIL

목록 보기
371/737
private static class Location {
	final int x;
	final int y;
	final int depth;

	public Location(int x, int y, int depth) {
		this.x = x;
		this.y = y;
		this.depth = depth;
	}
}

private static final int[] dx = { -1, 1, 0, 0 };
private static final int[] dy = { 0, 0, -1, 1 };

private int n;
private int m;

public int solution(String[] board) {
	initializeDimensions(board);

	Location robot = findLocation(board, 'R');
	Location goal = findLocation(board, 'G');

	return bfs(board, robot, goal);
}

private void initializeDimensions(String[] board) {
	n = board.length;
	m = board[0].length();
}

private Location findLocation(String[] board, char target) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			char ch = board[i].charAt(j);

			if (ch == target) {
				return new Location(i, j, 0);
			}
		}
	}
	return null;
}

private int bfs(String[] board, Location robot, Location goal) {
	Queue<Location> queue = new LinkedList<>();
	queue.add(robot);

	boolean[][] visited = new boolean[n][m];
	visited[robot.x][robot.y] = true;

	while (!queue.isEmpty()) {
		Location current = queue.poll();

		if (current.x == goal.x && current.y == goal.y) {
			return current.depth;
		}

		for (int i = 0; i < 4; i++) {
			int nx = current.x;
			int ny = current.y;

			while (inBounds(nx, ny) && board[nx].charAt(ny) != 'D') {
				nx += dx[i];
				ny += dy[i];
			}

			nx -= dx[i];
			ny -= dy[i];

			if (visited[nx][ny]) {
				continue;
			}

			visited[nx][ny] = true;
			queue.add(new Location(nx, ny, current.depth + 1));
		}
	}

	return -1;
}

private boolean inBounds(int x, int y) {
	return x >= 0 && y >= 0 && x < n && y < m;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/169199

0개의 댓글