Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- //#define int long long
- #define double long double
- #define pb push_back
- #define in insert
- #define f first
- #define s second
- #define all(x) x.begin(), x.end()
- #define allr(x) x.rbegin(), x.rend()
- using namespace std;
- int n, m;
- vector<vector<char>> a;
- vector<vector<int>> used;
- int sz = 0;
- void dfs(pair<int, int> v) {
- used[v.f][v.s] = 1;
- sz++;
- if (v.f + 1 < n) {
- v.f++;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f--;
- }
- if (v.f - 1 >= 0) {
- v.f--;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f++;
- }
- if (v.s + 1 < m) {
- v.s++;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.s--;
- }
- if (v.s - 1 >= 0) {
- v.s--;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.s++;
- }
- if (v.f % 2 == 1) {
- if (v.s + 1 < m) {
- v.s++;
- if (v.f + 1 < n) {
- v.f++;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f--;
- }
- if (v.f - 1 >= 0) {
- v.f--;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f++;
- }
- v.s--;
- }
- }
- if (v.f % 2 == 0) {
- if (v.s - 1 >= 0) {
- v.s--;
- if (v.f + 1 < n) {
- v.f++;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f--;
- }
- if (v.f - 1 >= 0) {
- v.f--;
- if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
- dfs({v.f, v.s});
- }
- v.f++;
- }
- v.s++;
- }
- }
- }
- int main() {
- int x;
- cin >> x >> n >> m;
- a.resize(n, vector<char> (m));
- used.resize(n, vector<int> (m));
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> a[i][j];
- }
- }
- vector<int> sz_ans;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (!used[i][j] && a[i][j] == '.') {
- sz = 0;
- dfs({i, j});
- sz_ans.pb(sz);
- }
- }
- }
- sort(allr(sz_ans));
- int ans = 0;
- for (int i = 0; x > 0; i++) {
- ans++;
- x -= sz_ans[i];
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement