#include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; const int N=3005; const long long MOD=1e9+7; int n, k, odl[N][N]; char c[N]; bool inv[N][N]; queue< pair<int,int> > q; vector< pair<int,int> > v[4]; long long ans, ilo[N][N]; void check(int x, int y, int odlo, long long il) { if(!inv[x][y]) { inv[x][y]=1; odl[x][y]=odlo; ilo[x][y]=il; v[odlo].push_back(make_pair(x,y)); if(odlo!=k) q.push(make_pair(x,y)); } else if(odlo!=1 && odlo==odl[x][y]) ilo[x][y]+=il; } void bfs() { while(!q.empty()) { int x=q.front().first; int y=q.front().second; int odlo=odl[x][y]+1; long long il=ilo[x][y]; q.pop(); check(x+1,y,odlo,il); check(x-1,y,odlo,il); check(x,y-1,odlo,il); check(x,y+1,odlo,il); } } long long pot(long long a, long long p) { if(p==0) return 1; long long w=pot(a,p/2); w=w*w%MOD; if(p%2==0) return w; return w*a%MOD; } long long ilew(int x, int y) { long long ans=0; if(odl[x-1][y]>odl[x][y]) ans++; if(odl[x][y-1]>odl[x][y]) ans++; if(odl[x][y+1]>odl[x][y]) ans++; if(odl[x+1][y]>odl[x][y]) ans++; return ans; } int main() { scanf("%d%d", &n, &k); for(int i=1;i<=n;i++) { scanf("%s", &c); for(int j=0;j<n;j++) if(c[j]=='#') { inv[i][j+1]=1; ilo[i][j+1]=1; q.push(make_pair(i,j+1)); } } for(int i=1;i<=n;i++) { inv[0][i]=1; inv[n+1][i]=1; inv[i][0]=1; inv[i][n+1]=1; } bfs(); if(k==1) ans=(long long)(v[1].size())%MOD; if(k==2) { long long tmp=v[1].size(); ans=tmp*(tmp-1); ans/=2; ans%=MOD; for(int i=0;i<v[2].size();i++) { int x=v[2][i].first; int y=v[2][i].second; ans+=ilo[x][y]; ans%=MOD; } } if(k==3) { long long ilo1=v[1].size(), ilo2=v[2].size(), ilo3=v[3].size(); for(int i=0;i<ilo3;i++) { int x=v[3].size(); int y=v[3].size(); ans+=ilo[x][y]; ans%=MOD; } for(int i=0;i<ilo1;i++) { int x=v[1][i].first; int y=v[1][i].second; long long d=ilew(x,y); ans+=(d*(d-1))/2; ans%=MOD; } for(int i=0;i<ilo2;i++) { int x=v[2][i].first; int y=v[2][i].second; ans+=(ilo1-ilo[x][y])*ilo[x][y]; ans%=MOD; ans+=((ilo[x][y]-1)*(ilo[x][y]))/2; ans%=MOD; } long long tmp=ilo1*(ilo1-1); tmp%=MOD; tmp*=(ilo1-2); tmp%=MOD; tmp*=pot(6,MOD-2); tmp%=MOD; ans+=tmp; ans%=MOD; } if(k==4) { long long ilo1=v[1].size(), ilo2=v[2].size(), ilo3=v[3].size(); long long ilo4=v[4].size(); for(int i=0;i<ilo4;i++) { int x=v[4][i].first; int y=v[4][i].second; ans+=ilo[x][y]; //1 ans%=MOD; } long long tmp=ilo4*(ilo4-1); tmp%=MOD; tmp*=(ilo4-2); tmp%=MOD; tmp*=(ilo4-3); tmp%=MOD; ans+=tmp; ans%=MOD; for(int i=0;i<ilo2;i++) { int x=v[2][i].first; int y=v[2][i].second; long long d=ilew(x,y); ans+=((d*(d-1))/2)*ilo[x][y]; ans%=MOD; long long tmp=(ilo1-ilo[x][y])*(ilo1-ilo[x][y]-1); tmp%=MOD; tmp*=ilo[x][y]; tmp%=MOD; tmp+=(ilo[x][y]*(ilo[x][y]-1)*(ilo[x][y]-2))/6; tmp%=MOD; ans+=tmp; ans%=MOD; tmp=d*ilo[x][y]*(ilo1-ilo[x][y])+ilo[x][y]*(ilo[x][y]-1)*d*(d-1); //5 tmp%=MOD; ans+=tmp; ans%=MOD; } for(int i=0;i<ilo1;i++) { int x=v[1][i].first; int y=v[1][i].second; long long d=ilew(x,y); if(d==3) ans++; ans%=MOD; } for(int i=0;i<ilo3;i++) { int x=v[3][i].first; int y=v[3][i].second; } } printf("%lld", ans); 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; const int N=3005; const long long MOD=1e9+7; int n, k, odl[N][N]; char c[N]; bool inv[N][N]; queue< pair<int,int> > q; vector< pair<int,int> > v[4]; long long ans, ilo[N][N]; void check(int x, int y, int odlo, long long il) { if(!inv[x][y]) { inv[x][y]=1; odl[x][y]=odlo; ilo[x][y]=il; v[odlo].push_back(make_pair(x,y)); if(odlo!=k) q.push(make_pair(x,y)); } else if(odlo!=1 && odlo==odl[x][y]) ilo[x][y]+=il; } void bfs() { while(!q.empty()) { int x=q.front().first; int y=q.front().second; int odlo=odl[x][y]+1; long long il=ilo[x][y]; q.pop(); check(x+1,y,odlo,il); check(x-1,y,odlo,il); check(x,y-1,odlo,il); check(x,y+1,odlo,il); } } long long pot(long long a, long long p) { if(p==0) return 1; long long w=pot(a,p/2); w=w*w%MOD; if(p%2==0) return w; return w*a%MOD; } long long ilew(int x, int y) { long long ans=0; if(odl[x-1][y]>odl[x][y]) ans++; if(odl[x][y-1]>odl[x][y]) ans++; if(odl[x][y+1]>odl[x][y]) ans++; if(odl[x+1][y]>odl[x][y]) ans++; return ans; } int main() { scanf("%d%d", &n, &k); for(int i=1;i<=n;i++) { scanf("%s", &c); for(int j=0;j<n;j++) if(c[j]=='#') { inv[i][j+1]=1; ilo[i][j+1]=1; q.push(make_pair(i,j+1)); } } for(int i=1;i<=n;i++) { inv[0][i]=1; inv[n+1][i]=1; inv[i][0]=1; inv[i][n+1]=1; } bfs(); if(k==1) ans=(long long)(v[1].size())%MOD; if(k==2) { long long tmp=v[1].size(); ans=tmp*(tmp-1); ans/=2; ans%=MOD; for(int i=0;i<v[2].size();i++) { int x=v[2][i].first; int y=v[2][i].second; ans+=ilo[x][y]; ans%=MOD; } } if(k==3) { long long ilo1=v[1].size(), ilo2=v[2].size(), ilo3=v[3].size(); for(int i=0;i<ilo3;i++) { int x=v[3].size(); int y=v[3].size(); ans+=ilo[x][y]; ans%=MOD; } for(int i=0;i<ilo1;i++) { int x=v[1][i].first; int y=v[1][i].second; long long d=ilew(x,y); ans+=(d*(d-1))/2; ans%=MOD; } for(int i=0;i<ilo2;i++) { int x=v[2][i].first; int y=v[2][i].second; ans+=(ilo1-ilo[x][y])*ilo[x][y]; ans%=MOD; ans+=((ilo[x][y]-1)*(ilo[x][y]))/2; ans%=MOD; } long long tmp=ilo1*(ilo1-1); tmp%=MOD; tmp*=(ilo1-2); tmp%=MOD; tmp*=pot(6,MOD-2); tmp%=MOD; ans+=tmp; ans%=MOD; } if(k==4) { long long ilo1=v[1].size(), ilo2=v[2].size(), ilo3=v[3].size(); long long ilo4=v[4].size(); for(int i=0;i<ilo4;i++) { int x=v[4][i].first; int y=v[4][i].second; ans+=ilo[x][y]; //1 ans%=MOD; } long long tmp=ilo4*(ilo4-1); tmp%=MOD; tmp*=(ilo4-2); tmp%=MOD; tmp*=(ilo4-3); tmp%=MOD; ans+=tmp; ans%=MOD; for(int i=0;i<ilo2;i++) { int x=v[2][i].first; int y=v[2][i].second; long long d=ilew(x,y); ans+=((d*(d-1))/2)*ilo[x][y]; ans%=MOD; long long tmp=(ilo1-ilo[x][y])*(ilo1-ilo[x][y]-1); tmp%=MOD; tmp*=ilo[x][y]; tmp%=MOD; tmp+=(ilo[x][y]*(ilo[x][y]-1)*(ilo[x][y]-2))/6; tmp%=MOD; ans+=tmp; ans%=MOD; tmp=d*ilo[x][y]*(ilo1-ilo[x][y])+ilo[x][y]*(ilo[x][y]-1)*d*(d-1); //5 tmp%=MOD; ans+=tmp; ans%=MOD; } for(int i=0;i<ilo1;i++) { int x=v[1][i].first; int y=v[1][i].second; long long d=ilew(x,y); if(d==3) ans++; ans%=MOD; } for(int i=0;i<ilo3;i++) { int x=v[3][i].first; int y=v[3][i].second; } } printf("%lld", ans); return 0; } |