#include <bits/stdc++.h>
using namespace std;
int drow[4] = {0, 1, 0, -1};
int dcol[4] = {1, 0, -1, 0};
const int MAX_W = 4000;
int depth[MAX_W][MAX_W]; // 0-indexed
char snow[MAX_W][MAX_W]; // 0-indexed
int h, w;
bool check(int row, int col)
{
return row >= 0 && row < h && col >= 0 && col < w && !depth[row][col] && snow[row][col] != '.';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
cin >> snow[i][j];
}
}
int min_tracks = 1;
deque<int> deq = {0, 0};
depth[0][0] = 1;
while (deq.size())
{
int row = deq.front();
deq.pop_front();
int col = deq.front();
deq.pop_front();
for (int i = 0; i < 4; ++i)
{
int new_row = row + drow[i];
int new_col = col + dcol[i];
if (check(new_row, new_col))
{
if (snow[row][col] == snow[new_row][new_col])
{
depth[new_row][new_col] = depth[row][col];
deq.push_front(new_col);
deq.push_front(new_row);
}
else
{
depth[new_row][new_col] = depth[row][col] + 1;
min_tracks = max(min_tracks, depth[new_row][new_col]);
deq.push_back(new_row);
deq.push_back(new_col);
}
}
}
}
cout << min_tracks << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |