#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long int li;
typedef unsigned long int uli;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;
int main()
{
int h, w;
cin >> h >> w;
vector<string> vec(h);
frange(i, h) cin >> vec[i];
deque<pii> dq;
vector<vb> vis(h, vb(w));
vvi dist(h, vi(w, 0));
dq.pb({0, 0});
dist[0][0] = 1;
vii pos;
pos.pb({0, 1});
pos.pb({0, -1});
pos.pb({1, 0});
pos.pb({-1, 0});
int ans = 1;
while (dq.size())
{
auto p = dq.front();
dq.pop_front();
if (vis[p.f][p.s])
continue;
vis[p.f][p.s] = true;
ans = max(ans, dist[p.f][p.s]);
for (auto e : pos)
{
if (0 <= p.f + e.f && p.f + e.f < h && 0 <= p.s + e.s && p.s + e.s < w && vec[p.f + e.f][p.s + e.s] != '.')
{
int w = 0;
if (vec[p.f][p.s] != vec[p.f + e.f][p.s + e.s])
{
w = 1;
}
if (dist[p.f + e.f][p.s + e.s] == 0)
{
dist[p.f + e.f][p.s + e.s] = dist[p.f][p.s] + w;
if (w)
dq.pb({p.f + e.f, p.s + e.s});
else
dq.push_front({p.f + e.f, p.s + e.s});
}
}
}
}
printf("%d", ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2004 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
10 ms |
1512 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
6 ms |
852 KB |
Output is correct |
13 |
Correct |
4 ms |
788 KB |
Output is correct |
14 |
Correct |
4 ms |
788 KB |
Output is correct |
15 |
Correct |
15 ms |
2060 KB |
Output is correct |
16 |
Correct |
16 ms |
2024 KB |
Output is correct |
17 |
Correct |
12 ms |
1912 KB |
Output is correct |
18 |
Correct |
9 ms |
1500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
980 KB |
Output is correct |
2 |
Correct |
67 ms |
9568 KB |
Output is correct |
3 |
Correct |
485 ms |
96592 KB |
Output is correct |
4 |
Correct |
128 ms |
23204 KB |
Output is correct |
5 |
Correct |
312 ms |
48684 KB |
Output is correct |
6 |
Correct |
930 ms |
109664 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
4 ms |
1080 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
66 ms |
9628 KB |
Output is correct |
14 |
Correct |
37 ms |
6256 KB |
Output is correct |
15 |
Correct |
36 ms |
6816 KB |
Output is correct |
16 |
Correct |
30 ms |
4140 KB |
Output is correct |
17 |
Correct |
171 ms |
25032 KB |
Output is correct |
18 |
Correct |
139 ms |
24668 KB |
Output is correct |
19 |
Correct |
120 ms |
23296 KB |
Output is correct |
20 |
Correct |
110 ms |
21508 KB |
Output is correct |
21 |
Correct |
283 ms |
50340 KB |
Output is correct |
22 |
Correct |
310 ms |
48560 KB |
Output is correct |
23 |
Correct |
329 ms |
41872 KB |
Output is correct |
24 |
Correct |
281 ms |
49484 KB |
Output is correct |
25 |
Correct |
655 ms |
96356 KB |
Output is correct |
26 |
Correct |
791 ms |
114496 KB |
Output is correct |
27 |
Correct |
888 ms |
114536 KB |
Output is correct |
28 |
Correct |
968 ms |
109444 KB |
Output is correct |
29 |
Correct |
936 ms |
106896 KB |
Output is correct |
30 |
Correct |
926 ms |
111096 KB |
Output is correct |
31 |
Correct |
618 ms |
54640 KB |
Output is correct |
32 |
Correct |
866 ms |
112728 KB |
Output is correct |