#include <bits/stdc++.h> #define rep(a, b) for(int a = 0; a < b; a++) #define pb push_back #define var(a, b) __typeof(b) a = b #define foreach(a, b) for(var(a, (b).begin()); a != (b).end(); a++) using namespace std; typedef long long ull; const int MAX = 3002; const int MOD = 1e9+7; bool c[MAX][MAX]; const int X[] = {0, 1, 0 , -1}; const int Y[] = {1, 0, -1, 0}; ull Oblicz(bool c[MAX][MAX], int n, int k, int x, int y){ ull wyn = 0; for(int i = y; i < n; i++) for(int j = x; j < n; j++){ rep(r, 4) if(c[i][j] == false && i + X[r] < n && i + X[r] >= 0 && j + Y[r] < n && j + Y[r] >= 0 && c[i+X[r]][j+Y[r]] == true){ c[i][j] = true; if(k == 1) wyn = (wyn + 1)%MOD; else wyn = (wyn + Oblicz(c, n, k-1, i, j))%MOD; c[i][j] = false; break; } } return wyn; } int main(){ ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; char ch; rep(i, n) rep(j, n){ cin >> ch; if(ch == '#') c[i][j] = true; else c[i][j] = false; } cout << Oblicz(c, n, k, 0, 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 | #include <bits/stdc++.h> #define rep(a, b) for(int a = 0; a < b; a++) #define pb push_back #define var(a, b) __typeof(b) a = b #define foreach(a, b) for(var(a, (b).begin()); a != (b).end(); a++) using namespace std; typedef long long ull; const int MAX = 3002; const int MOD = 1e9+7; bool c[MAX][MAX]; const int X[] = {0, 1, 0 , -1}; const int Y[] = {1, 0, -1, 0}; ull Oblicz(bool c[MAX][MAX], int n, int k, int x, int y){ ull wyn = 0; for(int i = y; i < n; i++) for(int j = x; j < n; j++){ rep(r, 4) if(c[i][j] == false && i + X[r] < n && i + X[r] >= 0 && j + Y[r] < n && j + Y[r] >= 0 && c[i+X[r]][j+Y[r]] == true){ c[i][j] = true; if(k == 1) wyn = (wyn + 1)%MOD; else wyn = (wyn + Oblicz(c, n, k-1, i, j))%MOD; c[i][j] = false; break; } } return wyn; } int main(){ ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; char ch; rep(i, n) rep(j, n){ cin >> ch; if(ch == '#') c[i][j] = true; else c[i][j] = false; } cout << Oblicz(c, n, k, 0, 0); } |