프로그래머스 - [PCCP 기출문제] 3번 / 충돌위험 찾기[Java]

우노구나·2025년 6월 30일

문제설명

x,y 좌표평면에서 정해진 규칙대로 움직이는 로봇의 충돌 횟수를 구하는 문제였다.
사실 예전에 몇번 풀다가 포기한 문제였는데 오랜만에 다시 풀어보니깐 해결되었다.

좌표평면의 범위가 100이라서 시간복잡도는 크게 문제가 되지 않았고, 문제에 나와있는대로 구현만 하면 되는 문제였는데, 좀 구현할 것이 많고 코드가 길어짐에따라 에러도 이것저것 많이 생겨서 포기했었는데 이번에는 다행이 끝까지 풀 수 있었다.

class Solution {
    class Robot{
        int x;
        int y;
        
        int location_x;
        int location_y;
        
        int[] route;
        int route_idx;
        
        boolean end;
        
        Robot(int[] route, int x, int y, int location_x, int location_y){
            route_idx = 1;
            this.route = route;
            this.x = x;
            this.y = y;
            this.location_x = location_x;
            this.location_y = location_y;
            this.end = false;
        }
    }
    
    public int solution(int[][] points, int[][] routes) {
        int answer = 0;
        int end_num = 0;
        
        int[][] matrix = new int[101][101];
        
        Robot[] robot = new Robot[routes.length];
        
        for(int i=0; i<routes.length; i++){
            int point_idx = routes[i][0] - 1;
            int location_idx = routes[i][1] - 1; 
            robot[i] = new Robot(routes[i], points[point_idx][1], points[point_idx][0],
                                 points[location_idx][1], points[location_idx][0]);
        }
        
        while(end_num < routes.length){
            //현재 반영
            for(int i=0; i<robot.length; i++){
                if(robot[i].end) continue;
                
                int x = robot[i].x;
                int y = robot[i].y;
                
                matrix[y][x]++;
            }
            
            //count 체크
            for(int i=0; i<matrix.length; i++){
                for(int j=0; j<matrix.length; j++){
                    if(matrix[j][i] > 1) answer++;
                    matrix[j][i] = 0;
                }
            }

            //다음 동작
            for(int i=0; i<robot.length; i++){
                if(robot[i].end) continue; //동작을 다 한 로봇
                
                if(robot[i].x == robot[i].location_x && robot[i].y == robot[i].location_y){
                    if(robot[i].route_idx + 1 == robot[i].route.length){
                        end_num++;
                        robot[i].end=true;
                        continue;
                    }
                    
                    //다음 location할당
                    robot[i].route_idx++;
                    int idx = robot[i].route[robot[i].route_idx];
                       
                    robot[i].location_y = points[idx-1][0];
                    robot[i].location_x = points[idx-1][1];
                }
                if(robot[i].y != robot[i].location_y){
                    //세로 움직
                    robot[i].y += robot[i].y > robot[i].location_y ? -1 : 1;
                }
                else{
                    //가로 움직
                    robot[i].x += robot[i].x > robot[i].location_x ? -1 : 1;
                }
            }
        }
         
        return answer;
    }
}

배울점

다른 사람들이 푼 코드를 보면 나처럼 class를 선언하여 풀지 않은 코드가 대부분이었다. class를 좀 복잡하게 선언하여 시행착오를 많이 겪은 것 같기도 하다. 다른 사람들의 코드를 통해서 조금 더 간단하게 구현할 방법을 찾아야 할 것 같다.

profile
기술 블로그

0개의 댓글