#include <cstdio> #include <unordered_set> #include <vector> #include <queue> #include <math.h> int n,k; int ilepol; int starti,startj; long long pot[1000000]; std::unordered_set<long long> hashtab; struct para{ int i,j; para(int i, int j){ this->i = i; this->j = j; } para(){} }; std::vector<para> ruchy(const std::vector<std::vector<bool>>& v,int i,int j){ std::vector<para> wyn; if(i-1>=0 && !v[i-1][j]){ wyn.emplace_back(i-1,j); } if(i+1<v.size() && !v[i+1][j] ){ wyn.emplace_back(i+1,j); } if(j+1<v[i].size() && !v[i][j+1]){ wyn.emplace_back(i,j+1); } if(j-1>=0 && !v[i][j-1]){ wyn.emplace_back(i,j-1); } return wyn; } std::vector<para> ruchypel(const std::vector<std::vector<bool>>& v,int i,int j){ std::vector<para> wyn; if(i-1>=0 && v[i-1][j]){ wyn.emplace_back(i-1,j); } if(i+1<v.size() && v[i+1][j] ){ wyn.emplace_back(i+1,j); } if(j+1<v[i].size() && v[i][j+1]){ wyn.emplace_back(i,j+1); } if(j-1>=0 && v[i][j-1]){ wyn.emplace_back(i,j-1); } return wyn; } long long wynik; long long hash(const std::vector<std::vector<bool>>& v){ long long res = 0; for(int i=0; i<v.size(); i++){ for(int j=0; j<v[i].size(); j++){ res+=(v[i][j]*pot[((j)+(i)*(n))%1000000])%1000000007; res%=1000000007; } } return res; } void jebaj(int i,int j,int depth,const std::vector<std::vector<bool>>& v){ long long hashed = hash(v); if(depth ==k && hashtab.find(hashed)==hashtab.end() ){ wynik++; hashtab.insert(hashed); } bool visited[n+2][n+2]; bool visitedpus[n+2][n+2]; for(int i=0; i<n+1; i++){ for(int j=0; j<n+1; j++){ visited[i][j] = false; visitedpus[i][j] = false; } } std::queue<para> q; q.emplace(i,j); while(!q.empty()){ i = q.front().i; j = q.front().j; q.pop(); visited[i][j] = true; if(depth<k){ std::vector<para> aruchy = ruchy(v,i,j); for(int it=0; it<aruchy.size(); it++){ if(!visitedpus[aruchy[it].i][aruchy[it].j]){ visitedpus[aruchy[it].i][aruchy[it].j] = true; std::vector<std::vector<bool>> tmpv = v; tmpv[aruchy[it].i][aruchy[it].j] = true; jebaj(aruchy[it].i,aruchy[it].j,depth+1,tmpv); } } } std::vector<para> ruchypelne = ruchypel(v,i,j); for(int i=0; i<ruchypelne.size(); i++){ if(!visited[ruchypelne[i].i][ruchypelne[i].j]){ q.emplace(ruchypelne[i].i,ruchypelne[i].j); } } } } int main(){ pot[0] = 1; for(int i=1; i<1000000; i++){ pot[i] = (pot[i-1]*2137)%1000000007; } scanf("%d%d",&n,&k); std::vector<std::vector<bool>> v; for(int i=0; i<n; i++){ std::vector<bool> temp; v.push_back(temp); for(int j=0; j<n; j++){ char wyb; scanf(" %c",&wyb); if(wyb=='.'){ v[i].push_back(false); } else{//# v[i].push_back(true); ilepol++; starti = i; startj = j; } } } /*for(int i=0; i<v.size(); i++){ for(int j=0; j<v[i].size(); j++){ printf("%d ",(int)v[i][j]); } printf("\n"); }*/ jebaj(starti,startj,0,v); printf("%lld",wynik); }
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 | #include <cstdio> #include <unordered_set> #include <vector> #include <queue> #include <math.h> int n,k; int ilepol; int starti,startj; long long pot[1000000]; std::unordered_set<long long> hashtab; struct para{ int i,j; para(int i, int j){ this->i = i; this->j = j; } para(){} }; std::vector<para> ruchy(const std::vector<std::vector<bool>>& v,int i,int j){ std::vector<para> wyn; if(i-1>=0 && !v[i-1][j]){ wyn.emplace_back(i-1,j); } if(i+1<v.size() && !v[i+1][j] ){ wyn.emplace_back(i+1,j); } if(j+1<v[i].size() && !v[i][j+1]){ wyn.emplace_back(i,j+1); } if(j-1>=0 && !v[i][j-1]){ wyn.emplace_back(i,j-1); } return wyn; } std::vector<para> ruchypel(const std::vector<std::vector<bool>>& v,int i,int j){ std::vector<para> wyn; if(i-1>=0 && v[i-1][j]){ wyn.emplace_back(i-1,j); } if(i+1<v.size() && v[i+1][j] ){ wyn.emplace_back(i+1,j); } if(j+1<v[i].size() && v[i][j+1]){ wyn.emplace_back(i,j+1); } if(j-1>=0 && v[i][j-1]){ wyn.emplace_back(i,j-1); } return wyn; } long long wynik; long long hash(const std::vector<std::vector<bool>>& v){ long long res = 0; for(int i=0; i<v.size(); i++){ for(int j=0; j<v[i].size(); j++){ res+=(v[i][j]*pot[((j)+(i)*(n))%1000000])%1000000007; res%=1000000007; } } return res; } void jebaj(int i,int j,int depth,const std::vector<std::vector<bool>>& v){ long long hashed = hash(v); if(depth ==k && hashtab.find(hashed)==hashtab.end() ){ wynik++; hashtab.insert(hashed); } bool visited[n+2][n+2]; bool visitedpus[n+2][n+2]; for(int i=0; i<n+1; i++){ for(int j=0; j<n+1; j++){ visited[i][j] = false; visitedpus[i][j] = false; } } std::queue<para> q; q.emplace(i,j); while(!q.empty()){ i = q.front().i; j = q.front().j; q.pop(); visited[i][j] = true; if(depth<k){ std::vector<para> aruchy = ruchy(v,i,j); for(int it=0; it<aruchy.size(); it++){ if(!visitedpus[aruchy[it].i][aruchy[it].j]){ visitedpus[aruchy[it].i][aruchy[it].j] = true; std::vector<std::vector<bool>> tmpv = v; tmpv[aruchy[it].i][aruchy[it].j] = true; jebaj(aruchy[it].i,aruchy[it].j,depth+1,tmpv); } } } std::vector<para> ruchypelne = ruchypel(v,i,j); for(int i=0; i<ruchypelne.size(); i++){ if(!visited[ruchypelne[i].i][ruchypelne[i].j]){ q.emplace(ruchypelne[i].i,ruchypelne[i].j); } } } } int main(){ pot[0] = 1; for(int i=1; i<1000000; i++){ pot[i] = (pot[i-1]*2137)%1000000007; } scanf("%d%d",&n,&k); std::vector<std::vector<bool>> v; for(int i=0; i<n; i++){ std::vector<bool> temp; v.push_back(temp); for(int j=0; j<n; j++){ char wyb; scanf(" %c",&wyb); if(wyb=='.'){ v[i].push_back(false); } else{//# v[i].push_back(true); ilepol++; starti = i; startj = j; } } } /*for(int i=0; i<v.size(); i++){ for(int j=0; j<v[i].size(); j++){ printf("%d ",(int)v[i][j]); } printf("\n"); }*/ jebaj(starti,startj,0,v); printf("%lld",wynik); } |