이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
set<pair<int, int>> edges;
queue<int> q;
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] && edges.count({component[i][j], component[i + 1][j]}) == 0) {
graph[component[i + 1][j]].push_back(component[i][j]);
graph[component[i][j]].push_back(component[i + 1][j]);
edges.insert({component[i + 1][j], component[i][j]});
edges.insert({component[i][j], component[i + 1][j]});
}
if(i - 1 >= 0 && meadow[i - 1][j] != '.' && component[i - 1][j] != component[i][j] && edges.count({component[i][j], component[i - 1][j]}) == 0) {
graph[component[i - 1][j]].push_back(component[i][j]);
graph[component[i][j]].push_back(component[i - 1][j]);
edges.insert({component[i - 1][j], component[i][j]});
edges.insert({component[i][j], component[i - 1][j]});
}
if(j + 1 < w && meadow[i][j + 1] != '.' && component[i][j + 1] != component[i][j] && edges.count({component[i][j], component[i][j + 1]}) == 0) {
graph[component[i][j + 1]].push_back(component[i][j]);
graph[component[i][j]].push_back(component[i][j + 1]);
edges.insert({component[i][j], component[i][j + 1]});
edges.insert({component[i][j + 1], component[i][j]});
}
if(j - 1 >= 0 && meadow[i][j - 1] != '.' && component[i][j - 1] != component[i][j] && edges.count({component[i][j], component[i][j - 1]}) == 0) {
graph[component[i][j - 1]].push_back(component[i][j]);
graph[component[i][j]].push_back(component[i][j - 1]);
edges.insert({component[i][j], component[i][j - 1]});
edges.insert({component[i][j - 1], component[i][j]});
}
}
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |