// #pragma GCC optimization ("unroll-loops")
// #pragma GCC optimization ("O1, O2, O3, Ofast")
// #pragma GCC optimization ("trapv")
// #pragma GCC optimization ("sse, sse2, sse3, sse4, sse4.1, sse4.2, avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max_heap priority_queue<ll>
#define min_heap priority_queue<pair <ll, ll>, vector<pair <ll, ll>>, greater<pair <ll, ll>>> // size(), push(), top(), pop();
//#define min_heap priority_queue<ll, vector<ll>, greater<ll>>
#define sariE cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define filE freopen("in.txt", "r", stdin); freopen("out1.txt", "w", stdout);
#define endl '\n'
#define md(a) (a % mod + mod) % mod
//cout << setprecision(5) << f;
ll const maxn = 2e5 + 10;
ll const inf = 2e18;
ll const loG = 23;
ll const mod = 1e6 + 3;//998244353; //1e9 + 9, // 1e9 + 7;
ll const sq = 750;
ll power(ll a, ll b, ll mod){if (b == 0) return 1; if (b == 1) return a; ll x = power(a, b / 2, mod); return (((x * x) % mod) * (b % 2 ? a : 1)) % mod;}
ll n, m, ans;
string mp[4010];
bool mark[4010][4010];
ll dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
void bfs(){
queue <pair <ll, ll>> qu[2];
ll anim = 0;
qu[0].push({1, 1});
qu[0].push({n, m});
while (!qu[anim].empty()){
while (!qu[anim].empty()){
ll x = qu[anim].front().first, y = qu[anim].front().second; qu[anim].pop();
if (mark[x][y])
continue;
mark[x][y] = 1;
for (int i = 0; i < 4; i++){
ll xx = x + dx[i], yy = y + dy[i];
if (xx > 0 && xx <= n && yy > 0 && yy <= m && !mark[xx][yy]){
if (mp[x][y] == mp[xx][yy]){
qu[anim].push({xx, yy});
}
else{
qu[1 - anim].push({xx, yy});
}
}
}
}
anim = 1 - anim;
ans++;
}
}
int main(){
sariE;// filE;
cin >> n >> m;
for (int i = 1; i < n + 1; i++){
cin >> mp[i];
mp[i] = ' ' + mp[i];
for (int j = 1; j < mp[i].size(); j++){
if (mp[i][j] == '.')
mark[i][j] = 1;
}
}
bfs();
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |