Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5 8
- ########
- #..#...#
- ####.#.#
- #..#...#
- ########
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- std::size_t rooms(std::istream &in) {
- std::size_t height;
- std::size_t width;
- std::size_t roomcount{0};
- static constexpr char empty{'.'};
- in >> height >> width;
- if (!in)
- return roomcount;
- std::vector<std::size_t> tracker(width, 0);
- for (auto i{height}; i; --i) {
- std::string row;
- in >> row;
- if (row.size() != width) {
- in.setstate(std::ios::failbit);
- return roomcount;
- }
- for (std::size_t j{0}; j < width; ++j) {
- if (row[j] == empty) {
- // continuation from line above?
- if (tracker[j]) {
- // also from left?
- if (j && tracker[j-1] && (tracker[j-1] != tracker[j])) {
- tracker[j] = tracker[j-1] = std::min(tracker[j], tracker[j-1]);
- --roomcount;
- }
- } else {
- // continuation from left?
- if (j && tracker[j-1]) {
- tracker[j] = tracker[j-1];
- } else {
- tracker[j] = ++roomcount;
- }
- }
- } else {
- tracker[j] = 0;
- }
- }
- }
- return roomcount;
- }
- int main() {
- auto r = rooms(std::cin);
- while (std::cin) {
- std::cout << r << 'n';
- r = rooms(std::cin);
- }
- }
- 5 8
- ########
- #..#...#
- ####.#.#
- #..#...#
- ########
- 9 25
- #########################
- #.#.#.#.#.#.#.#.#.#.#.#.#
- #########################
- #.#.#.#.#.#.#.#.#.#.#.#.#
- #########################
- #.#.#.#.#.#.#.#.#.#.#.#.#
- #########################
- #.#.#.#.#.#.#.#.#.#.#...#
- #########################
- 3 3
- ...
- ...
- ...
- 3 3
- ###
- ...
- ###
- 3 3
- ###
- ###
- ###
- 7 9
- #########
- #.#.#.#.#
- #.#.#.#.#
- #.#...#.#
- #.#####.#
- #.......#
- #########
- 5 8
- ########
- #..#.#.#
- ##.#.#.#
- #..#...#
- ########
- 7 8
- ########
- #..#.#.#
- ##.#.#.#
- #..#...#
- ########
- #..#...#
- ########
- 7 9
- #########
- #.#.#.#.#
- #.#.#.#.#
- #.#.#.#.#
- #.#.#.#.#
- #.......#
- #########
- 7 9
- #########
- #.#.##..#
- #.#.##.##
- #.#.##..#
- #.#...#.#
- #...#...#
- #########
- 7 9
- #########
- #.#.....#
- #.#.###.#
- #.#...#.#
- #.#####.#
- #.......#
- #########
- 7 9
- #########
- #.......#
- #.#####.#
- #.#.#.#.#
- #.#.#.#.#
- #.......#
- #########
- 3
- 47
- 1
- 1
- 0
- 2
- 2
- 4
- 1
- 1
- 1
- 1
Add Comment
Please, Sign In to add comment