Submission #1082680

#TimeUsernameProblemLanguageResultExecution timeMemory
1082680Grothendieck_505Tracks in the Snow (BOI13_tracks)C++14
29.58 / 100
1051 ms793092 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...