#include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; template<class V, class E> struct Graph { struct Edge : E { int v; Edge(E d, int w): E(d), v(w) { } }; struct Vertex : V, vector<Edge> { }; vector<Vertex> g; Graph(int n) : g(n), key(n, '.') { } void edgeD(int b, int e, E d = E()) { g[b].push_back(Edge(d, e)); } void edgeU(int b, int e, E d = E()) { Edge eg(d, e); g[b].push_back(eg); eg.v = b; g[e].push_back(eg); } void grid_edges(int n) { int m = n - 1; REP(y, n) { REP(x, n) { if (x > 0 && y > 0) { edgeU(y * n + x, (y - 1) * n + x - 1); } if (x > 0) { edgeU(y * n + x, y * n + x - 1); } if (x > 0 && y < m) { edgeU(y * n + x, (y + 1) * n + x - 1); } if (y < m) { edgeU(y * n + x, (y + 1) * n + x); } } } } void write() { REP(x, g.size()) { cout << x << ": "; for (auto it : g[x]) cout << it.v << " "; cout << "\n"; } } unordered_set<string> visited; string key; void moves_dfs(int v, int depth) { for (auto it : g[v]) { if (!g[it.v].visited) { key[it.v] = '#'; if (visited.find(key) == visited.end()) { g[it.v].visited = true; visited.insert(key); if (depth > 1) { moves_dfs(v, depth - 1); } } key[it.v] = '.'; g[it.v].visited = false; } } } int count_visited(string& str) { int result = 0; for (auto c : str) { if (c == '#') { ++result; } } return result; } int moves(int k) { visited.clear(); REP(x, g.size()) { if (g[x].visited) { moves_dfs((int)x, k); } } int result = 0; for (auto v : visited) { if (count_visited(v) == k) { ++result; } } return result; } }; struct Vs { bool visited; }; struct Ve { }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; Graph<Vs, Ve> g(n * n); g.grid_edges(n); int i = 0; REP(y, n) { string line; cin >> line; REP(x, n) { g.g[i++].visited = (line[x] == '#'); } } cout << g.moves(k) << endl; return 0; } #endif
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 | #include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; template<class V, class E> struct Graph { struct Edge : E { int v; Edge(E d, int w): E(d), v(w) { } }; struct Vertex : V, vector<Edge> { }; vector<Vertex> g; Graph(int n) : g(n), key(n, '.') { } void edgeD(int b, int e, E d = E()) { g[b].push_back(Edge(d, e)); } void edgeU(int b, int e, E d = E()) { Edge eg(d, e); g[b].push_back(eg); eg.v = b; g[e].push_back(eg); } void grid_edges(int n) { int m = n - 1; REP(y, n) { REP(x, n) { if (x > 0 && y > 0) { edgeU(y * n + x, (y - 1) * n + x - 1); } if (x > 0) { edgeU(y * n + x, y * n + x - 1); } if (x > 0 && y < m) { edgeU(y * n + x, (y + 1) * n + x - 1); } if (y < m) { edgeU(y * n + x, (y + 1) * n + x); } } } } void write() { REP(x, g.size()) { cout << x << ": "; for (auto it : g[x]) cout << it.v << " "; cout << "\n"; } } unordered_set<string> visited; string key; void moves_dfs(int v, int depth) { for (auto it : g[v]) { if (!g[it.v].visited) { key[it.v] = '#'; if (visited.find(key) == visited.end()) { g[it.v].visited = true; visited.insert(key); if (depth > 1) { moves_dfs(v, depth - 1); } } key[it.v] = '.'; g[it.v].visited = false; } } } int count_visited(string& str) { int result = 0; for (auto c : str) { if (c == '#') { ++result; } } return result; } int moves(int k) { visited.clear(); REP(x, g.size()) { if (g[x].visited) { moves_dfs((int)x, k); } } int result = 0; for (auto v : visited) { if (count_visited(v) == k) { ++result; } } return result; } }; struct Vs { bool visited; }; struct Ve { }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; Graph<Vs, Ve> g(n * n); g.grid_edges(n); int i = 0; REP(y, n) { string line; cin >> line; REP(x, n) { g.g[i++].visited = (line[x] == '#'); } } cout << g.moves(k) << endl; return 0; } #endif |