This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///* __ __ __ */
///* ====== _ /| /| __ _ / | | /| | * | | | | / /| |\ | / | | * | / */
///* \- || |_| |_ / |/ | | | |_ |- | |--| /-| | | \ \ |==| |- /=| | \ | | |--| | |- */
///* || | | |_ / | |__| _| |_ \__ | | / | |__ | __| | | | \ / | | \| \__ | | | | \ */
///*
// autor :: Rick Prime
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define Mp make_pair
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef vector<pii> vii;
const int N = 1e6, inf = 2e18, mod = 1e9+7;
void solve(){
//freopen("snakes.in", "r", stdin);
//freopen("snakes.out", "w", stdout );
int h, w;
cin >> h >> w;
char a[h][w];
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> a[i][j];
}
}
deque<pair<int, pii> > q;
q.pb({1,{0, 0}});
vi di = {-1, 1, 0, 0};
vi dj = {0, 0, -1, 1};
int ans = 1, cnt = 0;
int us[h][w]{};
us[0][0] = 1;
while(!q.empty()){
auto [dis, ps] = q.front();
auto [x, y] = ps;
q.pop_front();
ans = max(ans, dis);
//cout << dis << ' ' << ++cnt << '\n';
for(int k = 0; k < 4; k++){
int nx = x + di[k];
int ny = y + dj[k];
if(nx<0 || ny<0 || nx>=h || ny>=w || a[nx][ny] == '.')continue;
if(us[nx][ny])continue;
us[nx][ny] = 1;
if(a[nx][ny] == a[x][y])q.push_front({dis, {nx, ny}});
else q.pb({dis+1,{nx,ny}});
}
}
cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int test = 1;
//cin >> test;
while(test--){
solve();
}
}
Compilation message (stderr)
tracks.cpp: In function 'void solve()':
tracks.cpp:43:15: warning: unused variable 'cnt' [-Wunused-variable]
43 | int ans = 1, cnt = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |