Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <array>
- #include <vector>
- /*
- 5 8
- ....##..
- ..##.##.
- ###...##
- #.#..###
- #..####.
- */
- struct Coord {
- int i;
- int j;
- };
- int CountReachableFrom(std::vector<std::vector<bool>>& field, const Coord& start) {
- const int n = field.size();
- const int m = field.at(0).size();
- std::queue<Coord> queue;
- queue.push(start);
- field[start.i][start.j] = true;
- int count = 1;
- while (!queue.empty()) {
- const Coord current = queue.front();
- queue.pop();
- for (const Coord delta : std::vector<Coord>{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}) {
- const Coord next{current.i + delta.i, current.j + delta.j};
- if (next.i < 0 || next.j < 0 || next.i >= n || next.j >= m) {
- continue;
- }
- if (field[next.i][next.j] == false) {
- field[next.i][next.j] = true;
- queue.push(next);
- ++count;
- }
- }
- }
- return count;
- }
- int main(int, const char**) {
- int n, m;
- std::cin >> n >> m;
- std::string unused;
- std::getline(std::cin, unused);
- std::vector<std::vector<bool>> field(n, std::vector<bool>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- char ch;
- std::cin >> ch;
- if (ch == '#') {
- field[i][j] = true;
- } else {
- field[i][j] = false;
- }
- }
- std::getline(std::cin, unused);
- }
- int answer = CountReachableFrom(field, {1, 4});
- std::cout << answer << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement