51 problems solved
This commit is contained in:
44
CSES - CSES Problem Set/Graph_Algorithms/Counting_Rooms.cpp
Normal file
44
CSES - CSES Problem Set/Graph_Algorithms/Counting_Rooms.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// Counting Rooms
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void dfs(vector<vector<vector<pair<int, int>>>> &m, pair<int, int> s, vector<vector<bool>> &v) {
|
||||
v[s.first][s.second] = true;
|
||||
for (auto e: m[s.first][s.second]) {
|
||||
if (!v[e.first][e.second]) dfs(m, e, v);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, m; cin >> n >> m;
|
||||
// map<pair<int, int>, vector<pair<int, int>>> map1;
|
||||
vector<vector<vector<pair<int, int>>>> map1(n, vector<vector<pair<int, int>>>(m));
|
||||
vector<string> in;
|
||||
for (int i{}; i < n; i++) {
|
||||
string s; cin >> s; in.push_back(s);
|
||||
}
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j{}; j < m; j++) {
|
||||
if (in[i][j] == '#') continue;
|
||||
if (i > 0 && in[i-1][j] == '.') map1[i][j].push_back({i-1, j});
|
||||
if (i < n - 1 && in[i+1][j] == '.') map1[i][j].push_back({i+1, j});
|
||||
if (j > 0 && in[i][j-1] == '.') map1[i][j].push_back({i, j-1});
|
||||
if (j < m - 1 && in[i][j+1] == '.') map1[i][j].push_back({i, j+1});
|
||||
}
|
||||
}
|
||||
// set<pair<int, int>> v;
|
||||
vector<vector<bool>> v(n, vector<bool>(m, false));
|
||||
int count = 0;
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j{}; j < m; j++) {
|
||||
if (in[i][j] == '#') continue;
|
||||
if (v[i][j]) continue;
|
||||
count++;
|
||||
dfs(map1, {i, j}, v);
|
||||
}
|
||||
}
|
||||
cout << count << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user