#include <algorithm> #include <cstdio> #include <cstring> #include <set> #include <utility> #include <vector> std::vector<std::pair<int, int>> operator+(std::vector<std::pair<int, int>> a, const std::vector<std::pair<int, int>> &b) { for (auto it : b) a.push_back(it); return a; } const int MX = 3007; char S[MX][MX]; int n, k; bool ins(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= n); } std::set<std::vector<std::pair<int, int>>> bartosz_kostka; bool ok(int x, int y) { static const int dx[] = {-1, 1, 0, 0}; static const int dy[] = {0, 0, 1, -1}; if (ins(x, y) == false || S[x][y] == '#') return false; for (int i = 0; i < 4; ++i) { if (ins(x + dx[i], y + dy[i]) && S[x + dx[i]][y + dy[i]] == '#') return true; } return false; } void rek(int d, std::vector<std::pair<int, int>> vec) { if (d == k) { std::sort(vec.begin(), vec.end()); bartosz_kostka.insert(vec); return; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (ok(i, j)) { S[i][j] = '#'; vec.emplace_back(i, j); rek(d + 1, vec); vec.pop_back(); S[i][j] = '.'; } } } return; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf(" %s", S[i] + 1); } rek(0, {}); printf("%lu\n", bartosz_kostka.size()); return 0; for (auto it : bartosz_kostka) { for (auto ptr : it) { printf("{%d, %d} ", ptr.first, ptr.second); } puts(""); } 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 | #include <algorithm> #include <cstdio> #include <cstring> #include <set> #include <utility> #include <vector> std::vector<std::pair<int, int>> operator+(std::vector<std::pair<int, int>> a, const std::vector<std::pair<int, int>> &b) { for (auto it : b) a.push_back(it); return a; } const int MX = 3007; char S[MX][MX]; int n, k; bool ins(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= n); } std::set<std::vector<std::pair<int, int>>> bartosz_kostka; bool ok(int x, int y) { static const int dx[] = {-1, 1, 0, 0}; static const int dy[] = {0, 0, 1, -1}; if (ins(x, y) == false || S[x][y] == '#') return false; for (int i = 0; i < 4; ++i) { if (ins(x + dx[i], y + dy[i]) && S[x + dx[i]][y + dy[i]] == '#') return true; } return false; } void rek(int d, std::vector<std::pair<int, int>> vec) { if (d == k) { std::sort(vec.begin(), vec.end()); bartosz_kostka.insert(vec); return; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (ok(i, j)) { S[i][j] = '#'; vec.emplace_back(i, j); rek(d + 1, vec); vec.pop_back(); S[i][j] = '.'; } } } return; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf(" %s", S[i] + 1); } rek(0, {}); printf("%lu\n", bartosz_kostka.size()); return 0; for (auto it : bartosz_kostka) { for (auto ptr : it) { printf("{%d, %d} ", ptr.first, ptr.second); } puts(""); } return 0; } |