#include <bits/stdc++.h> using namespace std; const int mod = 1e9+7; char in[3005][3005]; bool active[3005][3005]; bool locked[5][3005][3005]; int res = 0; void go(int n, int k) { if(k == 0) { ++res; res %= mod; return; } for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { if(!locked[k][i][j] && in[i][j] == '.' && active[i][j]) { //cout << k << i << j << endl; for(int x = k-1;x >= 0;--x) locked[x][i][j] = true; bool b1 = active[i-1][j]; bool b2 = active[i+1][j]; bool b3 = active[i][j-1]; bool b4 = active[i][j+1]; active[i][j-1] = active[i][j+1] = active[i-1][j] = active[i+1][j] = true; go(n, k-1); active[i-1][j] = b1; active[i+1][j] = b2; active[i][j-1] = b3; active[i][j+1] = b4; } } } } int main() { int n, k; scanf("%d%d", &n, &k); for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { scanf(" %c", &in[i][j]); } } for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { if(in[i][j] == '.' && (in[i][j-1] == '#' || in[i][j+1] == '#' || in[i-1][j] == '#' || in[i+1][j] == '#')) { active[i][j] = true; } } } go(n, k); printf("%d\n", res); 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 | #include <bits/stdc++.h> using namespace std; const int mod = 1e9+7; char in[3005][3005]; bool active[3005][3005]; bool locked[5][3005][3005]; int res = 0; void go(int n, int k) { if(k == 0) { ++res; res %= mod; return; } for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { if(!locked[k][i][j] && in[i][j] == '.' && active[i][j]) { //cout << k << i << j << endl; for(int x = k-1;x >= 0;--x) locked[x][i][j] = true; bool b1 = active[i-1][j]; bool b2 = active[i+1][j]; bool b3 = active[i][j-1]; bool b4 = active[i][j+1]; active[i][j-1] = active[i][j+1] = active[i-1][j] = active[i+1][j] = true; go(n, k-1); active[i-1][j] = b1; active[i+1][j] = b2; active[i][j-1] = b3; active[i][j+1] = b4; } } } } int main() { int n, k; scanf("%d%d", &n, &k); for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { scanf(" %c", &in[i][j]); } } for(int i = 1;i <= n;++i) { for(int j = 1;j <= n;++j) { if(in[i][j] == '.' && (in[i][j-1] == '#' || in[i][j+1] == '#' || in[i-1][j] == '#' || in[i+1][j] == '#')) { active[i][j] = true; } } } go(n, k); printf("%d\n", res); return 0; } |