Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define CHECK(x) \
- if (!x) \
- exit(-1);
- // TODO: SPADANIE BLOKÓW NAPISZ OD GÓRY
- int m, n, num_blocks, num_tests;
- bool exit_flag = false;
- typedef struct {
- int w, h;
- char def[10][10];
- } Block;
- Block blocks[8];
- int tests[8][8];
- void read_input() {
- CHECK(scanf("%d%d%d", &m, &n, &num_blocks))
- for (int i = 0; i < num_blocks; i++) {
- Block *block = &blocks[i];
- CHECK(scanf("%d%d", &block->w, &block->h))
- getchar();
- if (block->w > m || block->h > n)
- exit_flag = true;
- for (int y = 0; y < block->h; y++) {
- for (int x = 0; x < block->w; x++) {
- block->def[x][y] = getchar();
- if (y > 0 && block->def[x][y] == '.' &&
- (block->def[x][y - 1] == '#' || block->def[x][y - 1] == 'S')) {
- block->def[x][y] = 'S';
- }
- }
- getchar();
- }
- }
- CHECK(scanf("%d", &num_tests))
- for (int i = 0; i < num_tests; i++)
- for (int j = 0; j < num_blocks; j++)
- CHECK(scanf("%d", &tests[i][j]))
- }
- void print_blocks() {
- for (int i = 0; i < num_blocks; i++) {
- Block block = blocks[i];
- for (int y = 0; y < block.h; y++) {
- for (int x = 0; x < block.w; x++)
- putchar(block.def[x][y]);
- putchar('\n');
- }
- }
- }
- void setup_board(char board[m][n]) {
- for (int y = 0; y < n; y++) {
- for (int x = 0; x < m; x++) {
- board[x][y] = '.';
- }
- }
- }
- void print_board(char board[m][n]) {
- for (int y = 0; y < n; y++) {
- for (int x = 0; x < m; x++) {
- putchar(board[x][y]);
- }
- putchar('\n');
- }
- putchar('\n');
- }
- int permute(char board[m][n], int order[num_blocks], int order_idx) {
- if (order_idx == num_blocks) {
- int lines_count = 0;
- for (int y = 0; y < n; y++) {
- bool complete_line = true;
- for (int x = 0; x < m; x++) {
- if (board[x][y] == '.' || board[x][y] == 'S') {
- complete_line = false;
- break;
- }
- }
- if (complete_line)
- lines_count++;
- }
- return lines_count;
- }
- Block block = blocks[order[order_idx]];
- int max = 0;
- bool placed_at_least_one = false;
- for (int i = 0; i < m - block.w + 1; i++) {
- for (int j = n - 1; j >= 0; j--) {
- if (j < block.h - 1) {
- return -1000;
- }
- bool can_be_placed = true;
- int block_y = block.h - 1;
- for (int jj = j; jj > j - block.h; jj--) {
- for (int k = i; k < i + block.w; k++) {
- if (block.def[k - i][block_y] == '#' &&
- (board[k][jj] == '#' || board[k][jj] == 'S')) {
- can_be_placed = false;
- jj = -1;
- break;
- }
- }
- block_y--;
- }
- if (can_be_placed) {
- placed_at_least_one = true;
- char board_cpy[m][n];
- memcpy(board_cpy, board, sizeof(board_cpy));
- for (int jj = j + 1; jj < n; jj++) {
- for (int x = 0; x < block.w; x++) {
- if (board[x + i][jj] == '.')
- board_cpy[x + i][jj] = 'S';
- }
- }
- for (int y = block.h - 1; y >= 0; y--) {
- for (int x = 0; x < block.w; x++) {
- if (board_cpy[x + i][j] == '#' &&
- (block.def[x][y] == '.' || block.def[x][y] == 'S'))
- continue;
- board_cpy[x + i][j] = block.def[x][y];
- }
- j--;
- }
- // print_board(board_cpy);
- int current =
- permute(board_cpy, order, order_idx + 1); //+ completed_lines;
- if (current > max)
- max = current;
- j = -1;
- }
- }
- }
- if (!placed_at_least_one)
- return -1000;
- return max;
- }
- int main(void) {
- read_input();
- if (exit_flag) {
- printf("0\n");
- return 0;
- }
- char board[m][n];
- setup_board(board);
- for (int i = 0; i < num_tests; i++) {
- int *order = tests[i];
- char board_cpy[m][n];
- memcpy(board_cpy, board, sizeof(board));
- int result = permute(board, order, 0);
- printf("%d ", result);
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment