#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define tc ll test;cin >> test;while(test--)
#define vi vector<ll>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define INF 1e18
#define MOD 1e9 + 7
#define ff first
#define ss second
#define in >>
#define out <<
#define space << " " <<
#define spacef << " "
#define fo(i,a,b) for(ll i = a; i <= b; i++)
#define nextline out "\n"
#define print(x) for(auto i : x ) cout out i spacef
#define mmax(x,i) x = max(x,i)
#define mmin(x,i) x = min(x,i)
#define N 105
int main() {
fast;
ll n,m;
cin in n in m;
vector<vector<char>> grid(n+5,vector<char>(m+5,'.'));
fo(i,1,n){
fo(j,1,m){
cin in grid[i][j];
}
}
vector<vi> dist(n+5,vi(m+5,INF));
deque<pll> q;
ll ans = -INF;
vi dx = {-1,1,0,0};
vi dy = {0,0,-1,1};
q.push_front({1,1});
dist[1][1] = 1;
while(!q.empty()){
ll a = q.front().first;
ll b = q.front().second;
q.pop_front();
mmax(ans,dist[a][b]);
fo(t,0,3){
ll aa = a+dx[t];
ll bb = b+dy[t];
if(aa >= 1 && aa <= n && bb >= 1 && bb <= m && grid[aa][bb] != '.'){
if(grid[aa][bb] == grid[a][b] && dist[aa][bb] > dist[a][b]){
dist[aa][bb] = dist[a][b];
q.push_front({aa,bb});
}
else if(grid[aa][bb] != grid[a][b] && dist[aa][bb] > dist[a][b] + 1){
dist[aa][bb] = dist[a][b] + 1;
q.push_back({aa,bb});
}
}
}
}
cout out ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
2764 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
10 ms |
2316 KB |
Output is correct |
5 |
Correct |
3 ms |
1088 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
928 KB |
Output is correct |
11 |
Correct |
4 ms |
844 KB |
Output is correct |
12 |
Correct |
7 ms |
1216 KB |
Output is correct |
13 |
Correct |
4 ms |
1100 KB |
Output is correct |
14 |
Correct |
4 ms |
1092 KB |
Output is correct |
15 |
Correct |
19 ms |
2892 KB |
Output is correct |
16 |
Correct |
20 ms |
2876 KB |
Output is correct |
17 |
Correct |
13 ms |
2748 KB |
Output is correct |
18 |
Correct |
10 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1228 KB |
Output is correct |
2 |
Correct |
72 ms |
15804 KB |
Output is correct |
3 |
Correct |
509 ms |
157556 KB |
Output is correct |
4 |
Correct |
133 ms |
37324 KB |
Output is correct |
5 |
Correct |
272 ms |
88760 KB |
Output is correct |
6 |
Correct |
1116 ms |
183824 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
1228 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
72 ms |
15692 KB |
Output is correct |
14 |
Correct |
40 ms |
9288 KB |
Output is correct |
15 |
Correct |
35 ms |
10264 KB |
Output is correct |
16 |
Correct |
36 ms |
6708 KB |
Output is correct |
17 |
Correct |
188 ms |
40320 KB |
Output is correct |
18 |
Correct |
143 ms |
39744 KB |
Output is correct |
19 |
Correct |
125 ms |
37316 KB |
Output is correct |
20 |
Correct |
111 ms |
34196 KB |
Output is correct |
21 |
Correct |
290 ms |
91860 KB |
Output is correct |
22 |
Correct |
297 ms |
88772 KB |
Output is correct |
23 |
Correct |
376 ms |
76480 KB |
Output is correct |
24 |
Correct |
278 ms |
89844 KB |
Output is correct |
25 |
Correct |
766 ms |
157416 KB |
Output is correct |
26 |
Correct |
687 ms |
220996 KB |
Output is correct |
27 |
Correct |
952 ms |
192408 KB |
Output is correct |
28 |
Correct |
1092 ms |
183796 KB |
Output is correct |
29 |
Correct |
1085 ms |
184384 KB |
Output is correct |
30 |
Correct |
1004 ms |
183332 KB |
Output is correct |
31 |
Correct |
931 ms |
101956 KB |
Output is correct |
32 |
Correct |
835 ms |
188512 KB |
Output is correct |