#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define vvb vector<vector<bool>>
#define piii pair<int, pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define inf 987654321
#define pb push_back
#define p push
#define ll long long
#define ull unsigned long long
#define g get
#define dbl double
#define pf push_front
int N, M;
char sn[4005][4005];
int depth[4005][4005];
int ans;
int xa[4] = {0, 0, 1, -1};
int ya[4] = {1, -1, 0, 0};
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> sn[i][j];
deque<pii> q;
q.pb({0, 0});
depth[0][0] = 1;
while (!q.empty())
{
int x = q.front().f, y = q.front().s;
q.pop_front();
ans = max(ans, depth[x][y]);
for (int i = 0; i < 4; i++)
{
int nx = x + xa[i], ny = y + ya[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < M && sn[nx][ny] != '.' && !depth[nx][ny])
{
if (sn[x][y] == sn[nx][ny])
{
depth[nx][ny] = depth[x][y];
q.pf({nx, ny});
}
else
{
depth[nx][ny] = depth[x][y] + 1;
q.pb({nx, ny});
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
5488 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
18 ms |
5228 KB |
Output is correct |
5 |
Correct |
8 ms |
2944 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
1132 KB |
Output is correct |
10 |
Correct |
7 ms |
2540 KB |
Output is correct |
11 |
Correct |
5 ms |
2156 KB |
Output is correct |
12 |
Correct |
11 ms |
3052 KB |
Output is correct |
13 |
Correct |
8 ms |
2924 KB |
Output is correct |
14 |
Correct |
8 ms |
2924 KB |
Output is correct |
15 |
Correct |
26 ms |
5356 KB |
Output is correct |
16 |
Correct |
27 ms |
5484 KB |
Output is correct |
17 |
Correct |
24 ms |
5228 KB |
Output is correct |
18 |
Correct |
17 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
30828 KB |
Output is correct |
2 |
Correct |
138 ms |
15084 KB |
Output is correct |
3 |
Correct |
1145 ms |
73600 KB |
Output is correct |
4 |
Correct |
294 ms |
32748 KB |
Output is correct |
5 |
Correct |
698 ms |
53356 KB |
Output is correct |
6 |
Correct |
1635 ms |
107812 KB |
Output is correct |
7 |
Correct |
24 ms |
32236 KB |
Output is correct |
8 |
Correct |
24 ms |
30828 KB |
Output is correct |
9 |
Correct |
5 ms |
748 KB |
Output is correct |
10 |
Correct |
3 ms |
492 KB |
Output is correct |
11 |
Correct |
24 ms |
31596 KB |
Output is correct |
12 |
Correct |
3 ms |
1644 KB |
Output is correct |
13 |
Correct |
137 ms |
15144 KB |
Output is correct |
14 |
Correct |
85 ms |
10476 KB |
Output is correct |
15 |
Correct |
85 ms |
13164 KB |
Output is correct |
16 |
Correct |
59 ms |
5740 KB |
Output is correct |
17 |
Correct |
357 ms |
28924 KB |
Output is correct |
18 |
Correct |
332 ms |
35840 KB |
Output is correct |
19 |
Correct |
296 ms |
32748 KB |
Output is correct |
20 |
Correct |
255 ms |
25836 KB |
Output is correct |
21 |
Correct |
669 ms |
52716 KB |
Output is correct |
22 |
Correct |
683 ms |
53612 KB |
Output is correct |
23 |
Correct |
666 ms |
43888 KB |
Output is correct |
24 |
Correct |
655 ms |
50028 KB |
Output is correct |
25 |
Correct |
1393 ms |
94444 KB |
Output is correct |
26 |
Correct |
1498 ms |
131196 KB |
Output is correct |
27 |
Correct |
1568 ms |
112688 KB |
Output is correct |
28 |
Correct |
1582 ms |
107636 KB |
Output is correct |
29 |
Correct |
1587 ms |
105448 KB |
Output is correct |
30 |
Correct |
1569 ms |
109928 KB |
Output is correct |
31 |
Correct |
1111 ms |
73512 KB |
Output is correct |
32 |
Correct |
1603 ms |
110832 KB |
Output is correct |