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 find_sum(board a){
  149. int s=0;
  150. for (int i = 0; i < 4; i++){
  151. for (int j =0; j < 4; j++){
  152. if (a[i][j]==0){
  153. s++;
  154. }
  155. }
  156. }
  157. return s;
  158. }
  159.  
  160. int evaluate(board a) {
  161. int score = 0;
  162. for (int i = 0; i < 4; i++)
  163. for (int j = 0; j < 4; j++)
  164. score += a[i][j];
  165. return score;
  166. }
  167.  
  168. char play(board a) {
  169. char moves[] = {'U', 'D', 'L', 'R'};
  170. int maxScore = INT_MIN;
  171. char bestMove = 'U';
  172.  
  173. for (char move : moves) {
  174. board b = move1(a, move);
  175. if (b == a) continue;
  176. int score = evaluate(b);
  177. if (score > maxScore) {
  178. maxScore = score;
  179. bestMove = move;
  180. }
  181. }
  182. return bestMove;
  183. }
  184.  
  185.  
  186. // Энд бичсэн хэсгээ л сервэрт илгээнэ
  187. // END
  188.  
  189. int main(int argc, char **argv) {
  190. string token;
  191. if (argc == 2) token = argv[1];
  192. srand(247);
  193. srand(time(0));
  194. board a; rep(i, j) a[i][j] = 0;
  195.  
  196. int itr = 0;
  197. clock_t begin = clock();
  198. while (true) {
  199. char c = play(a);
  200. rep(i, j) cout << a[i][j] << " \n"[j == 3];
  201. cout << c << "\n";
  202. if (c != 'U' && c != 'R' && c != 'D' && c != 'L') break;
  203. board b = move(a, c);
  204. if (itr > 0 && b == a) break;
  205. a = put(b);
  206. double timer = double(clock() - begin) / CLOCKS_PER_SEC;
  207. if (timer > 10) break;
  208. itr++;
  209. }
  210. int score = 0;
  211. rep(i, j) score += a[i][j];
  212. cout << "Score: " << score << endl;
  213. cout << "Last state:\n";
  214. rep(i, j) cout << a[i][j] << " \n"[j == 3];
  215. double timer = double(clock() - begin) / CLOCKS_PER_SEC;
  216. cout << "Time: " << (int)timer <<"sec\n";
  217. return 0;
  218. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
U
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 2
U
0 0 0 2
0 0 0 0
0 0 0 4
0 0 0 0
U
0 0 0 2
0 0 0 4
0 0 0 0
0 0 2 0
U
0 0 2 2
0 2 0 4
0 0 0 0
0 0 0 0
U
0 2 2 2
0 0 0 4
0 0 0 0
0 0 2 0
U
0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 2 32 2
32 16 4 8
8 4 4 0
U
2 16 8 4
4 2 32 8
32 16 8 2
8 4 0 0
D
2 16 0 4
4 2 8 4
32 16 32 8
8 4 8 2
U
2 16 8 8
4 2 32 8
32 16 8 2
8 4 2 0
U
2 16 8 16
4 2 32 2
32 16 8 0
8 4 2 2
U
2 16 8 16
4 2 32 4
32 16 8 0
8 4 2 4
U
2 16 8 16
4 2 32 8
32 16 8 0
8 4 2 2
U
2 16 8 16
4 2 32 8
32 16 8 2
8 4 2 2
U
2 16 8 16
4 2 32 8
32 16 8 4
8 4 2 2
L
2 16 8 16
4 2 32 8
32 16 8 4
8 4 4 2
L
2 16 8 16
4 2 32 8
32 16 8 4
8 8 2 2
L
2 16 8 16
4 2 32 8
32 16 8 4
16 4 0 2
D
2 16 2 16
4 2 8 8
32 16 32 4
16 4 8 2
L
2 16 2 16
4 2 16 2
32 16 32 4
16 4 8 2
U
Score: 174
Last state:
2 16 2 16
4 2 16 2
32 16 32 4
16 4 8 2
Time: 0sec