-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_150366.java
More file actions
128 lines (120 loc) ยท 4.4 KB
/
JW_150366.java
File metadata and controls
128 lines (120 loc) ยท 4.4 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
import java.util.ArrayList;
import java.util.Arrays;
class JW_150366 {
String[][] cells = new String[51][51];
int[][][] parent = new int[51][51][2];
public String[] solution(String[] commands) {
// ๋น ๋ฌธ์์ด๋ก ์ด๊ธฐํ
for (int i = 0; i < 51; i++)
Arrays.fill(cells[i], "EMPTY");
for (int i = 1; i < 51; i++)
for (int j = 1; j < 51; j++)
// ์๊ธฐ ์์ ์ ๋ถ๋ชจ๋ก ๊ฐ๋๋ก ์ด๊ธฐํ
parent[i][j] = new int[] { i, j };
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < commands.length; i++) {
String[] command = commands[i].split(" ");
int r1, c1, r2, c2;
String value1, value2;
switch (command[0]) {
case "UPDATE":
if (command.length == 4) {
r1 = Integer.parseInt(command[1]);
c1 = Integer.parseInt(command[2]);
value1 = command[3];
updateCell(r1, c1, value1);
} else {
value1 = command[1];
value2 = command[2];
updateArea(value1, value2);
}
break;
case "MERGE":
r1 = Integer.parseInt(command[1]);
c1 = Integer.parseInt(command[2]);
r2 = Integer.parseInt(command[3]);
c2 = Integer.parseInt(command[4]);
merge(r1, c1, r2, c2);
break;
case "UNMERGE":
r1 = Integer.parseInt(command[1]);
c1 = Integer.parseInt(command[2]);
unmerge(r1, c1);
break;
case "PRINT":
r1 = Integer.parseInt(command[1]);
c1 = Integer.parseInt(command[2]);
result.add(print(r1, c1));
break;
}
}
// ๋ฆฌ์คํธ๋ฅผ ๋ฐฐ์ด๋ก ๋ณ๊ฒฝ ํ ๋ฐํ
return result.toArray(new String[0]);
}
private void updateCell(int r, int c, String value) {
// ๋ฐ๊พธ๋ ค๋ ์์น์ ๊ทธ๋ฃน์ ๋ถ๋ชจ
int[] rootX = find(r, c);
for (int i = 1; i < 51; i++)
for (int j = 1; j < 51; j++) {
int[] rootY = find(i, j);
// ๊ฐ์ ๊ทธ๋ฃน์ด๋ผ๋ฉด ๊ฐ ๋ณ๊ฒฝ
if (rootX[0] == rootY[0] && rootX[1] == rootY[1]) {
cells[i][j] = value;
}
}
}
private void updateArea(String value1, String value2) {
for (int i = 1; i < 51; i++)
for (int j = 1; j < 51; j++)
// ๋์ผํ ๊ฐ์ ๊ฐ์ง๋ ๋ชจ๋ ๊ฐ ๋ณ๊ฒฝ
if (cells[i][j].equals(value1))
cells[i][j] = value2;
}
private void merge(int r1, int c1, int r2, int c2) {
if (r1 == r2 && c1 == c2) {
return;
}
String value = cells[r1][c1];
if (value.equals("EMPTY")) {
value = cells[r2][c2];
}
union(r1, c1, r2, c2); // ๊ทธ๋ฃน ํฉ์น๊ธฐ
updateCell(r1, c1, value); // ํด๋น ์
๊ณผ ๊ฐ์ ๊ทธ๋ฃน์ธ ์
๋ค ๊ฐ ๋ณ๊ฒฝ
}
private void unmerge(int r, int c) {
// merge๋์ด์๋ ๊ทธ๋ฃน์ ๋ถ๋ชจ
int[] rootX = find(r, c);
// ํด๋น ๊ทธ๋ฃน์ ๊ฐ
String value = cells[rootX[0]][rootX[1]];
for (int i = 1; i < 51; i++)
for (int j = 1; j < 51; j++) {
int[] rootY = find(i, j);
// ๊ฐ์ ๊ทธ๋ฃน์ด๋ผ๋ฉด
if (rootX[0] == rootY[0] && rootX[1] == rootY[1]) {
// union๋์ด์๋ ๊ด๊ณ ํ์ด์ฃผ๊ณ ๋น ๋ฌธ์์ด ์ฝ์
parent[i][j] = new int[] { i, j };
cells[i][j] = "EMPTY";
}
}
// ๋ณํฉ์ ์์ํ ๋ถ๋ถ์ ๊ฐ์ ๊ธฐ์กด์ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
cells[r][c] = value;
}
// ํด๋น ์
๋ฐํ
private String print(int r, int c) {
return cells[r][c];
}
// find ์ฐ์ฐ
private int[] find(int r, int c) {
int pR = parent[r][c][0];
int pC = parent[r][c][1];
if (r != pR || c != pC)
parent[r][c] = find(pR, pC);
return parent[r][c];
}
// union ์ฐ์ฐ
private void union(int r1, int c1, int r2, int c2) {
int[] rootX = find(r1, c1);
int[] rootY = find(r2, c2);
parent[rootX[0]][rootX[1]] = rootY;
}
}