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 n, m;
bool check(int i, int j){
return (i >= 0 && i < n && j >= 0 && j < m);
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
vector<vector<char>>grid(n, vector<char>(m));
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
cin >> grid[i][j];
}
}
vector<vector<int>>dist(n, vector<int>(m, 1e9));
dist[0][0] = 0;
deque<pair<int, int>>q;
q.push_front({0, 0});
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop_front();
for(int i = 0;i < 4;++i){
int cur_x = x + dx[i], cur_y = y + dy[i];
if(check(cur_x, cur_y) && dist[cur_x][cur_y] > dist[x][y] + (grid[x][y] != grid[cur_x][cur_y]) && grid[cur_x][cur_y] != '*'){
if(grid[x][y] != grid[cur_x][cur_y])q.push_back({cur_x, cur_y});
else q.push_front({cur_x, cur_y});
dist[cur_x][cur_y] = dist[x][y] + (grid[x][y] != grid[cur_x][cur_y]);
}
}
}
int mx = 0;
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
if(dist[i][j] != 1e9)mx = max(mx, dist[i][j]);
}
}
cout << mx + 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |