#include<bits/stdc++.h>
typedef long long ll;
#define vi vector<int>
#define pb push_back
#define self_min(a, b) a = min(a, b)
#define self_max(a, b) a = max(a, b)
#define V vector
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define fs(lcv, hi) for(lcv = 0; lcv < hi; lcv ++)
#define fb(lcv, hi) for(lcv = hi; lcv >= 0; lcv --)
#define loop(lcv, lower, upper) for(lcv = lower; lcv < upper; lcv++)
#define crd(pii) [pii.first][pii.second]
using namespace std;
int h, w;
bool within(pair<int, int> p)
{
int x, y;
tie(x, y) = p;
if(x >= 0 && x < h && y >= 0 && y < w)
{
return 1;
}
return 0;
}
pair<int, int> operator +(pair<int, int> p1, pair<int, int> p2)
{
return {p1.first + p2.first, p1.second + p2.second};
}
void solve()
{
int i, j;
cin >> h;
cin >> w;
V<string> grid(h);
fs(i, h) cin >> grid[i];
vector<pair<int, int>> dx = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
deque <pair<int, int>> bfsq = {{0, 0}};
V<vi> distances(h, vi(w, 0));
V<V<bool>> vis(h, V<bool>(w, 0));
distances[0][0] = 1;
int ans = 0;
while(!bfsq.empty())
{
pair<int, int> pt = bfsq.front();
// cout << pt.first << " " << pt.second << " " << distances crd(pt) << "\n";
vis crd(pt) = 1;
self_max(ans, distances crd(pt));
bfsq.pop_front();
self_max(ans, distances crd(pt));
for(auto xdx : dx)
{
pair<int, int> curr = pt + xdx;
// cout << curr.first << " " << curr.second << "Hola\n";
if(within(curr) && !vis crd(curr) && grid crd(curr) != '.')
{
if(grid crd(curr) != grid crd(pt))
{
distances crd(curr) = distances crd(pt) + 1;
bfsq.push_back(curr);
}
else
{
distances crd(curr) = distances crd(pt);
bfsq.push_front(curr);
}
}
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
Compilation message
tracks.cpp: In function 'void solve()':
tracks.cpp:38:12: warning: unused variable 'j' [-Wunused-variable]
38 | int i, j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
2004 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
9 ms |
1736 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
9 ms |
968 KB |
Output is correct |
13 |
Correct |
2 ms |
864 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
17 ms |
1876 KB |
Output is correct |
16 |
Correct |
23 ms |
2008 KB |
Output is correct |
17 |
Correct |
10 ms |
1852 KB |
Output is correct |
18 |
Correct |
9 ms |
1776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
980 KB |
Output is correct |
2 |
Correct |
52 ms |
9964 KB |
Output is correct |
3 |
Correct |
261 ms |
98736 KB |
Output is correct |
4 |
Correct |
57 ms |
23304 KB |
Output is correct |
5 |
Correct |
121 ms |
55496 KB |
Output is correct |
6 |
Correct |
1022 ms |
132352 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
1 ms |
1008 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
55 ms |
9972 KB |
Output is correct |
14 |
Correct |
30 ms |
5844 KB |
Output is correct |
15 |
Correct |
12 ms |
6584 KB |
Output is correct |
16 |
Correct |
28 ms |
4180 KB |
Output is correct |
17 |
Correct |
128 ms |
25216 KB |
Output is correct |
18 |
Correct |
54 ms |
24800 KB |
Output is correct |
19 |
Correct |
60 ms |
23236 KB |
Output is correct |
20 |
Correct |
58 ms |
21404 KB |
Output is correct |
21 |
Correct |
142 ms |
57408 KB |
Output is correct |
22 |
Correct |
121 ms |
55524 KB |
Output is correct |
23 |
Correct |
282 ms |
47840 KB |
Output is correct |
24 |
Correct |
110 ms |
56016 KB |
Output is correct |
25 |
Correct |
322 ms |
98616 KB |
Output is correct |
26 |
Correct |
586 ms |
175784 KB |
Output is correct |
27 |
Correct |
755 ms |
133312 KB |
Output is correct |
28 |
Correct |
971 ms |
132472 KB |
Output is correct |
29 |
Correct |
939 ms |
129080 KB |
Output is correct |
30 |
Correct |
866 ms |
131092 KB |
Output is correct |
31 |
Correct |
809 ms |
64404 KB |
Output is correct |
32 |
Correct |
691 ms |
127180 KB |
Output is correct |