이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |