Submission #971341

#TimeUsernameProblemLanguageResultExecution timeMemory
971341BodishaTracks in the Snow (BOI13_tracks)C++17
100 / 100
1426 ms679992 KiB
#include <bits/stdc++.h> #define MAX_D 4000 #define MAX_D 4000 using namespace std; int h, w; char meadow[MAX_D][MAX_D]; int component[MAX_D][MAX_D]; vector<int> graph[MAX_D * MAX_D]; int d[MAX_D * MAX_D]; queue<int> q; void floodfill(int x, int y, int cmp) { stack<pair<int, int>> s; component[x][y] = cmp; s.push({x, y}); while(!s.empty()) { pair<int, int> curr = s.top(); s.pop(); if(curr.first + 1 < h && meadow[curr.first][curr.second] == meadow[curr.first + 1][curr.second] && component[curr.first + 1][curr.second] == 0) { component[curr.first + 1][curr.second] = cmp; s.push({curr.first + 1, curr.second}); } if(curr.first - 1 >= 0 && meadow[curr.first][curr.second] == meadow[curr.first - 1][curr.second] && component[curr.first - 1][curr.second] == 0) { component[curr.first - 1][curr.second] = cmp; s.push({curr.first - 1, curr.second}); } if(curr.second + 1 < w && meadow[curr.first][curr.second] == meadow[curr.first][curr.second + 1] && component[curr.first][curr.second + 1] == 0) { component[curr.first][curr.second + 1] = cmp; s.push({curr.first, curr.second + 1}); } if(curr.second - 1 >= 0 && meadow[curr.first][curr.second] == meadow[curr.first][curr.second - 1] && component[curr.first][curr.second - 1] == 0) { component[curr.first][curr.second - 1] = cmp; s.push({curr.first, curr.second - 1}); } } } int main() { string tmps; cin >> h >> w; for(int i = 0; i < h; i++) { cin >> tmps; for(int j = 0; j < w; j++) { meadow[i][j] = tmps[j]; } } int cnt = 1; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(component[i][j] == 0 && meadow[i][j] != '.') { floodfill(i, j, cnt); cnt++; } } } for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(meadow[i][j] == '.') { continue; } if(i + 1 < h && meadow[i + 1][j] != '.' && component[i + 1][j] != component[i][j]) { graph[component[i + 1][j]].push_back(component[i][j]); graph[component[i][j]].push_back(component[i + 1][j]); } if(i - 1 >= 0 && meadow[i - 1][j] != '.' && component[i - 1][j] != component[i][j]) { graph[component[i - 1][j]].push_back(component[i][j]); graph[component[i][j]].push_back(component[i - 1][j]); } if(j + 1 < w && meadow[i][j + 1] != '.' && component[i][j + 1] != component[i][j]) { graph[component[i][j + 1]].push_back(component[i][j]); graph[component[i][j]].push_back(component[i][j + 1]); } if(j - 1 >= 0 && meadow[i][j - 1] != '.' && component[i][j - 1] != component[i][j]) { graph[component[i][j - 1]].push_back(component[i][j]); graph[component[i][j]].push_back(component[i][j - 1]); } } } fill(d, d + cnt, -1); d[component[0][0]] = 0; q.push(component[0][0]); while(!q.empty()) { int curr_node = q.front(); q.pop(); for(auto iter : graph[curr_node]) { if(d[iter] == -1) { d[iter] = d[curr_node] + 1; q.push(iter); } } } int ans = 1; for(int i = 0; i < cnt; i++) { ans = max(ans, d[i] + 1); } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...