제출 #971312

#제출 시각아이디문제언어결과실행 시간메모리
971312BodishaTracks in the Snow (BOI13_tracks)C++17
67.19 / 100
1235 ms1048576 KiB
#include <bits/stdc++.h> #define MAX_D 4001 #define MAX_D 4001 using namespace std; int h, w; char meadow[MAX_D][MAX_D]; int component[MAX_D][MAX_D]; unordered_set<int> graph[MAX_D * MAX_D]; void floodfill(int x, int y, int cmp) { component[x][y] = cmp; if(x + 1 < h && meadow[x + 1][y] == meadow[x][y] && component[x + 1][y] == 0) { floodfill(x + 1, y, cmp); } if(x - 1 >= 0 && meadow[x - 1][y] == meadow[x][y] && component[x - 1][y] == 0) { floodfill(x - 1, y, cmp); } if(y + 1 < w && meadow[x][y + 1] == meadow[x][y] && component[x][y + 1] == 0) { floodfill(x, y + 1, cmp); } if(y - 1 >= 0 && meadow[x][y - 1] == meadow[x][y] && component[x][y - 1] == 0) { floodfill(x, y - 1, cmp); } } 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]].count(component[i][j]) == 0) { graph[component[i + 1][j]].insert(component[i][j]); graph[component[i][j]].insert(component[i + 1][j]); } if(i - 1 >= 0 && meadow[i - 1][j] != '.' && component[i - 1][j] != component[i][j] && graph[component[i - 1][j]].count(component[i][j]) == 0) { graph[component[i - 1][j]].insert(component[i][j]); graph[component[i][j]].insert(component[i - 1][j]); } if(j + 1 < w && meadow[i][j + 1] != '.' && component[i][j + 1] != component[i][j] && graph[component[i][j + 1]].count(component[i][j]) == 0) { graph[component[i][j + 1]].insert(component[i][j]); graph[component[i][j]].insert(component[i][j + 1]); } if(j - 1 >= 0 && meadow[i][j - 1] != '.' && component[i][j - 1] != component[i][j] && graph[component[i][j - 1]].count(component[i][j]) == 0) { graph[component[i][j - 1]].insert(component[i][j]); graph[component[i][j]].insert(component[i][j - 1]); } } } int d[cnt]; fill(d, d + cnt, -1); queue<pair<int, int>> q; d[component[0][0]] = 0; q.push({component[0][0], 0}); while(!q.empty()) { pair<int, int> curr_node = q.front(); q.pop(); for(auto iter : graph[curr_node.first]) { if(d[iter] == -1) { d[iter] = curr_node.second + 1; q.push({iter, curr_node.second + 1}); } } } 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...