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;
const int MAX_H = 4000;
const int MAX_W = 4000;
vector <string> meadow(MAX_H);
bool diff_animal(int x, int y, int diffx, int diffy){
if (meadow[x][y] != meadow[x + diffx][y + diffy])
return true;
return false;
}
int main()
{
int h, w;
cin >> h >> w;
for (int i = 0; i < h; i++)
cin >> meadow[i];
queue <tuple <int, int, int>> q;
int dis[MAX_H][MAX_W];
bool visited[MAX_H][MAX_W];
for (int i = 0; i < MAX_H; i++){
for (int j = 0; j < MAX_W; j++){
dis[i][j] = INT_MAX;
visited[i][j] = false;
}
}
q.push({0, 0, 0});
dis[0][0] = 0;
int ans = 0;
while (!q.empty()){
int x = get<1>(q.front()), y = get<2>(q.front());
q.pop();
//check up
if (x > 0 && meadow[x-1][y] != '.' && !visited[x-1][y]){
int d = diff_animal(x, y, -1, 0);
if (dis[x-1][y] > dis[x][y] + d){
dis[x-1][y] = dis[x][y] + d;
ans = max(ans, dis[x-1][y]);
q.push({-dis[x-1][y], x-1, y});
visited[x-1][y] = true;
}
}
//check down
if (x < h-1 && meadow[x+1][y] != '.' && !visited[x+1][y]){
int d = diff_animal(x, y, 1, 0);
if (dis[x+1][y] > dis[x][y] + d){
dis[x+1][y] = dis[x][y] + d;
ans = max(ans, dis[x+1][y]);
q.push({-dis[x+1][y], x+1, y});
visited[x+1][y] = true;
}
}
//check left
if (y > 0 && meadow[x][y-1] != '.' && !visited[x][y-1]){
int d = diff_animal(x, y, 0, -1);
if (dis[x][y-1] > dis[x][y] + d){
dis[x][y-1] = dis[x][y] + d;
ans = max(ans, dis[x][y-1]);
q.push({-dis[x][y-1], x, y-1});
visited[x][y-1] = true;
}
}
//check right
if (y < w-1 && meadow[x][y+1] != '.' && !visited[x][y+1]){
int d = diff_animal(x, y, 0, 1);
if (dis[x][y+1] > dis[x][y] + d){
dis[x][y+1] = dis[x][y] + d;
ans = max(ans, dis[x][y+1]);
q.push({-dis[x][y+1], x, y+1});
visited[x][y+1] = true;
}
}
}
cout << ans+ 1 << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |