-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageEncoding.java
175 lines (132 loc) · 4.19 KB
/
ImageEncoding.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class ImageEncoding
{
private static long max(long a, long b) {
return a > b ? a : b;
}
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
String s = in.nextLine();
int white = s.indexOf(' ');
if (white >= 0) {
secondType(in, Integer.parseInt( s.substring(0,white) ), Integer.parseInt(s.substring(white+1, s.length())));
}
else {
firstType(in, Integer.parseInt(s));
}
}
public static void secondType( Scanner in, int x, int y) {
SortedSet<Pixel> pixels = new TreeSet<>((a,b) -> a.x < b.x ? -1 : ( a.x > b.x ? 1 : ( a.y < b.y ? -1 : ( a.y > b.y ? 1 : 0) ) ) );
Queue<Pixel> q = new LinkedList<>();
q.add(new Pixel(x,y));
while(q.size() > 0) {
Pixel p = q.poll();
pixels.add(p);
String line = in.nextLine();
for(int i =0;i < line.length(); i++) {
char c = line.charAt(i);
if (c == 'R') {
q.add( new Pixel(p.x+1,p.y) );
}
else if (c == 'L') {
q.add( new Pixel(p.x-1,p.y) );
}
else if (c == 'T') {
q.add( new Pixel(p.x,p.y+1) );
}
else if (c == 'B') {
q.add( new Pixel(p.x,p.y-1) );
}
}
}
System.out.println(pixels.size());
for (Pixel p : pixels) {
System.out.println(p.x+" "+p.y);
}
}
public static void firstType( Scanner in, int k) {
Set<Pixel> pixels = new HashSet<>();
if (k==0) {
System.out.println(".");
return;
}
Pixel first = null;
while (k-- > 0) {
int x = in.nextInt();
int y = in.nextInt();
Pixel p = new Pixel(x,y);
pixels.add(p);
if (first==null) {
first=p;
}
}
Queue<Pixel> q = new LinkedList<>();
q.add(first);
pixels.remove(first);
System.out.println(first.x+" "+first.y);
while(q.size() > 0) {
Pixel p = q.poll();
if (pixels.contains(p.right())) {
System.out.print("R");
q.add(p.right());
pixels.remove(p.right());
}
if (pixels.contains(p.top())) {
System.out.print("T");
q.add(p.top());
pixels.remove(p.top());
}
if (pixels.contains(p.left())) {
System.out.print("L");
q.add(p.left());
pixels.remove(p.left());
}
if (pixels.contains(p.bottom())) {
System.out.print("B");
q.add(p.bottom());
pixels.remove(p.bottom());
}
if (q.size() > 0) {
System.out.println(",");
}
else {
System.out.println(".");
}
}
}
public static class Pixel {
int x;
int y;
@Override
public int hashCode() {
return x * 31 + y;
}
@Override
public String toString() {
return "<"+x+","+y+">";
}
public Pixel(int x, int y) {
this.x=x;
this.y=y;
}
@Override
public boolean equals (Object o) {
return x == ((Pixel)o).x && y == ((Pixel)o).y;
}
public Pixel top() {
return new Pixel(x,y+1);
}
public Pixel bottom() {
return new Pixel(x,y-1);
}
public Pixel left() {
return new Pixel(x-1,y);
}
public Pixel right() {
return new Pixel(x+1,y);
}
}
}