#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
#define ll long long
#define int long long
#define f first
#define s second
#define ld long double
#define mod 1000000007
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
double distance(int x1,int y1,int x2,int y2)
{
return abs(x1-x2)*abs(x1-x2)+abs(y1-y2)*abs(y1-y2);
}
int n,m;
string snow[4003];
int vis[4003][4003];
bool valid(int i,int j){
if(i>=0 && j>=0 && i<n && j<m && snow[i][j]!='.' && vis[i][j]==0){
return 1;
}
return 0;
}
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("nocross.in","r",stdin);
//freopen("nocross.out","w",stdout);
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>snow[i];
}
deque<pair<int,int> > q;
q.push_front({0,0});
vis[0][0]=1;
int ans=1;
while(!q.empty()){
int i=q.front().f,j=q.front().s;
q.pop_front();
ans=max(ans,vis[i][j]);
for(int k=0;k<4;k++){
int x=i+dx[k],y=j+dy[k];
if(valid(x,y)){
if(snow[x][y]==snow[i][j]){
vis[x][y]=vis[i][j];
q.push_front({x,y});
}
else{
vis[x][y]=vis[i][j]+1;
q.push_back({x,y});
}
}
}
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4812 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
604 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 |
3 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
1880 KB |
Output is correct |
12 |
Correct |
4 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
7 ms |
4544 KB |
Output is correct |
16 |
Correct |
9 ms |
4952 KB |
Output is correct |
17 |
Correct |
6 ms |
4696 KB |
Output is correct |
18 |
Correct |
5 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16208 KB |
Output is correct |
2 |
Correct |
28 ms |
15220 KB |
Output is correct |
3 |
Correct |
150 ms |
92084 KB |
Output is correct |
4 |
Correct |
52 ms |
43092 KB |
Output is correct |
5 |
Correct |
113 ms |
68752 KB |
Output is correct |
6 |
Correct |
532 ms |
184156 KB |
Output is correct |
7 |
Correct |
8 ms |
17248 KB |
Output is correct |
8 |
Correct |
8 ms |
16220 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
7 ms |
16476 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
28 ms |
15184 KB |
Output is correct |
14 |
Correct |
16 ms |
9956 KB |
Output is correct |
15 |
Correct |
13 ms |
14416 KB |
Output is correct |
16 |
Correct |
14 ms |
6484 KB |
Output is correct |
17 |
Correct |
74 ms |
33028 KB |
Output is correct |
18 |
Correct |
69 ms |
47700 KB |
Output is correct |
19 |
Correct |
49 ms |
43092 KB |
Output is correct |
20 |
Correct |
38 ms |
27296 KB |
Output is correct |
21 |
Correct |
91 ms |
60380 KB |
Output is correct |
22 |
Correct |
91 ms |
68824 KB |
Output is correct |
23 |
Correct |
147 ms |
55892 KB |
Output is correct |
24 |
Correct |
83 ms |
58656 KB |
Output is correct |
25 |
Correct |
402 ms |
157808 KB |
Output is correct |
26 |
Correct |
576 ms |
235236 KB |
Output is correct |
27 |
Correct |
474 ms |
194356 KB |
Output is correct |
28 |
Correct |
516 ms |
184080 KB |
Output is correct |
29 |
Correct |
508 ms |
179744 KB |
Output is correct |
30 |
Correct |
516 ms |
188896 KB |
Output is correct |
31 |
Correct |
354 ms |
114608 KB |
Output is correct |
32 |
Correct |
515 ms |
184576 KB |
Output is correct |