728x90
반응형
SMALL

 

문제 이해

  • 리코쳇 로봇이라는 보드게임이 있습니다.
  • 게임판에는 "." - 빈공간, "R" - 로봇, "D" - 장애물, "G" - 목표지점 으로 구성되어있고 "R", "G는 무조건 하나씩 존재합니다.
  • 로봇이 목표지점으로 갈 수 있는 최소의 경로를 찾아야합니다.
  • 로봇은 상하좌우로 움직일수 있으며 한번 움직일때 미끄러져서 맨끝까지 가거나 장애물을 만나기전까지 멈추지 않습니다.
  • 로봇이 목표지점으로 갈 수 없다면 -1로 return 해주세요
  • 로봇이 상하좌우 움직일때마다 맨끝까지 or 장애물에 멈추게 move 함수를 만든다.
  • 0 - 상 , 1 - 하, 2 - 좌, 3 - 우 로 판단하여 만들었다.
  • 움직일 때마다 이동 count + 1 하고 방문했던적이 없다면 큐에 집어 넣고 아니라면 방향을 바꿔가며 이동한다.
  • 이동한 좌표가 "G" 목표지점이라면 answer 에 최소값을 비교하면서 집어 넣는다.
  • answer 값이 처음과 같다면 목표지점에 못갔다는 뜻으로 -1 아니라면 최소값으로 삼항식을 넣어준다.
import java.util.*;
class Solution {
    public int solution(String[] board) {
        int answer = Integer.MAX_VALUE;
        boolean[][] visit = new boolean[board.length][board[0].length()];
        Queue<int[]> que = new LinkedList<>();
        
        for(int i = 0; i<board.length; i++){
            for(int j = 0; j<board[i].length(); j++){
                if(board[i].charAt(j) == 'R') {
                    que.add(new int[] {i,j,0});
                    visit[i][j] = true;
                    while(!que.isEmpty()){
                        int[] tmp = que.poll();
                        
                        if(board[tmp[0]].charAt(tmp[1]) == 'G') {
                            answer = Math.min(answer, tmp[2]);
                        }
                        
                        for(int k = 0; k<4; k++){
                            int[] XY = move(k, tmp[0] , tmp[1], tmp[2], board);
                            int x = XY[0];
                            int y = XY[1];
                            
                            if(!visit[x][y]){
                                que.add(XY);
                                visit[x][y] = true;
                            }
                        }
                    }
                }
            }
        }
        
        return answer == Integer.MAX_VALUE ? -1 : answer;
    }
    
    // 상하좌우 장애물이나 맨 끝에 부딪힐때까지 미끄러지기
    public int[] move(int k, int x, int y, int cnt, String[] board){
        int[] result = new int[3];
        if(k == 0){
            for(int i = x; i>=0; i--){
                if(board[i].charAt(y) == 'D'){
                    return new int[]{i+1, y, cnt+1};
                }
            }            
            result[0] = 0;
            result[1] = y;
            result[2] = cnt+1;
        }else if(k == 1){
            for(int i = x; i<board.length; i++){
                if(board[i].charAt(y) == 'D'){
                    return new int[]{i-1, y, cnt+1};
                }
            }            
            result[0] = board.length - 1;
            result[1] = y;
            result[2] = cnt+1;
        }else if(k == 2){
            for(int i = y; i<board[0].length(); i++){
                if(board[x].charAt(i) == 'D'){
                    return new int[]{x, i-1, cnt+1};
                }
            }            
            result[0] = x;
            result[1] = board[0].length()-1;
            result[2] = cnt+1;
        }else if(k == 3){
            for(int i = y; i>=0; i--){
                if(board[x].charAt(i) == 'D'){
                    return new int[]{x, i+1, cnt+1};
                }
            }            
            result[0] = x;
            result[1] = 0;
            result[2] = cnt+1;
        }
        
        return result;
    }
}
728x90
반응형
LIST