-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_150366.java
More file actions
136 lines (116 loc) Β· 3.98 KB
/
JY_150366.java
File metadata and controls
136 lines (116 loc) Β· 3.98 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
import java.util.*;
class JY_150366 {
static final int N = 51;
static String[][] g;
static List<Integer>[] link;
static List<String> aList;
public String[] solution(String[] commands) {
aList = new ArrayList<>();
g = new String[N][N];
for(int i=0; i<N; i++) {
Arrays.fill(g[i], "");
}
int M = N*N + N;
link = new ArrayList[M];
for(int i=0; i<M; i++) {
link[i] = new ArrayList<>();
}
for(String c: commands) {
String[] crr = c.split(" ");
// 1) UPDATE r c value
if(crr[0].equals("UPDATE") && crr.length == 4) {
insert(Integer.parseInt(crr[1]), Integer.parseInt(crr[2]), crr[3]);
}
// 2) UPDATE value1, value2
else if (crr[0].equals("UPDATE") && crr.length == 3) {
change(crr[1], crr[2]);
}
// 3) MERGE r1 c1 r2 c2
else if(crr[0].equals("MERGE")) {
merge(Integer.parseInt(crr[1]),
Integer.parseInt(crr[2]),
Integer.parseInt(crr[3]),
Integer.parseInt(crr[4]));
}
// 4) UNMERGE r c
else if(crr[0].equals("UNMERGE")) {
unmerge(Integer.parseInt(crr[1]), Integer.parseInt(crr[2]));
}
// 5) PRINT r c
else if(crr[0].equals("PRINT")) {
print(Integer.parseInt(crr[1]), Integer.parseInt(crr[2]));
}
}
// System.out.println(aList);
String[] answer = new String[aList.size()];
for(int i=0; i<aList.size(); i++) {
answer[i] = aList.get(i);
}
return answer;
}
public static void printG() {
for(int i=0; i<N; i++) {
System.out.println(Arrays.toString(g[i]));
}
System.out.println();
}
public static void linking(int r, int c, String v, boolean isUnmerge) {
Queue<Integer> q = new LinkedList<>();
boolean[][] visited = new boolean[N][N];
q.add(r*N + c);
visited[r][c] = true;
while(!q.isEmpty()) {
int now = q.poll();
int x = now / N;
int y = now % N;
g[x][y] = v;
for(int next: link[now]) {
int nx = next / N;
int ny = next % N;
if(visited[nx][ny]) continue;
visited[nx][ny] = true;
q.add(next);
}
if(isUnmerge) link[now] = new ArrayList<>();
}
}
public static void insert(int r, int c, String v) {
g[r][c] = v;
if(link[r*N+c].isEmpty()) return;
linking(r, c, v, false);
}
public static void change(String v1, String v2) {
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
if(g[i][j].equals(v1)) g[i][j] = v2;
}
}
}
public static void merge(int r1, int c1, int r2, int c2) {
if(r1==r2 && c1 == c2) return;
// (r1, c1)μ΄ λΉμ΄μκ³ , (r2, c2)λ§ κ°μ κ°μ§κ³ μμ : (r2, c2)λ‘ λ³ν©
if(g[r1][c1].length() == 0 && g[r2][c2].length() != 0) {
g[r1][c1] = g[r2][c2];
linking(r1, c1, g[r1][c1], false);
}
// (r1, c1)λ‘ λ³ν©
else {
g[r2][c2] = g[r1][c1];
linking(r2, c2, g[r2][c2], false);
}
// λ§ν¬ μμ±
int n1 = r1*N + c1;
int n2 = r2*N + c2;
link[n1].add(n2);
link[n2].add(n1);
}
public static void unmerge(int r, int c) {
String value = g[r][c];
linking(r, c, "", true);
g[r][c] = value;
}
public static void print(int r, int c) {
if(g[r][c].length() == 0) aList.add("EMPTY");
else aList.add(g[r][c]);
}
}