Guest User

Untitled

a guest
Jul 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. 5 8
  2. ########
  3. #..#...#
  4. ####.#.#
  5. #..#...#
  6. ########
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <string>
  11. #include <algorithm>
  12.  
  13. std::size_t rooms(std::istream &in) {
  14. std::size_t height;
  15. std::size_t width;
  16. std::size_t roomcount{0};
  17. static constexpr char empty{'.'};
  18. in >> height >> width;
  19. if (!in)
  20. return roomcount;
  21. std::vector<std::size_t> tracker(width, 0);
  22. for (auto i{height}; i; --i) {
  23. std::string row;
  24. in >> row;
  25. if (row.size() != width) {
  26. in.setstate(std::ios::failbit);
  27. return roomcount;
  28. }
  29. for (std::size_t j{0}; j < width; ++j) {
  30. if (row[j] == empty) {
  31. // continuation from line above?
  32. if (tracker[j]) {
  33. // also from left?
  34. if (j && tracker[j-1] && (tracker[j-1] != tracker[j])) {
  35. tracker[j] = tracker[j-1] = std::min(tracker[j], tracker[j-1]);
  36. --roomcount;
  37. }
  38. } else {
  39. // continuation from left?
  40. if (j && tracker[j-1]) {
  41. tracker[j] = tracker[j-1];
  42. } else {
  43. tracker[j] = ++roomcount;
  44. }
  45. }
  46. } else {
  47. tracker[j] = 0;
  48. }
  49. }
  50. }
  51. return roomcount;
  52. }
  53.  
  54.  
  55. int main() {
  56. auto r = rooms(std::cin);
  57. while (std::cin) {
  58. std::cout << r << 'n';
  59. r = rooms(std::cin);
  60. }
  61. }
  62.  
  63. 5 8
  64. ########
  65. #..#...#
  66. ####.#.#
  67. #..#...#
  68. ########
  69. 9 25
  70. #########################
  71. #.#.#.#.#.#.#.#.#.#.#.#.#
  72. #########################
  73. #.#.#.#.#.#.#.#.#.#.#.#.#
  74. #########################
  75. #.#.#.#.#.#.#.#.#.#.#.#.#
  76. #########################
  77. #.#.#.#.#.#.#.#.#.#.#...#
  78. #########################
  79. 3 3
  80. ...
  81. ...
  82. ...
  83. 3 3
  84. ###
  85. ...
  86. ###
  87. 3 3
  88. ###
  89. ###
  90. ###
  91. 7 9
  92. #########
  93. #.#.#.#.#
  94. #.#.#.#.#
  95. #.#...#.#
  96. #.#####.#
  97. #.......#
  98. #########
  99. 5 8
  100. ########
  101. #..#.#.#
  102. ##.#.#.#
  103. #..#...#
  104. ########
  105. 7 8
  106. ########
  107. #..#.#.#
  108. ##.#.#.#
  109. #..#...#
  110. ########
  111. #..#...#
  112. ########
  113. 7 9
  114. #########
  115. #.#.#.#.#
  116. #.#.#.#.#
  117. #.#.#.#.#
  118. #.#.#.#.#
  119. #.......#
  120. #########
  121. 7 9
  122. #########
  123. #.#.##..#
  124. #.#.##.##
  125. #.#.##..#
  126. #.#...#.#
  127. #...#...#
  128. #########
  129. 7 9
  130. #########
  131. #.#.....#
  132. #.#.###.#
  133. #.#...#.#
  134. #.#####.#
  135. #.......#
  136. #########
  137. 7 9
  138. #########
  139. #.......#
  140. #.#####.#
  141. #.#.#.#.#
  142. #.#.#.#.#
  143. #.......#
  144. #########
  145.  
  146. 3
  147. 47
  148. 1
  149. 1
  150. 0
  151. 2
  152. 2
  153. 4
  154. 1
  155. 1
  156. 1
  157. 1
Add Comment
Please, Sign In to add comment