fork download
  1. #include <stdio.h>
  2.  
  3. #define W (1920)
  4. #define H (1080)
  5.  
  6. char board[H][W];
  7. const char * actions = "UULLLDULDRRDDLDULDURLDDRUDLUDRUDLDRRRDLDUDDDLLDURLLUURUDLLUDDDULRRDRLULLLDDRDLUDRRRRUDLRDURLLDDDLLRULURDLLLLDLRRUDLRRLULUUUDULULLLRLLLUDLRDLDLUUUDRUULURURDLLRLDDDDDURDDULURUDDDULLLDRDLULURLUURLRUDLLUULUDLLDUUURLLDLDUURRRUURRRUDLUDURLLLLDRULLLRULRURLUDDLDUL";
  8.  
  9. void do_action(char action) {
  10. switch (action) {
  11. case 'U':
  12. {
  13. for (int x = 0; x < W; ++x) {
  14. int dst_y = 0;
  15. for (int y = 0; y < H; ++y) {
  16. if (board[y][x] != '.') {
  17. board[dst_y++][x] = board[y][x];
  18. }
  19. }
  20. for (; dst_y < H; ++dst_y) {
  21. board[dst_y][x] = '.';
  22. }
  23. }
  24. break;
  25. }
  26. case 'D':
  27. {
  28. for (int x = 0; x < W; ++x) {
  29. int dst_y = H-1;
  30. for (int y = H-1; y >= 0; --y) {
  31. if (board[y][x] != '.') {
  32. board[dst_y--][x] = board[y][x];
  33. }
  34. }
  35. for (; dst_y >= 0; --dst_y) {
  36. board[dst_y][x] = '.';
  37. }
  38. }
  39. break;
  40. }
  41. case 'L':
  42. {
  43. for (int y = 0; y < H; ++y) {
  44. int dst_x = 0;
  45. for (int x = 0; x < W; ++x) {
  46. if (board[y][x] != '.') {
  47. board[y][dst_x++] = board[y][x];
  48. }
  49. }
  50. for (; dst_x < W; ++dst_x) {
  51. board[y][dst_x] = '.';
  52. }
  53. }
  54. break;
  55. }
  56. case 'R':
  57. {
  58. for (int y = 0; y < H; ++y) {
  59. int dst_x = W-1;
  60. for (int x = W-1; x >= 0; --x) {
  61. if (board[y][x] != '.') {
  62. board[y][dst_x--] = board[y][x];
  63. }
  64. }
  65. for (; dst_x >= 0; --dst_x) {
  66. board[y][dst_x] = '.';
  67. }
  68. }
  69. break;
  70. }
  71. }
  72. }
  73.  
  74.  
  75. int main(int argc, const char* argv[]) {
  76. const char *ac = actions;
  77.  
  78. #if 1
  79. /* reduce qctions */
  80. char opt[512];
  81. char attr[256] = {0};
  82. attr['U'] = attr['D'] = 1;
  83. attr['L'] = attr['R'] = 2;
  84.  
  85. while (1) {
  86. char last[3] = {0};
  87. int dst = 0, count = 0;
  88. for (int i = 0; ac[i]; ++i) {
  89. count++;
  90. int a = attr[ac[i]];
  91. if (ac[i+1] == 0 || a != attr[ac[i+1]]) {
  92. if (last[a] != ac[i]) {
  93. opt[dst++] = ac[i];
  94. last[a] = ac[i];
  95. }
  96. }
  97. }
  98. opt[dst] = 0;
  99. ac = opt;
  100. if (dst == count)
  101. break;
  102. }
  103. #else
  104. ac = actions;
  105. #endif
  106.  
  107. /* read initial board */
  108. fread(board, W * H, 1, stdin);
  109.  
  110. /* doit */
  111. for (int i = 0; ac[i]; ++i) {
  112. do_action(ac[i]);
  113. }
  114.  
  115. /* write result */
  116. fwrite(board, W * H, 1, stdout);
  117. }
  118.  
Runtime error #stdin #stdout 0.03s 4468KB
stdin
Standard input is empty
stdout