#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define all(v, l) v.begin() + l, v.end()
#define rall(v, l) v.rbegin(), v.rend() - l
#define pb push_back
#define pf push_front
#define rsz resize
#define fi first
#define se second
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
#define IMAX INT_MAX
#define IMIN INT_MIN
#define endl "\n"
#define newline cout << endl;
using namespace std;
// structs
struct graph
{
vector <vector <char> > g;
vector <vector <bool> > used;
int N, M;
void prep()
{
g.rsz(N + 1);
used.rsz(N + 1);
for (int i = 0; i <= N; i++)
{
g[i].rsz(M + 1);
used[i].rsz(M + 1);
}
}
void get()
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= M; j++)
{
cin >> g[i][j];
}
}
}
bool check(int r, int c)
{
return !(r < 1 || r > N || c < 1 || c > M || used[r][c] || g[r][c] == '.');
}
int BFS(pair <int, int> start)
{
int cnt = 0;
deque <pair <int, int> > dq;
dq.pb(start);
used[start.fi][start.se] = true;
char curr = 'X';
while (!dq.empty())
{
auto [r, c] = dq.front();
cnt += curr != g[r][c];
curr = g[r][c];
dq.pop_front();
for (auto [gotor, gotoc] : vector <pair <int, int> > {{r + 1, c}, {r - 1, c}, {r, c + 1}, {r, c - 1}})
{
if (!check(gotor, gotoc))
{
continue;
}
used[gotor][gotoc] = true;
if (g[gotor][gotoc] == g[r][c])
{
dq.pf({gotor, gotoc});
}
else
{
dq.pb({gotor, gotoc});
}
}
}
return cnt;
}
void cleanse()
{
for (int i = 0; i <= N; i++)
{
fill(all(used[i], 0), false);
}
}
};
// globals
// variables
// iterators
int i;
// notes
/*
-stuff you should look for-
* int overflow, array bounds
* special cases (n=1?)
* do something instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
continue - skip the rest in the loop
*/
// functions
ll GCD(ll numeroune, ll numerodeux);
ll LCM(ll numeroune, ll numerodeux);
ll power(ll numeroune, ll numerodeux);
void solve()
{
graph G;
cin >> G.N >> G.M;
G.prep();
G.get();
G.cleanse();
cout << G.BFS({1, 1});
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
newline
}
}
ll GCD(ll numeroune, ll numerodeux)
{
if (!numeroune)
{
return numerodeux;
}
return GCD(numerodeux % numeroune, numeroune);
}
ll LCM(ll numeroune, ll numerodeux)
{
return numeroune * numerodeux / GCD(numeroune, numerodeux);
}
ll power(ll numeroune, ll numerodeux)
{
ll res = 1;
while (numerodeux)
{
if (numerodeux & 1)
{
res *= numeroune;
}
numeroune *= numeroune;
numerodeux >>= 1;
}
return res;
}
/*
$$$$$$$$\ $$$$$$$$\
$$ _____|\____$$ |
$$ | $$ /
$$$$$\ $$ /
$$ __| $$ /
$$ | $$ /
$$$$$$$$\ $$$$$$$$\
\________|\________|
*/
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |