#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; const int MAXN = 3000 + 10; const int INF = MAXN * MAXN; const int MOD = 1e9 + 7; int n, k; string s[MAXN]; ULL w[MAXN][MAXN]; ULL curhash; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; bool ins(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; } vector<pair<int,int>> cands; int dist[MAXN][MAXN]; int used[MAXN][MAXN]; int iscnd[MAXN][MAXN]; int res; unordered_set<LL> M; void gogo(int x) { if (x == k) { if (M.insert(curhash).second) { res = (res + 1) % MOD; } return; } for (int i = 0; i < cands.size(); i++) { auto p = cands[i]; if (used[p.first][p.second]) continue; used[p.first][p.second] = 1; curhash += w[p.first][p.second]; int added = 0; for (int i = 0; i < 4; i++) { int x = p.first + dx[i]; int y = p.second + dy[i]; if (!ins(x, y)) continue; if (s[x][y] == '#') continue; if (iscnd[x][y]) continue; added++; iscnd[x][y] = 1; cands.push_back(make_pair(x, y)); } gogo(x + 1); curhash -= w[p.first][p.second]; for (int i = 0; i < added; i++) { auto p = cands.back(); iscnd[p.first][p.second] = 0; cands.pop_back(); } used[p.first][p.second] = 0; } } int main() { ios_base::sync_with_stdio(0); srand(time(0)); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> s[i]; } queue<pair<int,int>> Q; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[i][j] == '.') { w[i][j] = ((ULL)rand() << 32) + (unsigned)rand(); dist[i][j] = INF; } else { Q.push(make_pair(i, j)); dist[i][j] = 0; } } } while (!Q.empty()) { auto p = Q.front(); Q.pop(); if (dist[p.first][p.second] == k) { continue; } for (int i = 0; i < 4; i++) { int x = p.first + dx[i]; int y = p.second + dy[i]; if (!ins(x, y)) continue; if (dist[p.first][p.second] + 1 < dist[x][y]) { dist[x][y] = dist[p.first][p.second] + 1; Q.push(make_pair(x, y)); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dist[i][j] == 1) { cands.push_back({i, j}); iscnd[i][j] = 1; } } } gogo(0); cout << res << "\n"; 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 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long ULL; const int MAXN = 3000 + 10; const int INF = MAXN * MAXN; const int MOD = 1e9 + 7; int n, k; string s[MAXN]; ULL w[MAXN][MAXN]; ULL curhash; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; bool ins(int a, int b) { return a >= 0 && a < n && b >= 0 && b < n; } vector<pair<int,int>> cands; int dist[MAXN][MAXN]; int used[MAXN][MAXN]; int iscnd[MAXN][MAXN]; int res; unordered_set<LL> M; void gogo(int x) { if (x == k) { if (M.insert(curhash).second) { res = (res + 1) % MOD; } return; } for (int i = 0; i < cands.size(); i++) { auto p = cands[i]; if (used[p.first][p.second]) continue; used[p.first][p.second] = 1; curhash += w[p.first][p.second]; int added = 0; for (int i = 0; i < 4; i++) { int x = p.first + dx[i]; int y = p.second + dy[i]; if (!ins(x, y)) continue; if (s[x][y] == '#') continue; if (iscnd[x][y]) continue; added++; iscnd[x][y] = 1; cands.push_back(make_pair(x, y)); } gogo(x + 1); curhash -= w[p.first][p.second]; for (int i = 0; i < added; i++) { auto p = cands.back(); iscnd[p.first][p.second] = 0; cands.pop_back(); } used[p.first][p.second] = 0; } } int main() { ios_base::sync_with_stdio(0); srand(time(0)); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> s[i]; } queue<pair<int,int>> Q; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[i][j] == '.') { w[i][j] = ((ULL)rand() << 32) + (unsigned)rand(); dist[i][j] = INF; } else { Q.push(make_pair(i, j)); dist[i][j] = 0; } } } while (!Q.empty()) { auto p = Q.front(); Q.pop(); if (dist[p.first][p.second] == k) { continue; } for (int i = 0; i < 4; i++) { int x = p.first + dx[i]; int y = p.second + dy[i]; if (!ins(x, y)) continue; if (dist[p.first][p.second] + 1 < dist[x][y]) { dist[x][y] = dist[p.first][p.second] + 1; Q.push(make_pair(x, y)); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dist[i][j] == 1) { cands.push_back({i, j}); iscnd[i][j] = 1; } } } gogo(0); cout << res << "\n"; return 0; } |