#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ffr(i, s, n) for (int i = s; i < n; i++)
vector<vll> depth;
ll ans = 1;
vll dx = {-1, 1, 0, 0};
vll dy = {0, 0, -1, 1};
bool check_in_side(ll i, ll j, ll n, ll m, vector<string>& snow)
{
return (i >= 0 && i < n && j >= 0 && j < m && snow[i][j] != '.');
}
void bfs(ll n, ll m, vector<string> &snow)
{
// build the queue
deque<pll> q;
// strarting from node 0 , 0;
q.push_front(make_pair(0, 0));
// its depth is 1; but i have a question what about if there is no animal ?
// how we deal with '.'
depth[0][0] = 1;
while (!q.empty())
{
// pop the forntier;
pll forntier = q.front();
q.pop_front();
// make the code easier by taking the x, and y
ll x = forntier.first;
ll y = forntier.second;
// update ans if needed
ans = max(ans, depth[x][y]);
// go through each child
for (int i = 0; i < 4; i++)
{
ll nx = x + dx[i];
ll ny = y + dy[i];
// if they are not visited and inside the boundaries queue them
if (check_in_side(nx, ny, n, m, snow) && depth[nx][ny] == 0)
{
// give high proirity for the similar nodes
if (snow[nx][ny] == snow[x][y])
{
q.push_front(make_pair(nx, ny));
depth[nx][ny] = depth[x][y];
}
else
// lowest priority
{
q.push_back(make_pair(nx, ny));
depth[nx][ny] = depth[x][y] + 1;
}
}
}
}
}
void solve()
{
ll n, m;
cin >> n >> m;
depth.assign(n, vector<ll>(m));
vector<string> snow(n);
ffr(i, 0, n)
{
cin >> snow[i];
}
bfs(n, m , snow);
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
freopen("milkvisits.out", "r", stdin);
solve();
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:86:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen("milkvisits.out", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |