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 moves[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int main(){
int n, m;
cin >> n >> m;
char arr[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> arr[i][j];
}
}
int num[n][m];
memset(num, 0, sizeof(num));
num[0][0] = 1;
deque<pair<int, int>> q;
q.push_back({0, 0});
int ans = 0;
while(!q.empty()){
pair<int, int> x = q.front();
q.pop_front();
ans = max(num[x.first][x.second], ans);
for(int i = 0; i < 4; i++){
int x1 = x.first + moves[i][0], y1 = x.second + moves[i][1];
if(x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || num[x1][y1] != 0 || arr[x1][y1] == '.') continue;
if(arr[x1][y1] != arr[x.first][x.second]){
num[x1][y1] = num[x.first][x.second] + 1;
q.push_back({x1, y1});
} else {
num[x1][y1] = num[x.first][x.second];
q.push_back({x1, y1});
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |