#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define X first
#define Y second
//const ll mod = 1000000007;
const ll mod = 998244353;
int dp[4001][4001];
void solve()
{
ll n, m;
cin >> n >> m;
vector<string> s(n);
for (int i=0;i<n;i++) cin >> s[i];
deque<int> Q;
Q.push_back(0);
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++) dp[i][j] = mod;
}
dp[0][0] = 0;
vector<int> was(n*m);
vector<int> dx = {1,-1,0,0};
vector<int> dy = {0,0,1,-1};
while (Q.size()>0)
{
int p = Q.front();
Q.pop_front();
if (was[p]==1) continue;
was[p] = 1;
int x = p/m;
int y = p-m*x;
for (int d=0;d<4;d++)
{
int x2 = x+dx[d];
int y2 = y+dy[d];
if (x2>=n or x2<0 or y2>=m or y2<0 or s[x2][y2]=='.') continue;
int W = dp[x][y];
int F = 0;
if (s[x][y]!=s[x2][y2]) F = 1;
if (dp[x2][y2]>W+F)
{
dp[x2][y2] = W+F;
if (F==0) Q.push_front(x2*m+y2);
else Q.push_back(x2*m+y2);
}
}
}
int A = 0;
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
if (dp[i][j]<mod) A = max(A,dp[i][j]);
}
}
cout << A+1 << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
ll T;
T = 1;
//cin >> T;
while (T--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4436 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
8 ms |
3776 KB |
Output is correct |
5 |
Correct |
3 ms |
2132 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1492 KB |
Output is correct |
12 |
Correct |
6 ms |
2260 KB |
Output is correct |
13 |
Correct |
4 ms |
2132 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
12 ms |
4564 KB |
Output is correct |
16 |
Correct |
23 ms |
4436 KB |
Output is correct |
17 |
Correct |
11 ms |
4308 KB |
Output is correct |
18 |
Correct |
9 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16212 KB |
Output is correct |
2 |
Correct |
48 ms |
19200 KB |
Output is correct |
3 |
Correct |
221 ms |
143308 KB |
Output is correct |
4 |
Correct |
82 ms |
41172 KB |
Output is correct |
5 |
Correct |
143 ms |
92244 KB |
Output is correct |
6 |
Correct |
702 ms |
150216 KB |
Output is correct |
7 |
Correct |
8 ms |
16852 KB |
Output is correct |
8 |
Correct |
10 ms |
16212 KB |
Output is correct |
9 |
Correct |
2 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
7 ms |
16524 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
44 ms |
19156 KB |
Output is correct |
14 |
Correct |
25 ms |
11988 KB |
Output is correct |
15 |
Correct |
19 ms |
13216 KB |
Output is correct |
16 |
Correct |
21 ms |
7636 KB |
Output is correct |
17 |
Correct |
111 ms |
44500 KB |
Output is correct |
18 |
Correct |
86 ms |
43908 KB |
Output is correct |
19 |
Correct |
69 ms |
41160 KB |
Output is correct |
20 |
Correct |
56 ms |
37972 KB |
Output is correct |
21 |
Correct |
136 ms |
95432 KB |
Output is correct |
22 |
Correct |
139 ms |
92296 KB |
Output is correct |
23 |
Correct |
239 ms |
78264 KB |
Output is correct |
24 |
Correct |
134 ms |
93816 KB |
Output is correct |
25 |
Correct |
420 ms |
143268 KB |
Output is correct |
26 |
Correct |
369 ms |
141580 KB |
Output is correct |
27 |
Correct |
507 ms |
156712 KB |
Output is correct |
28 |
Correct |
716 ms |
150140 KB |
Output is correct |
29 |
Correct |
688 ms |
149356 KB |
Output is correct |
30 |
Correct |
600 ms |
149360 KB |
Output is correct |
31 |
Correct |
648 ms |
101984 KB |
Output is correct |
32 |
Correct |
443 ms |
150980 KB |
Output is correct |