Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int main() {
- int n, m;
- cin >> n >> m;
- char mat[n][m];
- bool visited[n][m];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- }
- }
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- visited[i][j] = false;
- }
- }
- int rooms = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(mat[i][j] == '.' and !visited[i][j]) {
- rooms++;
- queue<int> Q;
- Q.push(i);
- Q.push(j);
- visited[i][j] = true;
- while(!Q.empty()) { // Q.size() > 0
- int ci = Q.front();
- Q.pop();
- int cj = Q.front();
- Q.pop();
- if(ci + 1 < n and mat[ci + 1][cj] != '#' and !visited[ci + 1][cj]) {
- Q.push(ci + 1);
- Q.push(cj);
- visited[ci + 1][cj] = true;
- }
- if(ci - 1 >= 0 and mat[ci - 1][cj] != '#' and !visited[ci - 1][cj]) {
- Q.push(ci - 1);
- Q.push(cj);
- visited[ci - 1][cj] = true;
- }
- if(cj + 1 < m and mat[ci][cj + 1] != '#' and !visited[ci][cj + 1]) {
- Q.push(ci);
- Q.push(cj + 1);
- visited[ci][cj + 1] = true;
- }
- if(cj - 1 >= 0 and mat[ci][cj - 1] != '#' and !visited[ci][cj - 1]) {
- Q.push(ci);
- Q.push(cj - 1);
- visited[ci][cj - 1] = true;
- }
- }
- }
- }
- }
- cout << rooms << endl;
- return 0;
- }
- /*
- ########
- #..#...#
- ####.#.#
- #..#...#
- ########
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement