Skip to content

Commit ad70841

Browse files
authored
[김정호]꽉 자바 첫 주 코테 풀이 프로그래머스 340211번 문제 (#5)
* [김정호]꽉 자바 첫 주 코테 풀이 프로그래머스 340211번 문제 * [김정호]꽉 자바 첫 주 코테 풀이 프로그래머스 340211번 문제
1 parent 9ba80a7 commit ad70841

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

.idea/.name

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/week01/Unoguna/PG_340211.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package week01.Unoguna;
2+
3+
public class PG_340211 {
4+
5+
public static void main(String[] args){
6+
int[][] points = new int[][]{{3, 2}, {6, 4}, {4, 7}, {1, 4}};
7+
int[][] routes = new int[][]{{4, 2}, {1, 3}, {2, 4}};
8+
9+
Solution s = new Solution();
10+
11+
System.out.println(s.solution(points, routes));
12+
}
13+
14+
public static class Solution {
15+
class Robot{
16+
int x; //로봇의 현재 좌표x
17+
int y; //로봇의 현재 좌표y
18+
19+
int location_x; //로봇의 현재 목적지의 좌표x
20+
int location_y; //로봇의 현재 목적지의 좌표y
21+
22+
int[] route; //로봇의 이동경로
23+
int route_idx; //현재 가야하는 route의 idx
24+
25+
boolean end; //최종 목적지에 도착하면 true 아니면 false
26+
27+
Robot(int[] route, int x, int y, int location_x, int location_y){
28+
route_idx = 1; //0은 시작지점이기 때문에 첫 목적지의 idx는 1
29+
this.route = route;
30+
this.x = x;
31+
this.y = y;
32+
this.location_x = location_x;
33+
this.location_y = location_y;
34+
this.end = false;
35+
}
36+
}
37+
38+
//points는 특정 지점의 위치를 담고 있다.
39+
//routes는 각 로봇들의 이동경로를 담고 있다.
40+
public int solution(int[][] points, int[][] routes) {
41+
int answer = 0;
42+
int end_num = 0; //최종 목적지까지 도착이 완료된 로봇의 개수
43+
44+
int[][] matrix = new int[101][101]; //해당 위치의 로봇의 개수를 표시하기위한 배열 생성 전체 가로, 세로의 최대 길이가 100이라 101로 할당
45+
46+
Robot[] robot = new Robot[routes.length];
47+
48+
//로봇들을 생성
49+
for(int i=0; i<routes.length; i++){
50+
int point_idx = routes[i][0] - 1; //문제에서 첫 idx를 0이 아닌 1로 주어서 1을 뺌
51+
int location_idx = routes[i][1] - 1;
52+
robot[i] = new Robot(routes[i], points[point_idx][1], points[point_idx][0],
53+
points[location_idx][1], points[location_idx][0]);
54+
}
55+
56+
//로봇 이동 및 충돌횟수 체크
57+
while(end_num < routes.length){ //모든 로봇이 최종 목적지에 도착하면 종료
58+
//현재 로봇들의 위치를 matrix에 반영 matrix는 (x,y)좌표에 있는 로봇의 개수를 갖게됨
59+
for(int i=0; i<robot.length; i++){
60+
if(robot[i].end) continue; //최종 목적지까지 도착한 로봇은 계속 그 위치에 있는게 아니라 제거해야하기 때문에 도착한 경우 matrix에 반영하지 않는다.
61+
62+
int x = robot[i].x;
63+
int y = robot[i].y;
64+
65+
matrix[y][x]++;
66+
}
67+
68+
//count 체크
69+
//matrix의 크기가 최대 100*100이고 while문의 최대 loop횟수가 200이라 시간복잡도에 크게 지장이 없다고 생각하여 이중 for문으로 구현
70+
for(int i=0; i<matrix.length; i++){
71+
for(int j=0; j<matrix.length; j++){
72+
if(matrix[j][i] > 1) answer++; //해당위치에 로봇의 개수가 2개이상일 경우 충돌이 발생
73+
matrix[j][i] = 0; //해당위치 확인후 초기화 시켜준다.
74+
}
75+
}
76+
77+
//로봇들을 다음 이동할 위치로 이동
78+
for(int i=0; i<robot.length; i++){
79+
if(robot[i].end) continue; //최종목적지까지 도착한 로봇
80+
81+
//로봇이 현재 목적지에 도착한 경우
82+
if(robot[i].x == robot[i].location_x && robot[i].y == robot[i].location_y){
83+
84+
//현재 목적지가 최종 목적지인 경우
85+
if(robot[i].route_idx + 1 == robot[i].route.length){
86+
end_num++;
87+
robot[i].end=true;
88+
continue;
89+
}
90+
91+
//다음 목적지가 존재할 경우 다음 목적지 할당
92+
robot[i].route_idx++;
93+
int idx = robot[i].route[robot[i].route_idx];
94+
robot[i].location_y = points[idx-1][0];
95+
robot[i].location_x = points[idx-1][1];
96+
}
97+
98+
//정해진 규칙(세로방향 우선 이동)대로 이동
99+
if(robot[i].y != robot[i].location_y){
100+
//세로 이동
101+
robot[i].y += robot[i].y > robot[i].location_y ? -1 : 1;
102+
}
103+
else{
104+
//가로 이동
105+
robot[i].x += robot[i].x > robot[i].location_x ? -1 : 1;
106+
}
107+
}
108+
}
109+
110+
return answer;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)