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 pi pair<int, int>
using namespace std;
const int MAXN = 505;
char grid[MAXN][MAXN];
int dist[MAXN][MAXN];
queue<pi> q;
int n, m, ans = 0;
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> grid[i][j];
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// cout << grid[i][j];
// cout << endl;
// }
// cout << endl;
dist[0][0] = 1;
q.push({0, 0});
while (!q.empty())
{
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// cout << dist[i][j] << " ";
// cout << endl;
// }
// cout << endl;
int i = q.front().first, j = q.front().second;
q.pop();
ans = max(ans, dist[i][j]);
if (i + 1 < n)
{
if (grid[i + 1][j] == '.')
{
}
else if (grid[i][j] == grid[i + 1][j])
{
dist[i + 1][j] = min((dist[i + 1][j] == 0 ? INT_MAX : dist[i + 1][j]), dist[i][j]);
q.push({i + 1, j});
}
else if (grid[i][j] != grid[i + 1][j])
{
dist[i + 1][j] = min((dist[i + 1][j] == 0 ? INT_MAX : dist[i + 1][j]), dist[i][j] + 1);
q.push({i + 1, j});
}
}
if (j + 1 < m)
{
if (grid[i][j + 1] == '.')
{
}
else if (grid[i][j] == grid[i][j + 1])
{
dist[i][j + 1] = min((dist[i][j + 1] == 0 ? INT_MAX : dist[i][j + 1]), dist[i][j]);
q.push({i, j + 1});
}
else if (grid[i][j] != grid[i][j + 1])
{
dist[i][j + 1] = min((dist[i][j + 1] == 0 ? INT_MAX : dist[i][j + 1]), dist[i][j] + 1);
q.push({i, j + 1});
}
}
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |