#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << endl
#define all(x) x.begin(),x.end()
bool vis[4010][4010];
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m;cin>>n>>m;
string s[n];
for(int i=0;i<n;i++) cin>>s[i];
priority_queue<pair<int,pii>> pq;
pq.push({0,{0,0}});
vis[0][0]=1;
int ans=0;
while(!pq.empty())
{
int x=pq.top().se.fi,y=pq.top().se.se,v=-pq.top().fi;
pq.pop();
ans=max(ans,v);
if(x-1>=0 && !vis[x-1][y] && s[x-1][y]!='.')
{
vis[x-1][y]=1;
pq.push({-(v+(s[x][y]!=s[x-1][y])),{x-1,y}});
}
if(y-1>=0 && !vis[x][y-1] && s[x][y-1]!='.')
{
vis[x][y-1]=1;
pq.push({-(v+(s[x][y]!=s[x][y-1])),{x,y-1}});
}
if(x+1<n && !vis[x+1][y] && s[x+1][y]!='.')
{
vis[x+1][y]=1;
pq.push({-(v+(s[x][y]!=s[x+1][y])),{x+1,y}});
}
if(y+1<m && !vis[x][y+1] && s[x][y+1]!='.')
{
vis[x][y+1]=1;
pq.push({-(v+(s[x][y]!=s[x][y+1])),{x,y+1}});
}
}
cout<<ans+1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
16 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
1696 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
632 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
5 ms |
1456 KB |
Output is correct |
12 |
Correct |
9 ms |
1796 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
19 ms |
2736 KB |
Output is correct |
16 |
Correct |
26 ms |
2652 KB |
Output is correct |
17 |
Correct |
12 ms |
2392 KB |
Output is correct |
18 |
Correct |
16 ms |
2952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
15452 KB |
Output is correct |
2 |
Correct |
66 ms |
6972 KB |
Output is correct |
3 |
Correct |
180 ms |
33880 KB |
Output is correct |
4 |
Correct |
65 ms |
11864 KB |
Output is correct |
5 |
Correct |
68 ms |
21852 KB |
Output is correct |
6 |
Correct |
1936 ms |
60288 KB |
Output is correct |
7 |
Correct |
5 ms |
16216 KB |
Output is correct |
8 |
Correct |
5 ms |
15532 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
15964 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
65 ms |
7092 KB |
Output is correct |
14 |
Correct |
40 ms |
5032 KB |
Output is correct |
15 |
Correct |
6 ms |
5212 KB |
Output is correct |
16 |
Correct |
34 ms |
2652 KB |
Output is correct |
17 |
Correct |
170 ms |
12636 KB |
Output is correct |
18 |
Correct |
30 ms |
12380 KB |
Output is correct |
19 |
Correct |
70 ms |
11868 KB |
Output is correct |
20 |
Correct |
46 ms |
10844 KB |
Output is correct |
21 |
Correct |
110 ms |
22620 KB |
Output is correct |
22 |
Correct |
68 ms |
21852 KB |
Output is correct |
23 |
Correct |
337 ms |
18856 KB |
Output is correct |
24 |
Correct |
52 ms |
22360 KB |
Output is correct |
25 |
Correct |
177 ms |
33760 KB |
Output is correct |
26 |
Correct |
903 ms |
27736 KB |
Output is correct |
27 |
Correct |
1597 ms |
37060 KB |
Output is correct |
28 |
Correct |
1932 ms |
59032 KB |
Output is correct |
29 |
Correct |
1941 ms |
48004 KB |
Output is correct |
30 |
Correct |
1898 ms |
47124 KB |
Output is correct |
31 |
Correct |
1249 ms |
24924 KB |
Output is correct |
32 |
Correct |
1208 ms |
35440 KB |
Output is correct |