#include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const int INFTY=20000000; const int MAX=3100; const ll MOD=1000000007; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ bool B[MAX][MAX]; int L[MAX][MAX]; //layers int n,k; ll c=0; bool can(int i,int j){ if(B[i][j]) return false; if(i-1>=0&&B[i-1][j]) return true; if(i+1<n&&B[i+1][j]) return true; if(j-1>=0&&B[i][j-1]) return true; if(j+1<n&&B[i][j+1]) return true; return false; } bool ok(){ loop(i,0,n){ loop(j,0,n){ if(B[i][j]){ B[i][j]=false; if(!can(i,j)){ B[i][j]=true; return false; } B[i][j]=true; } } } return true; } void gen(int i,int str,int stc){ //number of move, start pos row, start pos if(i==k+1){ if(ok()) c=(c+1)%MOD; return; } loop(j,str,n){ int st=(j==str ? stc : 0); loop(l,st,n){ if(!B[j][l]&&L[j][l]>0){ B[j][l]=true; (l==n-1 ? gen(i+1,j+1,0) : gen(i+1,j,l+1)); B[j][l]=false; } } } } ll si(int k){ ll d=1; for(ll i=2;i<k+1;i++) d*=i; return d; } int main(){ ios_base::sync_with_stdio(0); int q; cin>>n>>k; char a; loop(i,0,n){ loop(j,0,n){ cin>>a; B[i][j]=(a=='#'); if(B[i][j]) L[i][j]=0; else L[i][j]=-1; } } //count layers loop(l,1,5){ loop(i,0,n){ loop(j,0,n){ if(L[i][j]==-1){ if(i-1>=0&&L[i-1][j]==l-1) L[i][j]=l; else if(i+1<n&&L[i+1][j]==l-1) L[i][j]=l; else if(j-1>=0&&L[i][j-1]==l-1) L[i][j]=l; else if(j+1<n&&L[i][j+1]==l-1) L[i][j]=l; } } } } gen(1,0,0); pln(c); }
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 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const int INFTY=20000000; const int MAX=3100; const ll MOD=1000000007; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ bool B[MAX][MAX]; int L[MAX][MAX]; //layers int n,k; ll c=0; bool can(int i,int j){ if(B[i][j]) return false; if(i-1>=0&&B[i-1][j]) return true; if(i+1<n&&B[i+1][j]) return true; if(j-1>=0&&B[i][j-1]) return true; if(j+1<n&&B[i][j+1]) return true; return false; } bool ok(){ loop(i,0,n){ loop(j,0,n){ if(B[i][j]){ B[i][j]=false; if(!can(i,j)){ B[i][j]=true; return false; } B[i][j]=true; } } } return true; } void gen(int i,int str,int stc){ //number of move, start pos row, start pos if(i==k+1){ if(ok()) c=(c+1)%MOD; return; } loop(j,str,n){ int st=(j==str ? stc : 0); loop(l,st,n){ if(!B[j][l]&&L[j][l]>0){ B[j][l]=true; (l==n-1 ? gen(i+1,j+1,0) : gen(i+1,j,l+1)); B[j][l]=false; } } } } ll si(int k){ ll d=1; for(ll i=2;i<k+1;i++) d*=i; return d; } int main(){ ios_base::sync_with_stdio(0); int q; cin>>n>>k; char a; loop(i,0,n){ loop(j,0,n){ cin>>a; B[i][j]=(a=='#'); if(B[i][j]) L[i][j]=0; else L[i][j]=-1; } } //count layers loop(l,1,5){ loop(i,0,n){ loop(j,0,n){ if(L[i][j]==-1){ if(i-1>=0&&L[i-1][j]==l-1) L[i][j]=l; else if(i+1<n&&L[i+1][j]==l-1) L[i][j]=l; else if(j-1>=0&&L[i][j-1]==l-1) L[i][j]=l; else if(j+1<n&&L[i][j+1]==l-1) L[i][j]=l; } } } } gen(1,0,0); pln(c); } |