#include <bits/stdc++.h>
using namespace std;
int dx[4]{0, 0, 1, -1}, dy[4]{1, -1, 0, 0};
int dist[4000][4000];
int w, h;
bool inside(int i, int j)
{
return (0 <= i && i < w && 0 <= j && j < h);
}
int main()
{
cin >> w >> h;
vector<string> mp(4000);
for (int i = 0; i < w; i++)
{
cin >> mp[i];
}
int ans = 1;
deque<pair<int, int>> q;
dist[0][0] = 1;
q.push_back({0, 0});
while (!q.empty())
{
auto [x, y] = q.front();
q.pop_front();
for (int i = 0; i < 4; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (inside(newx, newy) && dist[newx][newy] == 0 && mp[newx][newy] != '.')
{
if (mp[newx][newy] == mp[x][y])
{
dist[newx][newy] = dist[x][y];
q.push_front({newx, newy});
}
else
{
dist[newx][newy] = dist[x][y] + 1;
ans = max(ans, dist[newx][newy]);
q.push_back({newx, newy});
}
}
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4052 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
6 ms |
3500 KB |
Output is correct |
5 |
Correct |
3 ms |
1984 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
696 KB |
Output is correct |
9 |
Correct |
1 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
1728 KB |
Output is correct |
11 |
Correct |
4 ms |
1492 KB |
Output is correct |
12 |
Correct |
5 ms |
2128 KB |
Output is correct |
13 |
Correct |
5 ms |
2004 KB |
Output is correct |
14 |
Correct |
5 ms |
1988 KB |
Output is correct |
15 |
Correct |
13 ms |
3828 KB |
Output is correct |
16 |
Correct |
12 ms |
3976 KB |
Output is correct |
17 |
Correct |
10 ms |
3908 KB |
Output is correct |
18 |
Correct |
6 ms |
3508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15780 KB |
Output is correct |
2 |
Correct |
50 ms |
12564 KB |
Output is correct |
3 |
Correct |
358 ms |
87784 KB |
Output is correct |
4 |
Correct |
95 ms |
32428 KB |
Output is correct |
5 |
Correct |
210 ms |
53260 KB |
Output is correct |
6 |
Correct |
606 ms |
121964 KB |
Output is correct |
7 |
Correct |
16 ms |
16520 KB |
Output is correct |
8 |
Correct |
10 ms |
15820 KB |
Output is correct |
9 |
Correct |
4 ms |
828 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
10 ms |
16212 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
51 ms |
12620 KB |
Output is correct |
14 |
Correct |
28 ms |
8684 KB |
Output is correct |
15 |
Correct |
29 ms |
11172 KB |
Output is correct |
16 |
Correct |
22 ms |
4916 KB |
Output is correct |
17 |
Correct |
134 ms |
28548 KB |
Output is correct |
18 |
Correct |
116 ms |
35416 KB |
Output is correct |
19 |
Correct |
96 ms |
32416 KB |
Output is correct |
20 |
Correct |
82 ms |
25436 KB |
Output is correct |
21 |
Correct |
227 ms |
52204 KB |
Output is correct |
22 |
Correct |
220 ms |
53212 KB |
Output is correct |
23 |
Correct |
254 ms |
43404 KB |
Output is correct |
24 |
Correct |
202 ms |
49400 KB |
Output is correct |
25 |
Correct |
465 ms |
108720 KB |
Output is correct |
26 |
Correct |
526 ms |
130612 KB |
Output is correct |
27 |
Correct |
563 ms |
127044 KB |
Output is correct |
28 |
Correct |
654 ms |
121940 KB |
Output is correct |
29 |
Correct |
578 ms |
119728 KB |
Output is correct |
30 |
Correct |
630 ms |
123980 KB |
Output is correct |
31 |
Correct |
445 ms |
73008 KB |
Output is correct |
32 |
Correct |
531 ms |
124960 KB |
Output is correct |