Submission #1082677

#TimeUsernameProblemLanguageResultExecution timeMemory
1082677Grothendieck_505Tracks in the Snow (BOI13_tracks)C++14
0 / 100
829 ms1048576 KiB
#include "bits/stdc++.h" using namespace std; vector<int> dx = {0, 0, 1, -1}; vector<int> dy = {1, -1, 0, 0}; int h, w; void dfs(int x, int y, int comp, vector<vector<int>> &visited, vector<vector<char>> &v, set<char> &s, int &animal) { if (x < 0 || x >= h || y < 0 || y >= w || visited[x][y] || v[x][y] == '.') { return; } visited[x][y] = comp; s.insert(v[x][y]); animal = max(animal, (int)s.size()); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; dfs(nx, ny, comp, visited, v, s, animal); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w; vector<vector<int>> visited(h, vector<int>(w, 0)); vector<vector<char>> v(h, vector<char>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> v[i][j]; } } int comp = 0; int ans = 0; // To store the total sum of distinct animals in all components for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (!visited[i][j] && v[i][j] != '.') { set<char> s; int animal = 0; dfs(i, j, ++comp, visited, v, s, animal); ans += animal; // Summing up the distinct animals in each component } } } cout << comp << " " << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...