Submission #1217889

#TimeUsernameProblemLanguageResultExecution timeMemory
1217889nishchay213Tracks in the Snow (BOI13_tracks)C++20
27.40 / 100
1179 ms1114112 KiB
#include<bits/stdc++.h>
using namespace std;

void dfs(vector<string> &v, vector<vector<int>> &vis, int i, int j) {
  vis[i][j] = 1;
  int n = v.size();
  int m = v[0].size();
  if (i+1<n && v[i+1][j]==v[i][j] && vis[i+1][j] == 0){
    dfs(v, vis, i+1, j);
  } 
  if (i-1>=0 && v[i-1][j]==v[i][j] && vis[i-1][j] == 0){
    dfs(v, vis, i-1, j);
  } 
  if (j+1<m && v[i][j+1]==v[i][j] && vis[i][j+1] == 0){
    dfs(v, vis, i, j+1);
  } 
  if (j-1>=0 && v[i][j-1]==v[i][j] && vis[i][j-1] == 0){
    dfs(v, vis, i, j-1);
  } 
}

int main() {
  int n, m;
  cin>>n>>m;
  
  vector<string> v(n);
  for (int i = 0; i<n; i++) cin>>v[i];
    
  int ans = 0;
  vector<vector<int>> vis(n, vector<int> (m));
  queue<pair<int, int>> q;
  for (int i = 0; i<n; i++) {
    for (int j = 0; j<m; j++) {
      if (v[i][j] != '.' && vis[i][j] == 0) {
        ans++;
        dfs(v, vis, i, j);
      }
    }
  }
  cout<<ans;
  
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...