CSES Problem Set Counting Rooms 1192 Solution


 

Problem Link:  https://cses.fi/problemset/task/1192

 Solution in C++:

/// La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
/// Abul Hasnat  Tonmoy
/// Department of CSE,23rd batch
/// Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
bool vis[N][N];
int n, m;
char c;
int x[] = {1, -100}, y[] = {001, -1};
void dfs(int a, int b) {
  vis[a][b] = true;
  for (int i = 0; i < 4; i++) {
    int dx = a + x[i];
    int dy = b + y[i];
    if (0 <= dx && dx < n && 0 <= dy && dy < m && vis[dx][dy] != true)
      dfs(dx, dy);
  }
}
int main() {
  int cn = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == '#') vis[i][j] = true;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vis[i][j] == false) {
        dfs(i, j);
        cn++;
      }
    }
  }
  cout << cn << endl;
}

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.