1
+ import java .util .*;
2
+
3
+ class DH_150366 {
4
+ static class Point {
5
+ int r , c ;
6
+ public Point (int r , int c ) {
7
+ this .r = r ;
8
+ this .c = c ;
9
+ }
10
+ }
11
+
12
+ static String [][] table ;
13
+ static int [][] markGroupNum ;
14
+ static HashMap <Integer , HashSet <Point >> hashMap ;
15
+
16
+ static ArrayList <String > solution (String [] commands ) {
17
+ hashMap = new HashMap <Integer , HashSet <Point >>();
18
+ ArrayList <String > answer = new ArrayList <>();
19
+ table = new String [51 ][51 ];
20
+ markGroupNum = new int [51 ][51 ];
21
+
22
+ for (int r = 1 ; r < 51 ; r ++) {
23
+ for (int c = 1 ; c < 51 ; c ++) {
24
+ int key = r * 51 + c ;
25
+ hashMap .put (key , new HashSet <>());
26
+ hashMap .get (key ).add (new Point (r , c ));
27
+ markGroupNum [r ][c ] = key ;
28
+ }
29
+ }
30
+
31
+ for (String command : commands ) {
32
+ String [] input = command .split (" " );
33
+ String order = input [0 ];
34
+
35
+ if (order .equals ("UPDATE" )) {
36
+ if (input .length == 4 ) {
37
+ int r = getPoint (input , 1 );
38
+ int c = getPoint (input , 2 );
39
+ String value = input [3 ];
40
+ update (r , c , value );
41
+ }
42
+ if (input .length == 3 ) {
43
+ String value1 = input [1 ];
44
+ String value2 = input [2 ];
45
+ update (value1 , value2 );
46
+ }
47
+ }
48
+ //
49
+ if (order .equals ("MERGE" )) {
50
+ int r1 = getPoint (input , 1 );
51
+ int c1 = getPoint (input , 2 );
52
+ int r2 = getPoint (input , 3 );
53
+ int c2 = getPoint (input , 4 );
54
+
55
+ merge (r1 , c1 , r2 , c2 );
56
+ }
57
+ if (order .equals ("UNMERGE" )) {
58
+ int r = getPoint (input , 1 );
59
+ int c = getPoint (input , 2 );
60
+ unmerge (r , c );
61
+ }
62
+ if (order .equals ("PRINT" )) {
63
+ int r = getPoint (input , 1 );
64
+ int c = getPoint (input , 2 );
65
+ answer .add (print (r , c ));
66
+ }
67
+ }
68
+
69
+ return answer ;
70
+ }
71
+
72
+ static String print (int r , int c ) {
73
+ return table [r ][c ] == null ? "EMPTY" : table [r ][c ];
74
+ }
75
+
76
+ static void unmerge (int r , int c ) {
77
+ int key = markGroupNum [r ][c ];
78
+
79
+ ArrayList <Point > removeSet = new ArrayList <Point >();
80
+ for (Point p : hashMap .get (key )) {
81
+ if (p .r == r && p .c == c ) continue ;
82
+
83
+ int nextKey = p .r * 51 + p .c ;
84
+ markGroupNum [p .r ][p .c ] = nextKey ;
85
+ table [p .r ][p .c ] = null ;
86
+ removeSet .add (p );
87
+ hashMap .get (nextKey ).add (p );
88
+ }
89
+
90
+ hashMap .get (key ).removeAll (removeSet );
91
+ }
92
+
93
+ static void merge (int r1 , int c1 , int r2 , int c2 ) {
94
+ if (r1 == r2 && c1 == c2 ) return ;
95
+
96
+ int key = markGroupNum [r1 ][c1 ];
97
+ String value = table [r1 ][c1 ];
98
+ int removeKey = markGroupNum [r2 ][c2 ];
99
+
100
+ if (key == removeKey ) return ;
101
+
102
+ if (value == null && table [r2 ][c2 ] != null ) {
103
+ key = removeKey ;
104
+ removeKey = markGroupNum [r1 ][c1 ];
105
+ value = table [r2 ][c2 ];
106
+ }
107
+
108
+ for (Point p : hashMap .get (removeKey )) {
109
+ table [p .r ][p .c ] = value ;
110
+ markGroupNum [p .r ][p .c ] = key ;
111
+ }
112
+
113
+ hashMap .get (key ).addAll (hashMap .get (removeKey ));
114
+ hashMap .get (removeKey ).clear ();
115
+ }
116
+
117
+ static void update (String value1 , String value2 ) {
118
+ for (int r = 1 ; r < table .length ; r ++) {
119
+ for (int c = 1 ; c < table [0 ].length ; c ++) {
120
+ if (table [r ][c ] == null || !table [r ][c ].equals (value1 )) continue ;
121
+
122
+ int key = markGroupNum [r ][c ];
123
+
124
+ for (Point p : hashMap .get (key )) table [p .r ][p .c ] = value2 ;
125
+ }
126
+ }
127
+ }
128
+
129
+ static void update (int r , int c , String value ) {
130
+ int key = markGroupNum [r ][c ];
131
+ for (Point p : hashMap .get (key )) table [p .r ][p .c ] = value ;
132
+ }
133
+
134
+ static int getPoint (String [] input , int idx ) {
135
+ return Integer .parseInt (input [idx ]);
136
+ }
137
+
138
+ public static void main (String [] args ) {
139
+ String [] commands = {"UPDATE 1 1 apple" , "MERGE 1 1 2 2" , "MERGE 2 2 3 3" , "UNMERGE 1 1" , "UNMERGE 2 2" , "PRINT 1 1" , "PRINT 2 2" , "PRINT 3 3" };
140
+ System .out .println (Arrays .toString (solution (commands ).toArray ()));
141
+ }
142
+ }
0 commit comments