#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int n, k, ile; set < pair <int, int> > S; set < set < pair <int, int> > > S1, S2; int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { char c; scanf(" %c", &c); if(c == '#') { S.insert({i, j}); } } } S1.insert(S); for(int i = 0; i < k; i++) { for(const auto s : S1) { auto x = s; for(auto p : s) { for(int j = 0; j < 4; j++) { auto q = p; q.first += dx[j]; q.second += dy[j]; if(q.first < 1 || q.first > n || q.second < 1 || q.second > n) continue; if(s.find(q) == s.end()) { x.insert(q); S2.insert(x); x.erase(q); } } } } swap(S1, S2); S2.clear(); } printf("%d\n", (int)S1.size()); }
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 | #include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int n, k, ile; set < pair <int, int> > S; set < set < pair <int, int> > > S1, S2; int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { char c; scanf(" %c", &c); if(c == '#') { S.insert({i, j}); } } } S1.insert(S); for(int i = 0; i < k; i++) { for(const auto s : S1) { auto x = s; for(auto p : s) { for(int j = 0; j < 4; j++) { auto q = p; q.first += dx[j]; q.second += dy[j]; if(q.first < 1 || q.first > n || q.second < 1 || q.second > n) continue; if(s.find(q) == s.end()) { x.insert(q); S2.insert(x); x.erase(q); } } } } swap(S1, S2); S2.clear(); } printf("%d\n", (int)S1.size()); } |