#include <bits/stdc++.h> #include <unordered_set> #define ll long long int #define UI unsigned int #define duzo 30000000 #define inf 0xfffffff using namespace std; unordered_set<bitset<32*4> > Wyn; //vector<vector<bool> > int n, k; inline void AddWyn(vector<UI> ToAdd) { sort(ToAdd.begin(), ToAdd.end()); for (int i = k; i < 4; i++) { ToAdd.push_back(inf); } ll p1 = (((ll)ToAdd[0]) << 32) +(ll) ToAdd[1]; ll p2 = (((ll)ToAdd[2])<<32) + (ll)ToAdd[3]; bitset<32 * 4> bs1(p1); bitset<32 * 4> bs2(p2); bs1 = bs1<<64; bs1 |= bs2; Wyn.insert(bs1); } inline UI Flaten2d(UI a,UI b) { return (a << 16) + b; } struct Graf { vector <vector<bool> > Data; vector<UI> cur; template<typename T> bool VisitFriends(int x,int y,T Run) { // n <-- max size const static vector<int> rotx = { 1,0,-1,0 }; const static vector<int> roty = { 0,1,0,-1 }; bool out = false; for (int i = 0; i < 4; i++) { if (x + rotx[i] >= 0 && x + rotx[i] < n && (y + roty[i]) >= 0 && y + roty[i] < n) { out|= Run(x + rotx[i], y + roty[i]); } } return out; } void VisitEveryone() { auto Check = [&](int a, int b) { if (!Data[a][b]) { vector<UI> tmp = cur; tmp.push_back(Flaten2d(a, b)); AddWyn(tmp); } return 0; }; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (Data[i][j]) { VisitFriends(i, j, Check); } } } } }; void IterateOver(int MaxDepth,Graf G,vector<UI> nums) { if (MaxDepth == 1) { G.VisitEveryone(); return; } auto QCheck = [&](int x, int y) { return G.Data[x][y]; }; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (G.Data[i][j] || !G.VisitFriends(i,j,QCheck)) { continue; } auto tmp = G; tmp.Data[i][j] = true; tmp.cur.push_back(Flaten2d(i, j)); IterateOver(MaxDepth-1, tmp,nums); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); Wyn.reserve(duzo); cin >> n >> k; Graf G; G.Data.resize(n, vector<bool>(n)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { G.Data[i][j] = (s[j] == '#'); } } vector<UI> ppp; IterateOver(k, G,ppp); cout << Wyn.size(); 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 | #include <bits/stdc++.h> #include <unordered_set> #define ll long long int #define UI unsigned int #define duzo 30000000 #define inf 0xfffffff using namespace std; unordered_set<bitset<32*4> > Wyn; //vector<vector<bool> > int n, k; inline void AddWyn(vector<UI> ToAdd) { sort(ToAdd.begin(), ToAdd.end()); for (int i = k; i < 4; i++) { ToAdd.push_back(inf); } ll p1 = (((ll)ToAdd[0]) << 32) +(ll) ToAdd[1]; ll p2 = (((ll)ToAdd[2])<<32) + (ll)ToAdd[3]; bitset<32 * 4> bs1(p1); bitset<32 * 4> bs2(p2); bs1 = bs1<<64; bs1 |= bs2; Wyn.insert(bs1); } inline UI Flaten2d(UI a,UI b) { return (a << 16) + b; } struct Graf { vector <vector<bool> > Data; vector<UI> cur; template<typename T> bool VisitFriends(int x,int y,T Run) { // n <-- max size const static vector<int> rotx = { 1,0,-1,0 }; const static vector<int> roty = { 0,1,0,-1 }; bool out = false; for (int i = 0; i < 4; i++) { if (x + rotx[i] >= 0 && x + rotx[i] < n && (y + roty[i]) >= 0 && y + roty[i] < n) { out|= Run(x + rotx[i], y + roty[i]); } } return out; } void VisitEveryone() { auto Check = [&](int a, int b) { if (!Data[a][b]) { vector<UI> tmp = cur; tmp.push_back(Flaten2d(a, b)); AddWyn(tmp); } return 0; }; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (Data[i][j]) { VisitFriends(i, j, Check); } } } } }; void IterateOver(int MaxDepth,Graf G,vector<UI> nums) { if (MaxDepth == 1) { G.VisitEveryone(); return; } auto QCheck = [&](int x, int y) { return G.Data[x][y]; }; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (G.Data[i][j] || !G.VisitFriends(i,j,QCheck)) { continue; } auto tmp = G; tmp.Data[i][j] = true; tmp.cur.push_back(Flaten2d(i, j)); IterateOver(MaxDepth-1, tmp,nums); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); Wyn.reserve(duzo); cin >> n >> k; Graf G; G.Data.resize(n, vector<bool>(n)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { G.Data[i][j] = (s[j] == '#'); } } vector<UI> ppp; IterateOver(k, G,ppp); cout << Wyn.size(); return 0; } |