#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define ii pair<int, int>
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define P 31
#define mod 1'000'000'007
#define inf 1'000'000'000'000
#define pb push_back
#define str string
#define sz(x) (x).size()
#define vvi vector<vi>
#define fun function
#define oopt cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false);
#define file freopen("problemname.in", "r", stdin); freopen("pr.out", "w", stdout);
#define dbg(v) cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
using namespace std;
template <class T, int SZ> using arr = array<T, SZ>;
char a[4001][4001];
int ds[4001][4001];
vector<ii> pr = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
signed main()
{
oopt;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
ds[i][j] = inf;
}
ds[0][0] = 1;
int rez = 1;
deque<ii> dq;
dq.pb({0, 0});
while (dq.size())
{
ii t = dq.front();
dq.pop_front();
rez = max(rez, ds[t.f][t.s]);
for (ii d : pr)
{
int i = t.f+d.f, j = t.s+d.s;
if (i < 0 || j < 0 || i >= n || j >= m || a[i][j] == '.') continue;
if (ds[i][j] > ds[t.f][t.s]+(a[i][j] != a[t.f][t.s]))
{
ds[i][j] = ds[t.f][t.s]+(a[i][j] != a[t.f][t.s]);
if (a[i][j] == a[t.f][t.s])
dq.push_front({i, j});
else
dq.pb({i, j});
}
}
}
cout << rez << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21328 KB |
Output is correct |
2 |
Correct |
1 ms |
6640 KB |
Output is correct |
3 |
Correct |
2 ms |
6480 KB |
Output is correct |
4 |
Correct |
8 ms |
21596 KB |
Output is correct |
5 |
Correct |
4 ms |
14928 KB |
Output is correct |
6 |
Correct |
1 ms |
6480 KB |
Output is correct |
7 |
Correct |
2 ms |
6648 KB |
Output is correct |
8 |
Correct |
2 ms |
6480 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
4 ms |
12880 KB |
Output is correct |
11 |
Correct |
3 ms |
10876 KB |
Output is correct |
12 |
Correct |
6 ms |
15096 KB |
Output is correct |
13 |
Correct |
4 ms |
14928 KB |
Output is correct |
14 |
Correct |
4 ms |
14928 KB |
Output is correct |
15 |
Correct |
10 ms |
21328 KB |
Output is correct |
16 |
Correct |
13 ms |
21328 KB |
Output is correct |
17 |
Correct |
9 ms |
21072 KB |
Output is correct |
18 |
Correct |
7 ms |
21584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
138332 KB |
Output is correct |
2 |
Correct |
39 ms |
49232 KB |
Output is correct |
3 |
Correct |
247 ms |
156956 KB |
Output is correct |
4 |
Correct |
70 ms |
73800 KB |
Output is correct |
5 |
Correct |
187 ms |
117320 KB |
Output is correct |
6 |
Correct |
629 ms |
180472 KB |
Output is correct |
7 |
Correct |
14 ms |
141136 KB |
Output is correct |
8 |
Correct |
14 ms |
138320 KB |
Output is correct |
9 |
Correct |
3 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
15 ms |
140880 KB |
Output is correct |
12 |
Correct |
3 ms |
10720 KB |
Output is correct |
13 |
Correct |
43 ms |
49240 KB |
Output is correct |
14 |
Correct |
26 ms |
36176 KB |
Output is correct |
15 |
Correct |
21 ms |
40272 KB |
Output is correct |
16 |
Correct |
19 ms |
19548 KB |
Output is correct |
17 |
Correct |
105 ms |
78408 KB |
Output is correct |
18 |
Correct |
80 ms |
78128 KB |
Output is correct |
19 |
Correct |
86 ms |
73800 KB |
Output is correct |
20 |
Correct |
59 ms |
69452 KB |
Output is correct |
21 |
Correct |
139 ms |
120456 KB |
Output is correct |
22 |
Correct |
174 ms |
116040 KB |
Output is correct |
23 |
Correct |
188 ms |
100424 KB |
Output is correct |
24 |
Correct |
143 ms |
120336 KB |
Output is correct |
25 |
Correct |
354 ms |
153472 KB |
Output is correct |
26 |
Correct |
519 ms |
240636 KB |
Output is correct |
27 |
Correct |
553 ms |
186036 KB |
Output is correct |
28 |
Correct |
615 ms |
176048 KB |
Output is correct |
29 |
Correct |
583 ms |
178856 KB |
Output is correct |
30 |
Correct |
604 ms |
191172 KB |
Output is correct |
31 |
Correct |
414 ms |
118600 KB |
Output is correct |
32 |
Correct |
563 ms |
190664 KB |
Output is correct |