#include<bits/stdc++.h>
using namespace std;
#define pi pair<int,int>
#define ll long long
#define pb push_back
#define pf push_front
const int MOD = 1e9+7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main()
{
cin.tie(0)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
string s[n];
for(int i=0; i<n; i++)
cin >> s[i];
deque<pi> q;
vector<vector<int>> d(n, vector<int>(m));
vector<vector<bool>> vis(n, vector<bool>(m));
d[0][0] = 1;
q.pb({0, 0});
int ans = 0;
while(q.size()){
pi u = q.front();
q.pop_front();
if(vis[u.first][u.second]) continue;
vis[u.first][u.second] = true;
ans = max(ans, d[u.first][u.second]);
for(int i=0; i<4; i++){
int new_x = u.first + dx[i];
int new_y = u.second + dy[i];
if(new_x < 0 or new_x >= n or new_y < 0 or new_y >= m) continue;
if(s[new_x][new_y] == s[u.first][u.second]){
d[new_x][new_y] = d[u.first][u.second];
q.pf({new_x, new_y});
}else if(s[new_x][new_y] != '.'){
d[new_x][new_y] = d[u.first][u.second] + 1;
q.pb({new_x, new_y});
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2136 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
1952 KB |
Output is correct |
5 |
Correct |
2 ms |
996 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
856 KB |
Output is correct |
11 |
Correct |
3 ms |
1012 KB |
Output is correct |
12 |
Correct |
6 ms |
1336 KB |
Output is correct |
13 |
Correct |
2 ms |
856 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
12 ms |
1884 KB |
Output is correct |
16 |
Correct |
15 ms |
2140 KB |
Output is correct |
17 |
Correct |
8 ms |
1880 KB |
Output is correct |
18 |
Correct |
8 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
43 ms |
10108 KB |
Output is correct |
3 |
Correct |
224 ms |
97364 KB |
Output is correct |
4 |
Correct |
53 ms |
23124 KB |
Output is correct |
5 |
Correct |
153 ms |
55016 KB |
Output is correct |
6 |
Correct |
866 ms |
132992 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1236 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
472 KB |
Output is correct |
13 |
Correct |
42 ms |
10076 KB |
Output is correct |
14 |
Correct |
26 ms |
5980 KB |
Output is correct |
15 |
Correct |
13 ms |
6456 KB |
Output is correct |
16 |
Correct |
24 ms |
4444 KB |
Output is correct |
17 |
Correct |
111 ms |
25172 KB |
Output is correct |
18 |
Correct |
58 ms |
24676 KB |
Output is correct |
19 |
Correct |
51 ms |
23124 KB |
Output is correct |
20 |
Correct |
53 ms |
21536 KB |
Output is correct |
21 |
Correct |
132 ms |
56892 KB |
Output is correct |
22 |
Correct |
161 ms |
55020 KB |
Output is correct |
23 |
Correct |
215 ms |
47632 KB |
Output is correct |
24 |
Correct |
125 ms |
55676 KB |
Output is correct |
25 |
Correct |
377 ms |
97456 KB |
Output is correct |
26 |
Correct |
582 ms |
270540 KB |
Output is correct |
27 |
Correct |
701 ms |
196532 KB |
Output is correct |
28 |
Correct |
855 ms |
131064 KB |
Output is correct |
29 |
Correct |
808 ms |
126128 KB |
Output is correct |
30 |
Correct |
767 ms |
155256 KB |
Output is correct |
31 |
Correct |
668 ms |
62876 KB |
Output is correct |
32 |
Correct |
632 ms |
183388 KB |
Output is correct |