This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<vector<char> > graph(h, vector<char>(w, 0));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> graph[i][j];
}
}
deque<vector<int> > bfs;
bfs.push_back({0, 0});
vector<vector<int> > dist(h, vector<int>(w, -1));
dist[0][0] = 1;
int ans = 0;
vector<pair<int, int> > vec = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!bfs.empty()) {
auto current = bfs.front();
bfs.pop_front();
ans = max(ans, dist[current[0]][current[1]]);
// cout << current[0] << current[1] << dist[current[0]][current[1]] << endl;
for (pair<int, int> val: vec) {
if (current[0] + val.first >= 0 &&
current[0] + val.first < h &&
current[1] + val.second < w &&
current[1] + val.second >= 0) {
if (graph[current[0] + val.first][current[1] + val.second] == '.') continue;
if (graph[current[0] + val.first][current[1] + val.second] ==
graph[current[0]][current[1]] && dist[current[0] + val.first][current[1] + val.second] == -1) {
dist[current[0] + val.first][current[1] + val.second] = dist[current[0]][current[1]];
bfs.push_front({current[0] + val.first, current[1] + val.second});
} else if (dist[current[0] + val.first][current[1] + val.second] == -1) {
bfs.push_back({current[0] + val.first, current[1] + val.second});
dist[current[0] + val.first][current[1] + val.second] = dist[current[0]][current[1]] + 1;
}
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |