-
Notifications
You must be signed in to change notification settings - Fork 34
/
B.java
113 lines (108 loc) · 3.61 KB
/
B.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
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* APAC 2015 Round A Problem B: Super 2048
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
int n=scanner.nextInt();
String dir=scanner.next();
int[][] nums=new int[n][n];
for (int i=0;i<n;i++) for (int j=0;j<n;j++) nums[i][j]=scanner.nextInt();
int[][] ans=new int[n][n];
if ("left".equals(dir)) {
for (int i=0;i<n;i++) {
int curr=0;
for (int j=0;j<n;j++) {
if (nums[i][j]==0) continue;
if (ans[i][curr]==0) {
ans[i][curr]=nums[i][j];
}
else if (ans[i][curr]==nums[i][j]) {
ans[i][curr]*=2;
curr++;
}
else {
curr++;
j--;
}
}
}
}
if ("right".equals(dir)) {
for (int i=0;i<n;i++) {
int curr=n-1;
for (int j=n-1;j>=0;j--) {
if (nums[i][j]==0) continue;
if (ans[i][curr]==0) {
ans[i][curr]=nums[i][j];
}
else if (ans[i][curr]==nums[i][j]) {
ans[i][curr]*=2;
curr--;
}
else {
curr--;
j++;
}
}
}
}
if ("up".equals(dir)) {
for (int j=0;j<n;j++) {
int curr=0;
for (int i=0;i<n;i++) {
if (nums[i][j]==0) continue;
if (ans[curr][j]==0) {
ans[curr][j]=nums[i][j];
}
else if (ans[curr][j]==nums[i][j]) {
ans[curr][j]*=2;
curr++;
}
else {
curr++;
i--;
}
}
}
}
if ("down".equals(dir)) {
for (int j=0;j<n;j++) {
int curr=n-1;
for (int i=n-1;i>=0;i--) {
if (nums[i][j]==0) continue;
if (ans[curr][j]==0) {
ans[curr][j]=nums[i][j];
}
else if (ans[curr][j]==nums[i][j]) {
ans[curr][j]*=2;
curr--;
}
else {
curr--;
i++;
}
}
}
}
return "\n"+String.join("\n", Arrays.stream(ans)
.map(x->String.join(" ",
Arrays.stream(x).boxed().map(String::valueOf).collect(Collectors.toList())))
.collect(Collectors.toList()));
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}