#include <iostream> #include <vector> #include <unordered_set> typedef unsigned long long ull; const int N = 3000; const ull MOD = 1000000007; // yep, I need it const bool tile = true; const bool no_tile = false; const std::hash<std::vector<bool>> hash; int n, k; std::vector<bool> plane; std::unordered_set<std::vector<bool>> planes; struct move { int row, col; }; inline int get_index(int row, int col) { return row * n + col; } std::vector<move> get_possible_moves() { std::vector<move> moves; for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { if (plane[get_index(row, col)] == tile) continue; if(col - 1 >= 0 && plane[get_index(row, col - 1)] == tile || col + 1 < n && plane[get_index(row, col + 1)] == tile || row - 1 >= 0 && plane[get_index(row - 1, col)] == tile || row + 1 < n && plane[get_index(row + 1, col)] == tile) { moves.push_back({ row, col }); } } } return moves; } void solve(int player) { std::vector<move> moves = get_possible_moves(); for(auto&& move : moves) { // do this move plane[get_index(move.row, move.col)] = tile; if(player + 1 == k) { // last player planes.insert(plane); } else { solve(player + 1); } plane[get_index(move.row, move.col)] = no_tile; } } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; plane.resize(n * n); char tmp; for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { std::cin >> tmp; plane[get_index(row, col)] = tmp == '#' ? tile : no_tile; } } solve(0); std::cout << planes.size(); 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 | #include <iostream> #include <vector> #include <unordered_set> typedef unsigned long long ull; const int N = 3000; const ull MOD = 1000000007; // yep, I need it const bool tile = true; const bool no_tile = false; const std::hash<std::vector<bool>> hash; int n, k; std::vector<bool> plane; std::unordered_set<std::vector<bool>> planes; struct move { int row, col; }; inline int get_index(int row, int col) { return row * n + col; } std::vector<move> get_possible_moves() { std::vector<move> moves; for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { if (plane[get_index(row, col)] == tile) continue; if(col - 1 >= 0 && plane[get_index(row, col - 1)] == tile || col + 1 < n && plane[get_index(row, col + 1)] == tile || row - 1 >= 0 && plane[get_index(row - 1, col)] == tile || row + 1 < n && plane[get_index(row + 1, col)] == tile) { moves.push_back({ row, col }); } } } return moves; } void solve(int player) { std::vector<move> moves = get_possible_moves(); for(auto&& move : moves) { // do this move plane[get_index(move.row, move.col)] = tile; if(player + 1 == k) { // last player planes.insert(plane); } else { solve(player + 1); } plane[get_index(move.row, move.col)] = no_tile; } } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; plane.resize(n * n); char tmp; for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { std::cin >> tmp; plane[get_index(row, col)] = tmp == '#' ? tile : no_tile; } } solve(0); std::cout << planes.size(); return 0; } |