This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define node pair<int,int>
#define ordered_set tree<node, null_type,less<node>, rb_tree_tag,tree_order_statistics_node_update>
//order_of_key (k) : Number of items strictly smaller than k
//find_by_order(k) : K-th element in a set (counting from zero)
*/
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define ff first
#define endl '\n'
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define fr(i, s, e) for(int i = s; i < e; i++)
#define rfr(i, s, e) for(int i = s; i >= e; i--)
#define geta(a, n) for(int i = 0; i < n; i++) cin >> a[i];
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define all(x) x.begin(),x.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int dx[] = { -1, 1, 0, 0}; int dy[] = {0, 0, 1, -1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
/*------------------------------UNORDERED MAP HASH --------------------------------------------*/
//To make unordered_map unhackable
// use it as unordered_map<int,int,custom_hash> mapp;
/*
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
*/
/*------------------------------END--------------------------------------------*/
inline int powMod(int a, int b) { int x = 1; while (b > 0) { if (b & 1) x = (x * a) % MOD; a = (a * a) % MOD; b >>= 1; } return x; }
inline int multiply(int x, int y) { return ((x % MOD) * (y % MOD)) % MOD; }
inline int divide(int x, int y) { return ((x % MOD) * powMod(y % MOD, MOD - 2)) % MOD; }
inline int ceil(int a, int b) { return (a + b - 1) / b; }
int gcd (int a, int b) { while (b) { a %= b; swap(a, b); } return a; }
int lcm (int a, int b) { return a / gcd(a, b) * b; }
int inverseMod(int a, int m) { a = a % m; for (ll x = 1; x < m; x++) if ((a * x) % m == 1) return x; return -1; }
#ifdef GAURAVDEBUG
#include "C:\Users\gengi\OneDrive\Documents\debug.cpp"
#else
#define debug(...) ""
#endif
template<int D, typename T> struct vec : public vector < vec < D - 1, T >> { static_assert(D >= 1, "Vector dimension must be greater than zero!"); template<typename... Args> vec(int n = 0, Args... args) : vector < vec < D - 1, T >> (n, vec < D - 1, T > (args...)) { } }; template<typename T> struct vec<1, T> : public vector<T> { vec(int n = 0, T val = T()) : vector<T>(n, val) { }};
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
int h,w;
vector<string> mat;
void solve() {
cin >> h >> w;
mat.resize(h);
for (int i = 0; i < h; i++) {
cin >> mat[i];
}
deque<pii> dq;
dq.push_front({0, 0});
int ans = 0;
vector<vector<int>> dis(h, vector<int>(w, INT_MIN));
dis[0][0] = 1;
auto inside = [&](int x, int y)->bool{
return x >= 0 and y >= 0 and x < h and y < w and mat[x][y] != '.';
};
vector<vector<bool>> vis(h, vector<bool>(w, false));
debug(mat);
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int i = p.ff; int j = p.ss;
if (vis[i][j]) continue;
vis[i][j] = true;
ans=max(ans,dis[i][j]);
// cout<<"i "<<" "<<j<<" "<<endl;
for (int k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (inside(x, y) and !vis[x][y]) {
if (mat[x][y] == mat[i][j])
{
dq.push_front({x,y});
dis[x][y]=dis[i][j];
}
else
{
dq.push_back({x,y});
dis[x][y]=dis[i][j] + 1;
}
}
}
}
cout<<ans;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void solve()':
tracks.cpp:79:20: warning: statement has no effect [-Wunused-value]
79 | #define debug(...) ""
| ^~
tracks.cpp:108:5: note: in expansion of macro 'debug'
108 | debug(mat);
| ^~~~~
tracks.cpp: In function 'int32_t main()':
tracks.cpp:148:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen("error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |