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, -1, 0, 0}, y[] = {0, 0, 1, -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