Skip to content

Commit a9fdbbe

Browse files
committed
[김정호]꽉 자바 첫 주 코테 풀이 프로그래머스 340211번 문제
1 parent 9ba80a7 commit a9fdbbe

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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;
17+
int y;
18+
19+
int location_x;
20+
int location_y;
21+
22+
int[] route;
23+
int route_idx;
24+
25+
boolean end;
26+
27+
Robot(int[] route, int x, int y, int location_x, int location_y){
28+
route_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+
public int solution(int[][] points, int[][] routes) {
39+
int answer = 0;
40+
int end_num = 0;
41+
42+
int[][] matrix = new int[101][101];
43+
44+
Robot[] robot = new Robot[routes.length];
45+
46+
for(int i=0; i<routes.length; i++){
47+
int point_idx = routes[i][0] - 1;
48+
int location_idx = routes[i][1] - 1;
49+
robot[i] = new Robot(routes[i], points[point_idx][1], points[point_idx][0],
50+
points[location_idx][1], points[location_idx][0]);
51+
}
52+
53+
while(end_num < routes.length){
54+
//현재 반영
55+
for(int i=0; i<robot.length; i++){
56+
if(robot[i].end) continue;
57+
58+
int x = robot[i].x;
59+
int y = robot[i].y;
60+
61+
matrix[y][x]++;
62+
}
63+
64+
//count 체크
65+
for(int i=0; i<matrix.length; i++){
66+
for(int j=0; j<matrix.length; j++){
67+
if(matrix[j][i] > 1) answer++;
68+
matrix[j][i] = 0;
69+
}
70+
}
71+
72+
//다음 동작
73+
for(int i=0; i<robot.length; i++){
74+
if(robot[i].end) continue; //동작을 다 한 로봇
75+
76+
if(robot[i].x == robot[i].location_x && robot[i].y == robot[i].location_y){
77+
if(robot[i].route_idx + 1 == robot[i].route.length){
78+
end_num++;
79+
robot[i].end=true;
80+
continue;
81+
}
82+
83+
//다음 location할당
84+
robot[i].route_idx++;
85+
int idx = robot[i].route[robot[i].route_idx];
86+
87+
88+
robot[i].location_y = points[idx-1][0];
89+
robot[i].location_x = points[idx-1][1];
90+
}
91+
if(robot[i].y != robot[i].location_y){
92+
//세로 움직
93+
robot[i].y += robot[i].y > robot[i].location_y ? -1 : 1;
94+
}
95+
else{
96+
//가로 움직
97+
robot[i].x += robot[i].x > robot[i].location_x ? -1 : 1;
98+
}
99+
}
100+
}
101+
102+
return answer;
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)