#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define MOD 1000000007
#define vi vector<int>
#define pi pair<int, int>
char g[4005][4005];
int dist[4005][4005];
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int h, w, ans = 1;
int main()
{
//freopen("13_tracks.in", "r", stdin);
//freopen("13_tracks.out", "w", stdout);
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> h >> w;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
cin >> g[i][j];
deque<pi> q;
q.push_front({1, 1});
memset(dist, 0x3f, sizeof(dist));
//cout << dist[1][2] << endl;
dist[1][1] = 1;
while (!q.empty())
{
pi t = q.front(); q.pop_front();
int tx = t.first, ty = t.second;
ans = max(ans, dist[tx][ty]);
for (int i = 0; i < 4; ++i)
{
int x = tx + dx[i], y = ty + dy[i];
if (x < 1 || x > h || y < 1 || y > w || g[x][y] == '.') continue;
if (dist[x][y] > dist[tx][ty] + (g[x][y] != g[tx][ty]))
{
dist[x][y] = dist[tx][ty] + (g[x][y] != g[tx][ty]);
if (g[x][y] == g[tx][ty])
q.push_front({x, y});
else q.push_back({x, y});
}
}
}
/*
for (int i = 1; i <= h; ++i)
{
for (int j = 1; j <= w; ++j)
cout << dist[i][j] << " ";
cout << endl;
}
*/
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
65516 KB |
Output is correct |
2 |
Correct |
32 ms |
63212 KB |
Output is correct |
3 |
Correct |
32 ms |
63340 KB |
Output is correct |
4 |
Correct |
40 ms |
65516 KB |
Output is correct |
5 |
Correct |
35 ms |
64364 KB |
Output is correct |
6 |
Correct |
32 ms |
63212 KB |
Output is correct |
7 |
Correct |
33 ms |
63340 KB |
Output is correct |
8 |
Correct |
32 ms |
63340 KB |
Output is correct |
9 |
Correct |
33 ms |
63468 KB |
Output is correct |
10 |
Correct |
34 ms |
64108 KB |
Output is correct |
11 |
Correct |
34 ms |
64128 KB |
Output is correct |
12 |
Correct |
37 ms |
64364 KB |
Output is correct |
13 |
Correct |
40 ms |
64492 KB |
Output is correct |
14 |
Correct |
35 ms |
64492 KB |
Output is correct |
15 |
Correct |
44 ms |
65408 KB |
Output is correct |
16 |
Correct |
46 ms |
65388 KB |
Output is correct |
17 |
Correct |
45 ms |
65260 KB |
Output is correct |
18 |
Correct |
40 ms |
65388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
78188 KB |
Output is correct |
2 |
Correct |
87 ms |
69612 KB |
Output is correct |
3 |
Correct |
403 ms |
94444 KB |
Output is correct |
4 |
Correct |
143 ms |
74220 KB |
Output is correct |
5 |
Correct |
254 ms |
83820 KB |
Output is correct |
6 |
Correct |
869 ms |
108280 KB |
Output is correct |
7 |
Correct |
44 ms |
78828 KB |
Output is correct |
8 |
Correct |
40 ms |
78188 KB |
Output is correct |
9 |
Correct |
39 ms |
63212 KB |
Output is correct |
10 |
Correct |
34 ms |
63212 KB |
Output is correct |
11 |
Correct |
41 ms |
78572 KB |
Output is correct |
12 |
Correct |
33 ms |
63724 KB |
Output is correct |
13 |
Correct |
86 ms |
69612 KB |
Output is correct |
14 |
Correct |
62 ms |
67564 KB |
Output is correct |
15 |
Correct |
62 ms |
67948 KB |
Output is correct |
16 |
Correct |
59 ms |
65388 KB |
Output is correct |
17 |
Correct |
179 ms |
75116 KB |
Output is correct |
18 |
Correct |
153 ms |
74860 KB |
Output is correct |
19 |
Correct |
137 ms |
74220 KB |
Output is correct |
20 |
Correct |
124 ms |
73324 KB |
Output is correct |
21 |
Correct |
247 ms |
84332 KB |
Output is correct |
22 |
Correct |
262 ms |
83692 KB |
Output is correct |
23 |
Correct |
307 ms |
80620 KB |
Output is correct |
24 |
Correct |
255 ms |
84460 KB |
Output is correct |
25 |
Correct |
623 ms |
94444 KB |
Output is correct |
26 |
Correct |
565 ms |
139216 KB |
Output is correct |
27 |
Correct |
738 ms |
121032 KB |
Output is correct |
28 |
Correct |
860 ms |
108244 KB |
Output is correct |
29 |
Correct |
875 ms |
106464 KB |
Output is correct |
30 |
Correct |
793 ms |
111984 KB |
Output is correct |
31 |
Correct |
634 ms |
86380 KB |
Output is correct |
32 |
Correct |
681 ms |
109860 KB |
Output is correct |