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 fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 4e3 + 5;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char a[N][N];
int dis[N][N];
int n,m,ans;
void bfs(int i, int j)
{
deque <pair <int, int>> q;
q.push_back(mp(i, j));
dis[i][j] = 0;
while (q.size())
{
pair <int, int> cur = q.front();
q.pop_front();
int x = cur.fi;
int y = cur.se;
for (int dir = 0; dir < 4; dir++)
{
int u = x + dx[dir];
int v = y + dy[dir];
if (min(u, v) > 0 && u <= n && v <= m && dis[u][v] < 0)
{
if (a[u][v] == a[x][y])
{
q.push_front(mp(u, v));
dis[u][v] = dis[x][y];
} else {
q.push_back(mp(u, v));
dis[u][v] = dis[x][y] + 1;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("file.inp","r",stdin);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
for (int j = 1; j <= m; j++)
a[i][j] = s[j - 1];
}
memset(dis, -1, sizeof(dis));
bfs(n, m);
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans = max(ans, dis[i][j]);
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |