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>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
// void setIO(string filename) {
// freopen((filename + ".in").c_str(), "r", stdin);
// freopen((filename + ".out").c_str(), "w", stdout);
// }
int n, m;
vector<vector<char>> snow;
vector<vector<int>> dis;
int ans;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool check(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && snow[x][y] != '.');
}
void bfs(pair<int, int> st) {
dis.assign(n, vector<int>(m, 1e9));
deque<pair<int, int>> dq;
dis[st.first][st.second] = 0;
dq.push_back({st.first, st.second});
while (!dq.empty()) {
auto node = dq.front(); dq.pop_front();
int i = node.first, j = node.second;
ans = max(ans, dis[i][j]);
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (check(x, y) && dis[x][y] == 1e9) {
if (snow[x][y] == snow[i][j]) {
// 0 edge
dis[x][y] = dis[i][j];
dq.push_front({x, y});
} else {
// 1 edge
dis[x][y] = dis[i][j] + 1;
dq.push_back({x, y});
}
}
}
}
}
void solution() {
cin >> n >> m;
snow.resize(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> snow[i][j];
}
}
bfs({0, 0});
cout << ans + 1 << endl;
}
int main() {
// setIO("");
fast;
solution();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |