#include<bits/stdc++.h>
//#define int long long
#define pii pair<int,ll>
#define fi first
#define se second
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")*/
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const ll N = (int)1e6;
const int maxN = (int)3e5 + 5;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e18 + 7;
const int base = (int)4e5;
const int Block_size = 350;
void InputFile()
{
//freopen("threesum.in","r",stdin);
//freopen("threesum.out","w",stdout);
//freopen("test.out","r",stdin);
}
void FastInput()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int n,m;
char a[4005][4005];
int dp[4005][4005];
void Read()
{
cin >> m >> n;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
//dp[i][j] = mod;
}
}
deque<pii> q;
dp[1][1] = 1;
q.push_back({1,1});
int res = 0;
do
{
pii u = q.front();
res = max(res,dp[u.fi][u.se]);
q.pop_front();
pii v = {u.fi,u.se+1};
if(v.se <= n && a[v.fi][v.se] != '.')
{
if(!dp[v.fi][v.se])
{
dp[v.fi][v.se] = dp[u.fi][u.se] + (a[u.fi][u.se] != a[v.fi][v.se]);
if(a[u.fi][u.se] != a[v.fi][v.se]) q.push_back(v);
else q.push_front(v);
}
}
v = {u.fi+1,u.se};
if(v.fi <= m && a[v.fi][v.se] != '.')
{
if(!dp[v.fi][v.se])
{
dp[v.fi][v.se] = dp[u.fi][u.se] + (a[u.fi][u.se] != a[v.fi][v.se]);
if(a[u.fi][u.se] != a[v.fi][v.se]) q.push_back(v);
else q.push_front(v);
}
}
v = {u.fi-1,u.se};
if(v.fi >= 1 && a[v.fi][v.se] != '.')
{
if(!dp[v.fi][v.se])
{
dp[v.fi][v.se] = dp[u.fi][u.se] + (a[u.fi][u.se] != a[v.fi][v.se]);
if(a[u.fi][u.se] != a[v.fi][v.se]) q.push_back(v);
else q.push_front(v);
}
}
v = {u.fi,u.se-1};
if(v.se >= 1 && a[v.fi][v.se] != '.')
{
if(!dp[v.fi][v.se])
{
dp[v.fi][v.se] = dp[u.fi][u.se] + (a[u.fi][u.se] != a[v.fi][v.se]);
if(a[u.fi][u.se] != a[v.fi][v.se]) q.push_back(v);
else q.push_front(v);
}
}
}
while(!q.empty());
cout << res;
/*for(int i = 1;i <= m;i++)
{
for(int j = 1;j <= n;j++)
{
cout << dp[i][j] <<' ';
}
cout <<'\n';
}*/
}
void Solve()
{
}
void Debug()
{
}
int32_t main()
{
FastInput();
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve()
int test;
//cin >> test;
test = 1;
while(test--)
{
Read();
Solve();
//Debug();
}
}
////
/////
//////
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
5204 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
7 ms |
5196 KB |
Output is correct |
5 |
Correct |
3 ms |
2900 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
736 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
4 ms |
3028 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
3 ms |
2840 KB |
Output is correct |
15 |
Correct |
9 ms |
5016 KB |
Output is correct |
16 |
Correct |
10 ms |
5204 KB |
Output is correct |
17 |
Correct |
8 ms |
4948 KB |
Output is correct |
18 |
Correct |
7 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30768 KB |
Output is correct |
2 |
Correct |
43 ms |
13472 KB |
Output is correct |
3 |
Correct |
282 ms |
57616 KB |
Output is correct |
4 |
Correct |
83 ms |
28932 KB |
Output is correct |
5 |
Correct |
192 ms |
44484 KB |
Output is correct |
6 |
Correct |
589 ms |
107376 KB |
Output is correct |
7 |
Correct |
17 ms |
32212 KB |
Output is correct |
8 |
Correct |
16 ms |
30756 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
16 ms |
31572 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
42 ms |
13444 KB |
Output is correct |
14 |
Correct |
24 ms |
9548 KB |
Output is correct |
15 |
Correct |
23 ms |
12036 KB |
Output is correct |
16 |
Correct |
18 ms |
5036 KB |
Output is correct |
17 |
Correct |
104 ms |
24776 KB |
Output is correct |
18 |
Correct |
89 ms |
31744 KB |
Output is correct |
19 |
Correct |
80 ms |
29004 KB |
Output is correct |
20 |
Correct |
68 ms |
22056 KB |
Output is correct |
21 |
Correct |
165 ms |
43428 KB |
Output is correct |
22 |
Correct |
188 ms |
44564 KB |
Output is correct |
23 |
Correct |
199 ms |
36248 KB |
Output is correct |
24 |
Correct |
165 ms |
40856 KB |
Output is correct |
25 |
Correct |
383 ms |
78672 KB |
Output is correct |
26 |
Correct |
515 ms |
169152 KB |
Output is correct |
27 |
Correct |
596 ms |
164684 KB |
Output is correct |
28 |
Correct |
600 ms |
107412 KB |
Output is correct |
29 |
Correct |
576 ms |
101828 KB |
Output is correct |
30 |
Correct |
580 ms |
118124 KB |
Output is correct |
31 |
Correct |
374 ms |
63808 KB |
Output is correct |
32 |
Correct |
551 ms |
124708 KB |
Output is correct |