이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int SIZE_OF = 4e3;
const char VISITED = '*';
const int VISUAL = 4;
int n, m;
int dl[] = { -1, 0, 1, 0 };
int dc[] = { 0, 1, 0, -1 };
char input[2 + SIZE_OF][2 + SIZE_OF];
int depth[1 + SIZE_OF][1 + SIZE_OF];
void solve ( pair<int, int> start ) {
deque<pair<int, int>> bfs_q;
bfs_q.push_front ( start );
depth[start.first][start.second] = 1;
int answer = 1;
while ( !bfs_q.empty () ) {
start = bfs_q.front (); bfs_q.pop_front ();
answer = max ( answer, depth[start.first][start.second] );
int line = start.first, column = start.second;
for ( int dir = 0; dir < VISUAL; dir ++ ) {
int next_line = line + dl[dir];
int next_col = column + dc[dir];
if ( depth[next_line][next_col] == 0 && input[next_line][next_col] != '.' && input[next_line][next_col] != VISITED ) {
if ( input[line][column] == input[next_line][next_col] ) {
depth[next_line][next_col] = depth[line][column];
bfs_q.push_front ( {next_line, next_col} );
}
else {
depth[next_line][next_col] = depth[line][column] + 1;
bfs_q.push_back ( {next_line, next_col} );
}
}
}
}
cout << answer;
}
int main()
{
cin >> n >> m; cin.get ();
for ( int line = 1; line <= n; line ++ ) {
for ( int column = 1; column <= m; column ++ )
input[line][column] = cin.get ();
cin.get ();
}
for ( int line = 0; line < n + 2; line ++ )
input[line][0] = input[line][m + 1] = VISITED;
for ( int column = 0; column < m + 2; column ++ )
input[0][column] = input[n + 1][column] = VISITED;
solve ( {1, 1} );
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |