-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_340211.java
More file actions
150 lines (135 loc) ยท 4.45 KB
/
JY_340211.java
File metadata and controls
150 lines (135 loc) ยท 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.*;
class Solution {
static final int N = 100;
static int X, M;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static Robot[] robots;
static boolean[] isDied;
static int[][] g;
static class Robot {
int x, y, ex, ey;
int sp, rCnt;
// sp: ํ์ฌ ์งํ์ค์ธ ํฌ์ธํฐ
// rCnt: ์งํํ ๋ฃจํธ ์นด์ดํฐ (rCnt == M์ด๋ฉด ๋ชจ๋ ๋ฃจํธ๋ฅผ ํ์ํจ)
public Robot(int x, int y, int ex, int ey, int sp, int rCnt) {
this.x = x;
this.y = y;
this.ex = ex;
this.ey = ey;
this.sp = sp;
this.rCnt = rCnt;
}
@Override
public String toString() {
return "R: ("+this.x+","+this.y+") -> "+"("+this.ex+","+this.ey+") sp:"+this.sp+" rCnt:"+this.rCnt;
}
}
public int solution(int[][] points, int[][] routes) {
int answer = 0;
// ๋ก๋ด ์ด๊ธฐํ
X = routes.length;
M = routes[0].length;
robots = new Robot[X+1];
for(int i=0; i<X; i++) {
int[] r = routes[i];
int sp = r[0];
int np = r[1];
int sx = points[sp-1][0];
int sy = points[sp-1][1];
int ex = points[np-1][0];
int ey = points[np-1][1];
robots[i+1] = new Robot(sx, sy, ex, ey, sp, 1);
}
isDied = new boolean[X+1];
// -- while๋ฌธ
while(true) {
// ๋ก๋ด๋ค ์๋ฃ ์ฌ๋ถ ์ฒดํฌ
if(isDone()) break;
// ์ถฉ๋ ์ํ ์ฒดํฌํ๊ธฐ
answer += check();
// ์ด๋ํ๊ธฐ
move(points, routes);
}
return answer;
}
public static int calDist(int sx, int sy, int ex, int ey) {
return Math.abs(sx-ex) + Math.abs(sy-ey);
}
public static void move(int[][] points, int[][] routes) {
for(int i=1; i<X+1; i++) {
Robot now = robots[i];
if(isDied[i]) continue; // ๋ชจ๋ ๋ฃจํธ ํ์ ๋๋ ๋ก๋ด
int p = now.sp;
// ๋ชฉ์ ์ง๋ฅผ ๊ฐฑ์ ๋ฐ ํ์ฌ ํฌ์ธํฐ ๋ณ๊ฒฝ
if(now.x == now.ex && now.y == now.ey) {
if(now.rCnt == M) {
isDied[i] = true;
continue;
}
int nextP = routes[i-1][now.rCnt];
int tx = points[nextP-1][0];
int ty = points[nextP-1][1];
now.ex = tx;
now.ey = ty;
now.sp = nextP;
}
// int minDist = calDist(now.x, now.y, now.ex, now.ey);
int minDist = Integer.MAX_VALUE;
int nextX = -1;
int nextY = -1;
for(int d=0; d<4; d++) {
int nx = now.x + dx[d];
int ny = now.y + dy[d];
if(!inRange(nx, ny)) continue;
int nDist = calDist(nx, ny, now.ex, now.ey);
if(minDist > nDist) {
minDist = nDist;
nextX = nx;
nextY = ny;
}
}
// ์ด๋
now.x = nextX;
now.y = nextY;
// ๋ง์ฝ ๋ชฉ์ ์ง์ ๋์ฐฉํ๋ค๋ฉด ๋ค์ ๋ชฉ์ ์ง ๊ฐฑ์ ์ํค ์นด์ดํธ ์ฆ๊ฐ
if(now.x == now.ex && now.y == now.ey) now.rCnt++;
}
}
public static boolean inRange(int x, int y) {
return x>0 && x<=N && y>0 && y<=N;
}
public static int check() {
g = new int[N+1][N+1];
for(int i=1; i<X+1; i++) {
Robot now = robots[i];
if(isDied[i]) continue; // ๋ชจ๋ ๋ฃจํธ ํ์ ๋๋ ๋ก๋ด
g[now.x][now.y] += 1;
}
// printG();
int cnt = 0;
for(int i=0; i<N+1; i++) {
for(int j=0; j<N+1; j++) {
if(g[i][j] > 1) cnt++;
}
}
return cnt;
}
public static boolean isDone() {
for(int i=1; i<X+1; i++) {
if(!isDied[i]) return false;
}
return true;
}
public static void printR() {
for(int i=1; i<X+1; i++) {
System.out.println(robots[i]);
}
}
public static void printG() {
for(int i=0; i<N+1; i++) {
System.out.println(Arrays.toString(g[i]));
}
System.out.println();
}
}