#include <iostream> #include <cmath> #include <vector> using namespace std; #define mod 1000000007 void possib(vector<bool> board, unsigned k, int n, unsigned it, unsigned &p); vector<vector<bool>> past; int main(){ cin.tie(NULL); ios::sync_with_stdio(false); unsigned n,k; cin >> n >> k; unsigned n2 = n*n; vector<bool> board(n2,0); for(unsigned i = 0; i < n2; ++i){ char c; cin >> c; if(c == '#') board[i] = 1; else board[i] = 0; } unsigned pos=0; possib(board, k, n, 0, pos); cout << pos%mod << endl; return 0; } void possib(vector<bool> board, unsigned k, int n, unsigned it, unsigned &p){ if(k == 0){ for(unsigned i=0; i<past.size(); ++i){ if(past[i]==board) return; } past.push_back(board); ++p; if(p >= mod) p%=mod; return; } int en = board.size(); for(int i=it; i<en; ++i){ if(board[i] == 0 && ((i%n>0 && board[i-1]) || ((i+1)%n > 0 && board[i+1]) || (i>=n && board[i-n]) || (i<n*(n-1) && board[i+n]))){ board[i] = 1; possib(board, k-1, n, max(0,i-n), p); board[i] = 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 | #include <iostream> #include <cmath> #include <vector> using namespace std; #define mod 1000000007 void possib(vector<bool> board, unsigned k, int n, unsigned it, unsigned &p); vector<vector<bool>> past; int main(){ cin.tie(NULL); ios::sync_with_stdio(false); unsigned n,k; cin >> n >> k; unsigned n2 = n*n; vector<bool> board(n2,0); for(unsigned i = 0; i < n2; ++i){ char c; cin >> c; if(c == '#') board[i] = 1; else board[i] = 0; } unsigned pos=0; possib(board, k, n, 0, pos); cout << pos%mod << endl; return 0; } void possib(vector<bool> board, unsigned k, int n, unsigned it, unsigned &p){ if(k == 0){ for(unsigned i=0; i<past.size(); ++i){ if(past[i]==board) return; } past.push_back(board); ++p; if(p >= mod) p%=mod; return; } int en = board.size(); for(int i=it; i<en; ++i){ if(board[i] == 0 && ((i%n>0 && board[i-1]) || ((i+1)%n > 0 && board[i+1]) || (i>=n && board[i-n]) || (i<n*(n-1) && board[i+n]))){ board[i] = 1; possib(board, k-1, n, max(0,i-n), p); board[i] = 0; } } } |