#include <bits/stdc++.h>
#define endl "\n"
#define ll long long
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define gett(tup,ind) get<ind>(tup)
#define print(x) for (auto i:x){cout<<i<<" ";}cout<<endl;
using namespace std;
void solve(){
int h,w;cin>>h>>w;
vector <string> arr(h);
for (int i=0;i<h;i++){
cin>>arr[i];
}
vector <vector<int>> dis(h,vector <int> (w,-1));
deque <tuple<int,int,int>> dq;dq.emplace_back(0,0,1);
while (dq.size()){
auto [a,b,c]=dq.front();dq.pop_front();
if (dis[a][b]!=-1)continue;
dis[a][b]=c;
if (a+1<h && arr[a+1][b]!='.'){if (arr[a][b]==arr[a+1][b]){dq.emplace_front(a+1,b,c);}else{dq.emplace_back(a+1,b,c+1);}}
if (b+1<w && arr[a][b+1]!='.'){if (arr[a][b]==arr[a][b+1]){dq.emplace_front(a,b+1,c);}else{dq.emplace_back(a,b+1,c+1);}}
if (a>0 && arr[a-1][b]!='.'){if (arr[a][b]==arr[a-1][b]){dq.emplace_front(a-1,b,c);}else{dq.emplace_back(a-1,b,c+1);}}
if (b>0 && arr[a][b-1]!='.'){if (arr[a][b]==arr[a][b-1]){dq.emplace_front(a,b-1,c);}else{dq.emplace_back(a,b-1,c+1);}}
}
int ans=0;
for (auto i:dis){
ans=max(ans,*max_element(all(i)));
}
cout<<ans<<endl;
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int no_test=1;
//cin>>no_test;
while (no_test--){solve();}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2136 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
2140 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
1180 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
976 KB |
Output is correct |
15 |
Correct |
11 ms |
2100 KB |
Output is correct |
16 |
Correct |
14 ms |
2140 KB |
Output is correct |
17 |
Correct |
7 ms |
1884 KB |
Output is correct |
18 |
Correct |
7 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
40 ms |
10072 KB |
Output is correct |
3 |
Correct |
209 ms |
96596 KB |
Output is correct |
4 |
Correct |
53 ms |
22888 KB |
Output is correct |
5 |
Correct |
127 ms |
54404 KB |
Output is correct |
6 |
Correct |
868 ms |
149740 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
904 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
47 ms |
9872 KB |
Output is correct |
14 |
Correct |
22 ms |
5724 KB |
Output is correct |
15 |
Correct |
15 ms |
6232 KB |
Output is correct |
16 |
Correct |
23 ms |
4376 KB |
Output is correct |
17 |
Correct |
108 ms |
24804 KB |
Output is correct |
18 |
Correct |
55 ms |
24148 KB |
Output is correct |
19 |
Correct |
53 ms |
22872 KB |
Output is correct |
20 |
Correct |
50 ms |
20820 KB |
Output is correct |
21 |
Correct |
125 ms |
56404 KB |
Output is correct |
22 |
Correct |
120 ms |
54356 KB |
Output is correct |
23 |
Correct |
196 ms |
47024 KB |
Output is correct |
24 |
Correct |
104 ms |
54868 KB |
Output is correct |
25 |
Correct |
369 ms |
96528 KB |
Output is correct |
26 |
Correct |
693 ms |
372268 KB |
Output is correct |
27 |
Correct |
735 ms |
246576 KB |
Output is correct |
28 |
Correct |
875 ms |
149536 KB |
Output is correct |
29 |
Correct |
885 ms |
142532 KB |
Output is correct |
30 |
Correct |
791 ms |
186668 KB |
Output is correct |
31 |
Correct |
616 ms |
64552 KB |
Output is correct |
32 |
Correct |
732 ms |
227620 KB |
Output is correct |