dakodev

Tetris

Nov 30th, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define CHECK(x) \
  7. if (!x) \
  8. exit(-1);
  9.  
  10. // TODO: SPADANIE BLOKÓW NAPISZ OD GÓRY
  11.  
  12. int m, n, num_blocks, num_tests;
  13. bool exit_flag = false;
  14.  
  15. typedef struct {
  16. int w, h;
  17. char def[10][10];
  18. } Block;
  19.  
  20. Block blocks[8];
  21. int tests[8][8];
  22.  
  23. void read_input() {
  24. CHECK(scanf("%d%d%d", &m, &n, &num_blocks))
  25.  
  26. for (int i = 0; i < num_blocks; i++) {
  27. Block *block = &blocks[i];
  28. CHECK(scanf("%d%d", &block->w, &block->h))
  29. getchar();
  30. if (block->w > m || block->h > n)
  31. exit_flag = true;
  32. for (int y = 0; y < block->h; y++) {
  33. for (int x = 0; x < block->w; x++) {
  34. block->def[x][y] = getchar();
  35. if (y > 0 && block->def[x][y] == '.' &&
  36. (block->def[x][y - 1] == '#' || block->def[x][y - 1] == 'S')) {
  37. block->def[x][y] = 'S';
  38. }
  39. }
  40. getchar();
  41. }
  42. }
  43.  
  44. CHECK(scanf("%d", &num_tests))
  45. for (int i = 0; i < num_tests; i++)
  46. for (int j = 0; j < num_blocks; j++)
  47. CHECK(scanf("%d", &tests[i][j]))
  48. }
  49.  
  50. void print_blocks() {
  51. for (int i = 0; i < num_blocks; i++) {
  52. Block block = blocks[i];
  53. for (int y = 0; y < block.h; y++) {
  54. for (int x = 0; x < block.w; x++)
  55. putchar(block.def[x][y]);
  56. putchar('\n');
  57. }
  58. }
  59. }
  60.  
  61. void setup_board(char board[m][n]) {
  62. for (int y = 0; y < n; y++) {
  63. for (int x = 0; x < m; x++) {
  64. board[x][y] = '.';
  65. }
  66. }
  67. }
  68.  
  69. void print_board(char board[m][n]) {
  70. for (int y = 0; y < n; y++) {
  71. for (int x = 0; x < m; x++) {
  72. putchar(board[x][y]);
  73. }
  74. putchar('\n');
  75. }
  76. putchar('\n');
  77. }
  78.  
  79. int permute(char board[m][n], int order[num_blocks], int order_idx) {
  80. if (order_idx == num_blocks) {
  81. int lines_count = 0;
  82. for (int y = 0; y < n; y++) {
  83. bool complete_line = true;
  84. for (int x = 0; x < m; x++) {
  85. if (board[x][y] == '.' || board[x][y] == 'S') {
  86. complete_line = false;
  87. break;
  88. }
  89. }
  90. if (complete_line)
  91. lines_count++;
  92. }
  93. return lines_count;
  94. }
  95. Block block = blocks[order[order_idx]];
  96. int max = 0;
  97. bool placed_at_least_one = false;
  98.  
  99. for (int i = 0; i < m - block.w + 1; i++) {
  100. for (int j = n - 1; j >= 0; j--) {
  101. if (j < block.h - 1) {
  102. return -1000;
  103. }
  104. bool can_be_placed = true;
  105. int block_y = block.h - 1;
  106. for (int jj = j; jj > j - block.h; jj--) {
  107. for (int k = i; k < i + block.w; k++) {
  108. if (block.def[k - i][block_y] == '#' &&
  109. (board[k][jj] == '#' || board[k][jj] == 'S')) {
  110. can_be_placed = false;
  111. jj = -1;
  112. break;
  113. }
  114. }
  115. block_y--;
  116. }
  117. if (can_be_placed) {
  118. placed_at_least_one = true;
  119. char board_cpy[m][n];
  120. memcpy(board_cpy, board, sizeof(board_cpy));
  121. for (int jj = j + 1; jj < n; jj++) {
  122. for (int x = 0; x < block.w; x++) {
  123. if (board[x + i][jj] == '.')
  124. board_cpy[x + i][jj] = 'S';
  125. }
  126. }
  127. for (int y = block.h - 1; y >= 0; y--) {
  128. for (int x = 0; x < block.w; x++) {
  129. if (board_cpy[x + i][j] == '#' &&
  130. (block.def[x][y] == '.' || block.def[x][y] == 'S'))
  131. continue;
  132. board_cpy[x + i][j] = block.def[x][y];
  133. }
  134. j--;
  135. }
  136. // print_board(board_cpy);
  137. int current =
  138. permute(board_cpy, order, order_idx + 1); //+ completed_lines;
  139. if (current > max)
  140. max = current;
  141. j = -1;
  142. }
  143. }
  144. }
  145.  
  146. if (!placed_at_least_one)
  147. return -1000;
  148. return max;
  149. }
  150.  
  151. int main(void) {
  152. read_input();
  153. if (exit_flag) {
  154. printf("0\n");
  155. return 0;
  156. }
  157. char board[m][n];
  158. setup_board(board);
  159.  
  160. for (int i = 0; i < num_tests; i++) {
  161. int *order = tests[i];
  162. char board_cpy[m][n];
  163. memcpy(board_cpy, board, sizeof(board));
  164. int result = permute(board, order, 0);
  165. printf("%d ", result);
  166. }
  167. putchar('\n');
  168.  
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment