This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int h, w;
vector<vector<char>> grid;
vector<vector<bool>> visited;
// Directions for moving up, down, left, right
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
// Function to perform DFS to mark the connected components
void dfs(int x, int y, char animal) {
visited[x][y] = true;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < h && ny >= 0 && ny < w && !visited[nx][ny] && grid[nx][ny] == animal) {
dfs(nx, ny, animal);
}
}
}
int main() {
// Input reading
cin >> h >> w;
grid.resize(h, vector<char>(w));
visited.resize(h, vector<bool>(w, false));
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> grid[i][j];
}
}
int components = 0;
// Iterate through all cells
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
// If the cell has not been visited and is either 'R' or 'F'
if (!visited[i][j] && grid[i][j] != '.') {
// Start a DFS for this component
dfs(i, j, grid[i][j]);
components++;
}
}
}
// Output the number of components
cout << components << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |