이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
/*
Consider each connected component as one node in the graph.
Consider adjacent connected components as having an edge between them
Res = max distance between cc(1, 1) and any other cc
*/
int H, W;
int p(int i, int j)
{
return (W+2)*i + j;
}
int main()
{
cin >> H >> W;
char c[(H+2)*(W+2)];
for(int i = 0; i <= H; i++) c[p(i, 0)] = c[p(i, W+1)] = '.';
for(int j = 0; j <= W; j++) c[p(0, j)] = c[p(H+1, j)] = '.';
for(int i = 1; i <= H; i++)
{
for(int j = 1; j <= W; j++)
{
cin >> c[p(i, j)];
}
}
int temp;
vector<int> res((H+2)*(W+2), 1e9);
res[p(1, 1)] = 0;
deque<int> tbv;
tbv.push_back(p(1, 1));
int final_res = 0;
while(!tbv.empty())
{
temp = tbv.front();
tbv.pop_front();
final_res = max(final_res, res[temp]);
for(int k: {temp+1, temp-1, temp+W+2, temp-W-2})
{
if(c[k] == '.') continue;
if(res[k] != 1e9) continue;
if(c[temp] != c[k])
{
res[k] = res[temp] + 1;
tbv.push_back(k);
}
else
{
res[k] = res[temp];
tbv.push_front(k);
}
}
}
cout << final_res + 1 << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |