#include <cstdio> #include <string> #include <vector> using namespace std; int n, k; char board[3007][3007]; int possible_moves[3007][3007]; bool used[3007][3007]; int sum, number_of_moves; const int MOD = 1e9 + 7; //inline void printBoard() { // printf("\n"); // for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= n; ++j) // printf("%c", board[i][j]); // printf("\n"); // } // printf("\n"); //} inline void makePossible(int a, int b, bool if_possible) { if (board[a][b] == '.') { if (if_possible) { if (possible_moves[a][b] == 0) { ++number_of_moves; } ++possible_moves[a][b]; } else { --possible_moves[a][b]; if (possible_moves[a][b] == 0) { --number_of_moves; } } } } inline void makeAdjacentPossible(int a, int b, bool if_possible) { makePossible(a - 1, b, if_possible); makePossible(a + 1, b, if_possible); makePossible(a, b - 1, if_possible); makePossible(a, b + 1, if_possible); } inline void markAsUsed(int a, int b, bool if_used) { if (if_used) { used[a][b] = true; --number_of_moves; } else { used[a][b] = false; ++number_of_moves; } } inline void findPossibleMoves() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (board[i][j] == '#') makeAdjacentPossible(i, j, true); } } } void placeTile(int tiles, int a, int b) { if (tiles == k - 1) { board[a][b] = '#'; --number_of_moves; makeAdjacentPossible(a, b, true); //printBoard(); sum += number_of_moves; sum %= MOD; board[a][b] = '.'; ++number_of_moves; makeAdjacentPossible(a, b, false); } else { board[a][b] = '#'; --number_of_moves; makeAdjacentPossible(a, b, true); vector<pair<int, int>> usedPositions; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (possible_moves[i][j] > 0 && board[i][j] == '.' && !used[i][j]) { placeTile(tiles + 1, i, j); markAsUsed(i, j, true); usedPositions.emplace_back(i, j); } } } board[a][b] = '.'; ++number_of_moves; makeAdjacentPossible(a, b, false); for (int i = 0; i < (int)usedPositions.size(); ++i) { pair<int, int> p = usedPositions[i]; markAsUsed(p.first, p.second, false); } } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%s", board[i] + 1); board[i][0] = '*'; board[i][n + 1] = '*'; } for (int i = 0; i <= n + 1; ++i) { board[0][i] = '*'; board[n + 1][i] = '*'; } findPossibleMoves(); if (k > 1) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (possible_moves[i][j] > 0 && board[i][j] == '.') { placeTile(1, i, j); markAsUsed(i, j, true); } } } printf("%d", sum); } else printf("%d", number_of_moves); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include <cstdio> #include <string> #include <vector> using namespace std; int n, k; char board[3007][3007]; int possible_moves[3007][3007]; bool used[3007][3007]; int sum, number_of_moves; const int MOD = 1e9 + 7; //inline void printBoard() { // printf("\n"); // for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= n; ++j) // printf("%c", board[i][j]); // printf("\n"); // } // printf("\n"); //} inline void makePossible(int a, int b, bool if_possible) { if (board[a][b] == '.') { if (if_possible) { if (possible_moves[a][b] == 0) { ++number_of_moves; } ++possible_moves[a][b]; } else { --possible_moves[a][b]; if (possible_moves[a][b] == 0) { --number_of_moves; } } } } inline void makeAdjacentPossible(int a, int b, bool if_possible) { makePossible(a - 1, b, if_possible); makePossible(a + 1, b, if_possible); makePossible(a, b - 1, if_possible); makePossible(a, b + 1, if_possible); } inline void markAsUsed(int a, int b, bool if_used) { if (if_used) { used[a][b] = true; --number_of_moves; } else { used[a][b] = false; ++number_of_moves; } } inline void findPossibleMoves() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (board[i][j] == '#') makeAdjacentPossible(i, j, true); } } } void placeTile(int tiles, int a, int b) { if (tiles == k - 1) { board[a][b] = '#'; --number_of_moves; makeAdjacentPossible(a, b, true); //printBoard(); sum += number_of_moves; sum %= MOD; board[a][b] = '.'; ++number_of_moves; makeAdjacentPossible(a, b, false); } else { board[a][b] = '#'; --number_of_moves; makeAdjacentPossible(a, b, true); vector<pair<int, int>> usedPositions; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (possible_moves[i][j] > 0 && board[i][j] == '.' && !used[i][j]) { placeTile(tiles + 1, i, j); markAsUsed(i, j, true); usedPositions.emplace_back(i, j); } } } board[a][b] = '.'; ++number_of_moves; makeAdjacentPossible(a, b, false); for (int i = 0; i < (int)usedPositions.size(); ++i) { pair<int, int> p = usedPositions[i]; markAsUsed(p.first, p.second, false); } } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%s", board[i] + 1); board[i][0] = '*'; board[i][n + 1] = '*'; } for (int i = 0; i <= n + 1; ++i) { board[0][i] = '*'; board[n + 1][i] = '*'; } findPossibleMoves(); if (k > 1) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (possible_moves[i][j] > 0 && board[i][j] == '.') { placeTile(1, i, j); markAsUsed(i, j, true); } } } printf("%d", sum); } else printf("%d", number_of_moves); return 0; } |