/*
Coded by thegamercoder
Happy Competition and Hacking
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define FILER 0
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = pow(10, 9) + 7;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const ull INFUL = 0x3f3f3f3f3f3f3f3f;
const ll INFT = 0x3f3f3f3f;
const ll MAX = 401;
const ll MODD = 998244353;
const ll BL = 1;
const double EPS = 1e-10;
#define V vector
#define pll pair<ll, ll>
#define pull2 pair<ull,ull>
#define MS multiset
#define M map
#define Q queue
#define PQ priority_queue
#define IOF ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(typ,i,a,b,c) for(typ i = a; i < b; i += c)
#define FORR(typ,i,a,b,c) for(typ i = a; i > b; i -= c)
#define FORA(a,i) for(auto i : a)
#define FORAE(a,i) for(auto &i : a)
#define all(v) v.begin(),v.end()
#define sorta(a) sort(all(a))
#define sortd(a) sort(all(a), greater<ll>())
#define setp(x) setprecision(x)<<fixed
#define RET return
#define log(a,b) log(b)/log(a)
#define WH(s) while(s)
#define WHI(t) WH(t--)
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define Yes cout<<"Yes"<<endl;
#define No cout<<"No"<<endl;
#define YESNO(s) cout<<(s?"YES":"NO")<<endl;
#define YesNo(s) cout<<(s?"Yes":"No")<<endl;
#define TYP 0
using namespace std;
ll n, m, ans, dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 };
V<string> g;
V<V<ll>> d;
bool chk(ll x, ll y)
{
RET x >= 0 && x < n&& y >= 0 && y < m && g[x][y] != '.';
}
void solve()
{
cin >> n >> m;
g.resize(n); d.resize(n, V<ll>(m, INFL));
deque<pll> dq;
FOR(ll, i, 0, n, 1) cin >> g[i];
d[0][0] = 1;
ans = 1;
dq.push_back({ 0,0 });
WH(!dq.empty())
{
auto [x, y] = dq.front(); dq.pop_front();
ans = max(ans, d[x][y]);
FOR(ll, i, 0, 4, 1)
{
ll xp = x + dx[i], yp = y + dy[i];
if (!chk(xp, yp))continue;
ll w = g[x][y] != g[xp][yp];
if (d[xp][yp] > d[x][y] + w)
{
d[xp][yp] = d[x][y] + w;
if (w)dq.push_back({ xp,yp });
else dq.push_front({ xp,yp });
}
}
}
cout << ans << endl;
}
void init()
{
if (FILER)
{
freopen("pump.in", "r", stdin);
freopen("pump.out", "w", stdout);
}
}
void handle()
{
init();
ll t = 1;
if (TYP)cin >> t;
WHI(t)solve();
}
int main()
{
IOF
handle();
RET 0;
}
Compilation message
tracks.cpp: In function 'void solve()':
tracks.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
70 | auto [x, y] = dq.front(); dq.pop_front();
| ^
tracks.cpp: In function 'void init()':
tracks.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen("pump.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen("pump.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
2140 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
8 ms |
2652 KB |
Output is correct |
16 |
Correct |
9 ms |
2652 KB |
Output is correct |
17 |
Correct |
6 ms |
2620 KB |
Output is correct |
18 |
Correct |
5 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
30 ms |
14448 KB |
Output is correct |
3 |
Correct |
170 ms |
143440 KB |
Output is correct |
4 |
Correct |
61 ms |
33624 KB |
Output is correct |
5 |
Correct |
123 ms |
80720 KB |
Output is correct |
6 |
Correct |
582 ms |
169932 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
1272 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
30 ms |
14428 KB |
Output is correct |
14 |
Correct |
17 ms |
8548 KB |
Output is correct |
15 |
Correct |
13 ms |
9308 KB |
Output is correct |
16 |
Correct |
15 ms |
5980 KB |
Output is correct |
17 |
Correct |
82 ms |
36400 KB |
Output is correct |
18 |
Correct |
63 ms |
36116 KB |
Output is correct |
19 |
Correct |
55 ms |
33624 KB |
Output is correct |
20 |
Correct |
42 ms |
31124 KB |
Output is correct |
21 |
Correct |
102 ms |
83540 KB |
Output is correct |
22 |
Correct |
129 ms |
81324 KB |
Output is correct |
23 |
Correct |
162 ms |
69456 KB |
Output is correct |
24 |
Correct |
100 ms |
81492 KB |
Output is correct |
25 |
Correct |
458 ms |
143696 KB |
Output is correct |
26 |
Correct |
362 ms |
210184 KB |
Output is correct |
27 |
Correct |
459 ms |
178496 KB |
Output is correct |
28 |
Correct |
595 ms |
169996 KB |
Output is correct |
29 |
Correct |
571 ms |
170600 KB |
Output is correct |
30 |
Correct |
520 ms |
169840 KB |
Output is correct |
31 |
Correct |
410 ms |
92824 KB |
Output is correct |
32 |
Correct |
402 ms |
174556 KB |
Output is correct |