Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. //#define int long long
  4. #define double long double
  5. #define pb push_back
  6. #define in insert
  7. #define f first
  8. #define s second
  9. #define all(x) x.begin(), x.end()
  10. #define allr(x) x.rbegin(), x.rend()
  11.  
  12. using namespace std;
  13.  
  14. int n, m;
  15.  
  16. vector<vector<char>> a;
  17. vector<vector<int>> used;
  18.  
  19. int sz = 0;
  20.  
  21. void dfs(pair<int, int> v) {
  22. used[v.f][v.s] = 1;
  23. sz++;
  24. if (v.f + 1 < n) {
  25. v.f++;
  26. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  27. dfs({v.f, v.s});
  28. }
  29. v.f--;
  30. }
  31. if (v.f - 1 >= 0) {
  32. v.f--;
  33. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  34. dfs({v.f, v.s});
  35. }
  36. v.f++;
  37. }
  38. if (v.s + 1 < m) {
  39. v.s++;
  40. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  41. dfs({v.f, v.s});
  42. }
  43. v.s--;
  44. }
  45. if (v.s - 1 >= 0) {
  46. v.s--;
  47. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  48. dfs({v.f, v.s});
  49. }
  50. v.s++;
  51. }
  52. if (v.f % 2 == 1) {
  53. if (v.s + 1 < m) {
  54. v.s++;
  55. if (v.f + 1 < n) {
  56. v.f++;
  57. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  58. dfs({v.f, v.s});
  59. }
  60. v.f--;
  61. }
  62. if (v.f - 1 >= 0) {
  63. v.f--;
  64. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  65. dfs({v.f, v.s});
  66. }
  67. v.f++;
  68. }
  69. v.s--;
  70. }
  71. }
  72. if (v.f % 2 == 0) {
  73. if (v.s - 1 >= 0) {
  74. v.s--;
  75. if (v.f + 1 < n) {
  76. v.f++;
  77. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  78. dfs({v.f, v.s});
  79. }
  80. v.f--;
  81. }
  82. if (v.f - 1 >= 0) {
  83. v.f--;
  84. if (!used[v.f][v.s] && a[v.f][v.s] == '.') {
  85. dfs({v.f, v.s});
  86. }
  87. v.f++;
  88. }
  89. v.s++;
  90. }
  91. }
  92. }
  93.  
  94. int main() {
  95. int x;
  96. cin >> x >> n >> m;
  97. a.resize(n, vector<char> (m));
  98. used.resize(n, vector<int> (m));
  99. for (int i = 0; i < n; i++) {
  100. for (int j = 0; j < m; j++) {
  101. cin >> a[i][j];
  102. }
  103. }
  104. vector<int> sz_ans;
  105. for (int i = 0; i < n; i++) {
  106. for (int j = 0; j < m; j++) {
  107. if (!used[i][j] && a[i][j] == '.') {
  108. sz = 0;
  109. dfs({i, j});
  110. sz_ans.pb(sz);
  111. }
  112. }
  113. }
  114. sort(allr(sz_ans));
  115. int ans = 0;
  116. for (int i = 0; x > 0; i++) {
  117. ans++;
  118. x -= sz_ans[i];
  119. }
  120. cout << ans;
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement