이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> s (h);
cin >> s[0] >> s[1];
vector<vector<pair<int, bool>>> g (h*w);
for (int i = 0; i < h; i++) {
if (i < h-2) {
cin >> s[i+2];
}
for (int j = 0; j < w; j++) {
if (s[i][j] == '.') continue;
if (i != 0 && s[i-1][j] != '.') {
g[i*w+j].push_back({(i-1)*w+j, s[i][j]==s[i-1][j]});
}
if (j != 0 && s[i][j-1] != '.') {
g[i*w+j].push_back({i*w+j-1, s[i][j]==s[i][j-1]});
}
if (i != h-1 && s[i+1][j] != '.') {
g[i*w+j].push_back({(i+1)*w+j, s[i][j]==s[i+1][j]});
}
if (j != w-1 && s[i][j+1] != '.') {
g[i*w+j].push_back({i*w+j+1, s[i][j]==s[i][j+1]});
}
}
if (i > 0) {
s[i-1] = "";
}
}
s.resize(0);
deque<pair<int, int>> q;
vector<bool> odw (h*w, 0);
q.push_back({0, 1});
int maxd = 0;
while (!q.empty()) {
auto x = q.front();
q.pop_front();
if (odw[x.first]) {
continue;
}
maxd = max(maxd, x.second);
odw[x.first] = 1;
for (auto i : g[x.first]) {
if (odw[i.first]) continue;
if (i.second) {
q.push_front({i.first, x.second});
}
else {
q.push_back({i.first, x.second+1});
}
}
}
cout << maxd << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |