이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
*
* ^v^
*
*/
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <iomanip>
#include <map>
#include <numeric>
#include <functional>
#include <algorithm>
#include <set>
#include <queue>
#include <climits>
#include <cstdlib>
#include <chrono>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define iamtefu ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long int
#define ld long double
#define sc(a) scanf("%lld", &a);
#define scv(a, n) vector<ll> a(n);fl(i,0,n){sc(a[i])}
#define prl(a) fl(i,0,a.size()){printf("%lld ", a[i]);}printf("\n");
#define fl(i,a,n) for (ll i(a); i<n; i++)
#define rfl(i,a,n) for (ll i(n-1); i>=a; i--)
#define print(a) for (auto x:a){cout<<x<<" ";} cout<<"\n";
#define tt int tt; cin>>tt; for(;tt--;)
ll gcd(ll a, ll b){
if (b==0){
return a;
}
return gcd(b, a%b);
}
ll pw(ll a, ll b, ll m){
ll res=1;
a%=m;
while (b){
if (b&1){
res=(res*a)%m;
}
a=(a*a)%m;
b/=2;
}
return res;
}
void scn(){
ll n, m; cin>>n>>m;
vector <string> a(n+1);
fl(i,0,n){
cin>>a[i];
}
vector <ll> di={1, 1, 0, -1, -1, -1, 0, 1};
vector <ll> dj={0, 1, 1, 1, 0, -1, -1, -1};
vector <vector <ll>> vis(n, vector <ll>(m));
ll cur = 0;
function <void(ll, ll, char)> dfs=[&](ll i, ll j, char c){
if (i<0 || i>=n || j<0 || j>=m || vis[i][j] || c!=a[i][j]){
return;
}
vis[i][j]=cur;
for (int d=0; d<8; d+=2){
dfs(i+di[d], j+dj[d], c);
}
};
// fl(i,0,n){
// fl(j,0,m){
// if (a[i][j]!='.' && !vis[i][j]){
// cur++;
// dfs(i, j, a[i][j]);
// }
// }
// }
vector <vector <ll>> dis(n, vector <ll>(m, 1e9));
queue <pair<ll,ll>> q;
q.push({0, 0});
dis[0][0] = 0;
ll ans = 0;
while (!q.empty()){
auto j = q.front();
q.pop();
ans = max(ans, dis[j.first][j.second]);
// cerr<<j.first<<" "<<j.second<<'\n';
for (int d = 0; d<8; d+=2){
ll i1 = j.first+di[d], j1 = j.second+dj[d];
if (i1<0 || i1>=n || j1<0 || j1>=m || a[i1][j1]=='.' || (dis[i1][j1]<=dis[j.first][j.second]+(a[j.first][j.second]!=a[i1][j1]))){
continue;
} else {
dis[i1][j1] = dis[j.first][j.second]+(a[j.first][j.second]!=a[i1][j1]);
q.push({i1, j1});
}
}
}
cout<<ans+1<<'\n';
// not necessarily distinct
}
int main(){
iamtefu;
#if defined(airths)
auto t1=chrono::high_resolution_clock::now();
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//
#endif
// tt
{
scn();
}
#if defined(airths)
auto t2=chrono::high_resolution_clock::now();
ld ti=chrono::duration_cast<chrono::nanoseconds>(t2-t1).count();
ti*=1e-6;
cerr<<"Time: "<<setprecision(12)<<ti;
cerr<<"ms\n";
#endif
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |