-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.cpp
62 lines (62 loc) · 1.23 KB
/
2048.cpp
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
// GitHub: EntityPlantt/Kattis
#include <bits/stdc++.h>
using namespace std;
typedef unsigned short num;
int main() {
num **table = new num*[4], direction;
for (num i = 0; i < 4; i++) {
table[i] = new num[4];
for (num j = 0; j < 4; j++) {
cin >> table[i][j];
}
}
cin >> direction;
bool invert = direction % 2;
if (invert) {
direction--;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
swap(table[i][j], table[j][i]);
}
}
}
for (num i = 0; i < 4; i++) {
num *now = new num[4], nowptr = 0;
for (num j = 0; j < 4; j++) {
now[j] = 0;
}
if (direction == 2)
reverse(table[i], table[i] + 4);
for (num j = 0; j < 4; j++) {
if (table[i][j] > 0) {
now[nowptr++] = table[i][j];
}
}
for (num j = 0; j < 3; j++) {
if (now[j] == now[j + 1]) {
now[j] *= 2;
for (num k = j + 1; k < 3; k++) {
now[k] = now[k + 1];
}
now[3] = 0;
}
}
table[i] = now;
if (direction == 2)
reverse(table[i], table[i] + 4);
}
if (invert) {
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
swap(table[i][j], table[j][i]);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << table[i][j] << ' ';
}
cout << endl;
}
return 0;
}