#include<bits/stdc++.h>
using namespace std;
int n, m;
string s[5000];
int dp[5000][5000];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i=0; i<n; i++) {
cin >> s[i];
}
deque<pair<int, int>> q;
q.push_back({0, 0});
dp[0][0] = 1;
int ans = 0;
while(!q.empty()) {
auto me = q.front();
q.pop_front();
ans = max(ans, dp[me.first][me.second]);
for(int to=0; to<4; to++) {
int i = me.first +dx[to], j = me.second + dy[to];
if(i>=0&&i<n&&j>=0&&j<m&&s[i][j]!='.') {
if(dp[i][j]==0) {
if(s[i][j]!=s[me.first][me.second]) {
dp[i][j] = dp[me.first][me.second] + 1;
q.push_back({i, j});
} else {
dp[i][j] = dp[me.first][me.second];
q.push_front({i, j});
}
}
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
624 KB |
Output is correct |
4 |
Correct |
5 ms |
3492 KB |
Output is correct |
5 |
Correct |
2 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
1724 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
4 ms |
2140 KB |
Output is correct |
13 |
Correct |
2 ms |
2140 KB |
Output is correct |
14 |
Correct |
2 ms |
2164 KB |
Output is correct |
15 |
Correct |
7 ms |
3772 KB |
Output is correct |
16 |
Correct |
8 ms |
3932 KB |
Output is correct |
17 |
Correct |
6 ms |
3724 KB |
Output is correct |
18 |
Correct |
5 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15960 KB |
Output is correct |
2 |
Correct |
28 ms |
11924 KB |
Output is correct |
3 |
Correct |
157 ms |
78192 KB |
Output is correct |
4 |
Correct |
52 ms |
29416 KB |
Output is correct |
5 |
Correct |
114 ms |
51544 KB |
Output is correct |
6 |
Correct |
436 ms |
126200 KB |
Output is correct |
7 |
Correct |
8 ms |
16732 KB |
Output is correct |
8 |
Correct |
8 ms |
15996 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
692 KB |
Output is correct |
11 |
Correct |
9 ms |
16476 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
27 ms |
11828 KB |
Output is correct |
14 |
Correct |
15 ms |
8064 KB |
Output is correct |
15 |
Correct |
12 ms |
10332 KB |
Output is correct |
16 |
Correct |
13 ms |
4920 KB |
Output is correct |
17 |
Correct |
71 ms |
25384 KB |
Output is correct |
18 |
Correct |
57 ms |
32208 KB |
Output is correct |
19 |
Correct |
51 ms |
29424 KB |
Output is correct |
20 |
Correct |
37 ms |
22608 KB |
Output is correct |
21 |
Correct |
92 ms |
50616 KB |
Output is correct |
22 |
Correct |
119 ms |
51532 KB |
Output is correct |
23 |
Correct |
146 ms |
42488 KB |
Output is correct |
24 |
Correct |
90 ms |
47444 KB |
Output is correct |
25 |
Correct |
346 ms |
112424 KB |
Output is correct |
26 |
Correct |
233 ms |
137824 KB |
Output is correct |
27 |
Correct |
312 ms |
125664 KB |
Output is correct |
28 |
Correct |
415 ms |
126244 KB |
Output is correct |
29 |
Correct |
413 ms |
124824 KB |
Output is correct |
30 |
Correct |
367 ms |
125408 KB |
Output is correct |
31 |
Correct |
345 ms |
74940 KB |
Output is correct |
32 |
Correct |
292 ms |
125640 KB |
Output is correct |