fork download
  1. #include <bits/stdc++.h>
  2. // #include "solution.h"
  3. using namespace std;
  4.  
  5. #define rep(i, j) for (int i = 0; i < 4; i++)\
  6. for (int j = 0; j < 4; j++)
  7.  
  8. typedef array<array<int, 4>, 4> board;
  9.  
  10. board flip(board a, bool z = 1) {
  11. board b;
  12. rep(i, j) {
  13. if (z) b[i][j] = a[j][i];
  14. else b[i][j] = a[3 - j][3 - i];
  15. }
  16. return b;
  17. }
  18.  
  19.  
  20. board dir(board a, char c) {
  21. if (c == 'U') return a;
  22. if (c == 'L') return flip(a);
  23. if (c == 'R') return flip(a, 0);
  24. return flip(flip(a, 0));
  25. }
  26.  
  27. board move(board a, char c) {
  28. a = dir(a, c);
  29. rep(j, i) {
  30. if (a[i][j] == 0) continue;
  31. for (int k = i + 1; k < 4; k++) {
  32. if (a[k][j] == 0) continue;
  33. if (a[i][j] == a[k][j]) {
  34. a[i][j] *= -2;
  35. a[k][j] = 0;
  36. }
  37. break;
  38. }
  39. }
  40. rep(i, j) if (a[i][j] < 0) a[i][j] = -a[i][j];
  41. rep(j, i) {
  42. if (a[i][j] > 0) continue;
  43. for (int k = i + 1; k < 4; k++) {
  44. if (a[k][j] > 0) {
  45. a[i][j] = a[k][j];
  46. a[k][j] = 0;
  47. break;
  48. }
  49. }
  50. }
  51. a = dir(a, c);
  52. return a;
  53. }
  54.  
  55. board put(board a) {
  56. vector<pair<int, int>> zeros;
  57. rep(i, j) if (a[i][j] == 0) zeros.push_back({i, j});
  58. int p = rand() % 100;
  59. int k = rand() % zeros.size();
  60. int i = zeros[k].first;
  61. int j = zeros[k].second;
  62. int val = (p < 10) ? 4 : 2;
  63. a[i][j] = val;
  64. return a;
  65. }
  66.  
  67. // BEGIN
  68. // Энд функц заралж ашиглаж болно
  69.  
  70. board flip1(board a, bool z = 1) {
  71. board b;
  72. for (int i = 0; i < 4; i++) {
  73. for (int j = 0; j < 4; j++) {
  74. if (z)
  75. b[i][j] = a[j][i];
  76. else
  77. b[i][j] = a[3 - j][3 - i];
  78. }
  79. }
  80. return b;
  81. }
  82.  
  83. board dir1(board a, char c) {
  84. if (c == 'U')
  85. return a;
  86. if (c == 'L')
  87. return flip1(a);
  88. if (c == 'R')
  89. return flip1(a, 0);
  90. return flip1(flip1(a, 0));
  91. }
  92.  
  93. board move1(board a, char c) {
  94. a = dir1(a, c);
  95. for (int j = 0; j < 4; j++) {
  96. for (int i = 0; i < 4; i++) {
  97. if (a[i][j] == 0) continue;
  98. for (int k = i + 1; k < 4; k++) {
  99. if (a[k][j] == 0) continue;
  100. if (a[i][j] == a[k][j]) {
  101. a[i][j] *= -2;
  102. a[k][j] = 0;
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. for (int i = 0; i < 4; i++) {
  109. for (int j = 0; j < 4; j++) {
  110. if (a[i][j] < 0) a[i][j] = -a[i][j];
  111. }
  112. }
  113. for (int j = 0; j < 4; j++) {
  114. for (int i = 0; i < 4; i++) {
  115. if (a[i][j] > 0) continue;
  116. for (int k = i + 1; k < 4; k++) {
  117. if (a[k][j] > 0) {
  118. a[i][j] = a[k][j];
  119. a[k][j] = 0;
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. a = dir1(a, c);
  126. return a;
  127. }
  128.  
  129. bool over(const board& a, char move) {
  130. board b = move1(a, move);
  131. for (int i = 0; i < 4; i++) {
  132. for (int j = 0; j < 4; j++) {
  133. if (b[i][j] == 0) {
  134. return false;
  135. }
  136. }
  137. }
  138. for (int i = 0; i < 4; i++) {
  139. for (int j = 0; j < 4; j++) {
  140. if ((i < 3 && b[i][j] == b[i + 1][j]) || (j < 3 && b[i][j] == b[i][j + 1])) {
  141. return false;
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147.  
  148. int findsum(const board& a) {
  149. int sum = 0;
  150. for (int i = 0; i < 4; i++) {
  151. for (int j = 0; j < 4; j++) {
  152. sum += a[i][j];
  153. }
  154. }
  155. return sum;
  156. }
  157.  
  158. int sumafter(const board& a, char move) {
  159. board x = move1(a, move);
  160. return findsum(x);
  161. }
  162.  
  163. char play(board a) {
  164. char up = 'U';
  165. char down = 'D';
  166. char right = 'R';
  167. char left = 'L';
  168.  
  169. if (over(a, right)) {
  170. return right;
  171. }
  172.  
  173. board b = move1(a, up);
  174. if (a != b) {
  175. if (!over(b, up)) {
  176. return up;
  177. }
  178. }
  179. b = move1(a, down);
  180. if (a != b) {
  181. if (!over(b, down)) {
  182. return down;
  183. }
  184. }
  185. b = move1(a, right);
  186. if (a != b) {
  187. if (!over(b, right)) {
  188. return right;
  189. }
  190. }
  191. return left;
  192. }
  193.  
  194.  
  195. // Энд бичсэн хэсгээ л сервэрт илгээнэ
  196. // END
  197.  
  198. int main(int argc, char **argv) {
  199. string token;
  200. if (argc == 2) token = argv[1];
  201. srand(247);
  202. srand(time(0));
  203. board a; rep(i, j) a[i][j] = 0;
  204.  
  205. int itr = 0;
  206. clock_t begin = clock();
  207. while (true) {
  208. char c = play(a);
  209. rep(i, j) cout << a[i][j] << " \n"[j == 3];
  210. cout << c << "\n";
  211. if (c != 'U' && c != 'R' && c != 'D' && c != 'L') break;
  212. board b = move(a, c);
  213. if (itr > 0 && b == a) break;
  214. a = put(b);
  215. double timer = double(clock() - begin) / CLOCKS_PER_SEC;
  216. if (timer > 10) break;
  217. itr++;
  218. }
  219. int score = 0;
  220. rep(i, j) score += a[i][j];
  221. cout << "Score: " << score << endl;
  222. cout << "Last state:\n";
  223. rep(i, j) cout << a[i][j] << " \n"[j == 3];
  224. double timer = double(clock() - begin) / CLOCKS_PER_SEC;
  225. cout << "Time: " << (int)timer <<"sec\n";
  226. return 0;
  227. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
L
0 0 0 0
0 0 0 2
0 0 0 0
0 0 0 0
U
0 0 0 2
0 0 0 0
0 0 0 0
0 2 0 0
U
0 2 0 2
0 2 0 0
0 0 0 0
0 0 0 0
U
0 4 0 2
0 0 0 0
0 0 0 2
0 0 0 0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 2
8 16 32 8
4 8 2 32
0 4 4 2
D
2 8 8 2
2 16 32 8
8 8 2 32
4 4 4 2
U
4 8 8 2
8 16 32 8
4 8 2 32
2 4 4 2
R
2 4 16 2
8 16 32 8
4 8 2 32
0 2 8 2
D
2 4 16 2
2 16 32 8
8 8 2 32
4 2 8 2
U
4 4 16 2
8 16 32 8
4 8 2 32
2 2 8 2
R
2 8 16 2
8 16 32 8
4 8 2 32
0 4 8 2
D
2 8 16 2
2 16 32 8
8 8 2 32
4 4 8 2
U
4 8 16 2
8 16 32 8
4 8 2 32
2 4 8 2
R
Score: 156
Last state:
4 8 16 2
8 16 32 8
4 8 2 32
2 4 8 2
Time: 0sec