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>
#define el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 1003
#define fi first
#define se second
using namespace std;
int n, m, d[maxn][maxn];
char a[maxn][maxn];
int x[] = {0, 0, 1, -1};
int y[] = {1, -1, 0, 0};
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
f1 (i,n) f1 (j, m)
cin >> a[i][j];
memset(d, -1, sizeof(d));
d[1][1] = 1;
queue <pair<int, int> >q[2];
q[0].push({1, 1});
int cur = 0;
int ans = 0;
while (q[0].size() || q[1].size())
{
int u = q[cur].front().fi, v = q[cur].front().se;
ans = max(ans, d[u][v]);
q[cur].pop();
f0 (i, 4)
{
int U = u + x[i];
int V = v + y[i];
if (a[U][V] == a[u][v] && d[U][V]==-1)
{
d[U][V] = d[u][v];
q[cur].push({U,V});
}
if (a[U][V] == ('B'+'T'-a[u][v]) && d[U][V] == -1)
{
d[U][V] = d[u][v] + 1;
q[1-cur].push({U,V});
}
}
if (q[cur].size() == 0) cur ^= 1;
}
cout << ans;
}
Compilation message (stderr)
zoo.cpp:13:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
13 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |