#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back //emplace_back
#define pp pop_back
#define pii pair<int, int> //pair<int, int>
#define all(x) (x).begin(),(x).end()
#define mp(a,b) make_pair(a , b)
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define F first
#define S second
#define bg(x) (x).begin()
#define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
#define debug(x) cout << #x << " : " << x << endl
#define endl (char)(10)
#define arr(x) array<int , (x)>
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
ll Sum(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a += b;
return a % MOD;
}
ll Mul(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a *= b;
return a % MOD;
}
ll Pow(ll a , ll b , ll MOD)
{
ll res = 1;
while(b)
{
if((b & 1))res = Mul(res , a , MOD);
a = Mul(a , a , MOD);
b >>= 1;
}
return res;
}
ll Min(ll a , ll b)
{
if(a > b)return b;
return a;
}
ll Max(ll a , ll b)
{
if(a > b)return a;
return b;
}
ll Ceil(ll a , ll b)
{
if(a % b)return (a/b)+1;
return a/b;
}
/////////////////////
//VALS
const int maxn = 5000;
int n,m;
string s[maxn];
bool vis[maxn][maxn];
int ans = 0;
int x[] = {1,-1,0,0};
int y[] = {0,0,1,-1};
/////////////////////
//FUNCTIONS
inline void bfs(queue<pii> &q1 , queue<pii> &q2, int d)
{
if(q1.empty())return;
ans = d;
while(!q1.empty())
{
pii p = q1.front();
q1.pop();
//debug(p.F);
//debug(p.S);
For(i,4)
{
if(p.F + x[i] < 0 or p.F + x[i] >= n or p.S + y[i] < 0 or p.S + y[i] >= m or s[p.F + x[i]][p.S + y[i]] == '.' or vis[p.F + x[i]][p.S + y[i]] == true)continue;
vis[p.F + x[i]][p.S + y[i]] = true;
bool flag = (s[p.F + x[i]][p.S + y[i]] == s[p.F][p.S]);
//debug(i);
//debug(flag);
if(flag)
q1.push(mp(p.F + x[i], p.S + y[i]));
else
q2.push(mp(p.F + x[i], p.S + y[i]));
}
}
bfs(q2, q1, d+1);
}
/////////////////////
//SOLVE
/////////////////////
//MAIN
int main()
{
FAST;
cin >> n >> m;
For(i,n)cin >> s[i];
vis[0][0] = 1;
queue<pii> q1, q2;
q1.push({0,0});
bfs(q1,q2,1);
cout << ans << "\n";
return 0;
}
/////////////////////
/*
ZZZZZZZ A M M IIIIIII N N
Z A A M M M M I NN N
Z A A M M M M I N N N
Z AAAAAAA M M M I N N N
Z A A M M I N N N
Z A A M M I N NN
ZZZZZZZ A A M M IIIIIII N N TREE
*/
Compilation message
tracks.cpp: In function 'void bfs(std::queue<std::pair<int, int> >&, std::queue<std::pair<int, int> >&, int)':
tracks.cpp:19:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
19 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
tracks.cpp:99:3: note: in expansion of macro 'For'
99 | For(i,4)
| ^~~
tracks.cpp: In function 'int main()':
tracks.cpp:19:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
19 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
tracks.cpp:127:2: note: in expansion of macro 'For'
127 | For(i,n)cin >> s[i];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
3160 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
3164 KB |
Output is correct |
5 |
Correct |
3 ms |
1884 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
884 KB |
Output is correct |
10 |
Correct |
2 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
2008 KB |
Output is correct |
15 |
Correct |
8 ms |
3164 KB |
Output is correct |
16 |
Correct |
9 ms |
3208 KB |
Output is correct |
17 |
Correct |
6 ms |
3160 KB |
Output is correct |
18 |
Correct |
5 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15964 KB |
Output is correct |
2 |
Correct |
33 ms |
9468 KB |
Output is correct |
3 |
Correct |
158 ms |
53240 KB |
Output is correct |
4 |
Correct |
47 ms |
17180 KB |
Output is correct |
5 |
Correct |
110 ms |
32940 KB |
Output is correct |
6 |
Correct |
715 ms |
63540 KB |
Output is correct |
7 |
Correct |
8 ms |
16732 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
608 KB |
Output is correct |
11 |
Correct |
8 ms |
16220 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
33 ms |
9432 KB |
Output is correct |
14 |
Correct |
19 ms |
6492 KB |
Output is correct |
15 |
Correct |
13 ms |
7320 KB |
Output is correct |
16 |
Correct |
15 ms |
3792 KB |
Output is correct |
17 |
Correct |
92 ms |
18360 KB |
Output is correct |
18 |
Correct |
56 ms |
18512 KB |
Output is correct |
19 |
Correct |
56 ms |
17172 KB |
Output is correct |
20 |
Correct |
38 ms |
15880 KB |
Output is correct |
21 |
Correct |
95 ms |
34644 KB |
Output is correct |
22 |
Correct |
108 ms |
33140 KB |
Output is correct |
23 |
Correct |
193 ms |
28620 KB |
Output is correct |
24 |
Correct |
96 ms |
33396 KB |
Output is correct |
25 |
Correct |
287 ms |
53580 KB |
Output is correct |
26 |
Correct |
362 ms |
42976 KB |
Output is correct |
27 |
Correct |
484 ms |
55344 KB |
Output is correct |
28 |
Correct |
640 ms |
63580 KB |
Output is correct |
29 |
Correct |
583 ms |
61932 KB |
Output is correct |
30 |
Correct |
556 ms |
60128 KB |
Output is correct |
31 |
Correct |
489 ms |
37664 KB |
Output is correct |
32 |
Correct |
393 ms |
54388 KB |
Output is correct |