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 n, m;
cin >> n >> m;
vector<vector<char>> karte(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> karte[i][j];
}
}
vector<vector<int>> dist(n, vector<int>(m, 1e9));
vector<pair<int, int>> richtungen = {{1,0}, {-1,0}, {0, 1}, {0, -1}};
dist[0][0] = 0;
deque<pair<int,int>> q;
q.push_back({0, 0});
int maxi = 0;
while (!q.empty()) {
pair<int,int> ort = q.front();
q.pop_front();
for (pair<int, int> richtung: richtungen) {
if (ort.first+richtung.first>= 0 && ort.first+richtung.first < n && ort.second+richtung.second >= 0 && ort.second+richtung.second<m) {
if (karte[ort.first][ort.second] == karte[ort.first+richtung.first][ort.second+richtung.second] && dist[ort.first+richtung.first][ort.second+richtung.second]>dist[ort.first][ort.second]) {
dist[ort.first+richtung.first][ort.second+richtung.second] = dist[ort.first][ort.second];
q.push_front({ort.first+richtung.first, ort.second+richtung.second});
} else if(dist[ort.first+richtung.first][ort.second+richtung.second]>dist[ort.first][ort.second]+1){
dist[ort.first+richtung.first][ort.second+richtung.second] = dist[ort.first][ort.second]+1;
maxi = max(maxi, dist[ort.first][ort.second]+1);
q.push_back({ort.first+richtung.first, ort.second+richtung.second});
}
}
}
}
cout << maxi;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |