Submission #857879

# Submission time Handle Problem Language Result Execution time Memory
857879 2023-10-07T06:30:08 Z MickeyIsAwesome Tracks in the Snow (BOI13_tracks) C++17
100 / 100
797 ms 210692 KB
#include <bits/stdc++.h> // Include every standard library
// Common file
#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<pll> vll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<ld> vd;
typedef vector<vd> vvd;
typedef pair<ll,pll> ppll;

double EPS = 1e-9;
int INF = 2000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = { -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8] = { 0, 1, -1, 0, -1, 1, -1, 1 };

#define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (ll(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (ll(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (ll(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define RESET(a, b) memset(a, b, sizeof(a))
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define tiii tuple<int,int,int>
#define mt make_tuple
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define MAX 10000100
// #define mo 998244353
#define mo 1000000007
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>


// Begin DEBUG //
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << '{' << p.first << ", " << p.second << '}';
}

template <class T, class = decay_t<decltype(*begin(declval<T>()))>,
        class = enable_if_t<!is_same<T, string>::value>>
ostream &operator<<(ostream &os, const T &c) {
os << '[';
for (auto it = c.begin(); it != c.end(); ++it)
    os << &", "[2 * (it == c.begin())] << *it;
return os << ']';
}

//support up to 5 args
#define _NTH_ARG(_1, _2, _3, _4, _5, _6, N, ...) N
#define _FE_0(_CALL, ...)
#define _FE_1(_CALL, x) _CALL(x)
#define _FE_2(_CALL, x, ...) _CALL(x) _FE_1(_CALL, __VA_ARGS__)
#define _FE_3(_CALL, x, ...) _CALL(x) _FE_2(_CALL, __VA_ARGS__)
#define _FE_4(_CALL, x, ...) _CALL(x) _FE_3(_CALL, __VA_ARGS__)
#define _FE_5(_CALL, x, ...) _CALL(x) _FE_4(_CALL, __VA_ARGS__)
#define FOR_EACH_MACRO(MACRO, ...)                                             \
_NTH_ARG(dummy, ##__VA_ARGS__, _FE_5, _FE_4, _FE_3, _FE_2, _FE_1, _FE_0)     \
(MACRO, ##__VA_ARGS__)
//Change output format here
#define out(x) #x " = " << x << "; "
#define debug(...)                                                              \
cout << "Line " << __LINE__ << ": " FOR_EACH_MACRO(out, __VA_ARGS__) << "\n"

// End DEBUG //

ll add(ll a, ll b, ll c = mo) {ll res=(a+b)%c; return res;}
ll mod_neg(ll a, ll b, ll c = mo) {ll res=(a-b+c)%c; return res;} 
ll mul(ll a, ll b, ll c = mo) {ll res=(a*b)%c; res=(res+c)%c; return res;}

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);

        if (hash1 != hash2) {
            return hash1 ^ hash2;             
        }
        
        // If hash1 == hash2, their XOR is zero.
        return hash1;
    }
};

int MOD = 1000000007;
struct mi {
    int v;
    explicit operator int() const { return v; }
    mi() { v = 0; }
    mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi &operator+=(mi &a, mi b) {
    if ((a.v += b.v) >= MOD) a.v -= MOD;
    return a;
}
mi &operator-=(mi &a, mi b) {
    if ((a.v -= b.v) < 0) a.v += MOD;
    return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); }
mi &operator*=(mi &a, mi b) { return a = a * b; }
mi pow(mi a, long long p) {
    assert(p >= 0);
    return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
}
mi operator/(mi a, mi b) { return a * inv(b); }


typedef vector<mi> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;

// Clear the stringstream
// ss.str("");
// ss.clear(); // Clear the stringstream state
// ss.str(str2);

///////////////////////////////////////////
/* Main starts here -> Mukund Krishnatrey*/
///////////////////////////////////////////

bool isValid(ll n, ll m, ll r, ll c){
    if(r>=0 && r<n && c>=0 && c<m) return true;
    else return false;
}

int main()
{   
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    if (fopen("threesum.in", "r")) {
        freopen("threesum.in", "r", stdin);
        freopen("threesum.out", "w", stdout);
    }
    cout<<fixed<<setprecision(10);
    // ll t_t; cin>>t_t;
    // while(t_t--){
        ll h,w; cin>>h>>w;
        vvc ar(h+1, vc(w+1));
        rep(i,h) rep(j,w) cin>>ar[i][j];
        deque<pll> dq;
        dq.pb(mp(0,0));
        vvl dist(h+1, vl(w+1, INF));
        dist[0][0]=1;
        ll r,c,nr,nc;
        vvb vis(h+1, vb(w+1,false));
        ll dirn[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
        while(!dq.empty()){
            auto p=dq.front(); dq.pop_front();
            r=p.ff; c=p.ss;
            if(vis[r][c]) continue;
            vis[r][c]=true;
            rep(i,4){
                nr=r+dirn[i][0]; nc=c+dirn[i][1];
                if(isValid(h,w,nr,nc) && ar[nr][nc]!='.'){
                    if(ar[nr][nc]==ar[r][c]){
                        if(dist[r][c]<dist[nr][nc]){
                            dist[nr][nc]=dist[r][c];
                            dq.pf(mp(nr,nc));
                        }
                    }
                    else{
                        if(dist[r][c]+1<dist[nr][nc]){
                            dist[nr][nc]=dist[r][c]+1;
                            dq.pb(mp(nr,nc));
                        }
                    }
                }
            }
        }
        ll ans=0;
        rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
        cout<<ans<<endl;
    // }
    return 0;
}   

// ** Don't use bitwise operators in conditional true and false checks.
 
// ** Always assert for sanity checks if value should be in a certain range.
 
// ** In getting n^p, power p shouldn't be reduced by mod. So when obtaining the value of p, don't use any mod operations.
 
// ** Instead of -1, INT_MIN, etc. use INFF and -INFF if long long values are allowed.
// Consider possible values if >INT_MAX ( or < INT_MIN) don't use it.
 
// Take care that when erasing values from vector/array/related containers, that the iterator position is gone after that. And even in map/set/multiset, that iterator has to be replaced, can't do it-- or it++ after erase.
 
// When comparing values, for eg:-, two vectors of equal size, if every value is equal, return false or return v[0]<v[1]. If you return true, then you get segmentation fault.
 
// Multiset value erase will remove all instances of that value, better to use a map
 
// Try to divide problem into subcases, and deal with them independently.
 
// If lexicographical, then definitely don't do any swaps in your code, as it will change the order
 
// Please read constraints carefully, you might end up overcomplicating the question if not.
 
// Always verify your solution with atleast the given examples, and some extra edge cases you can think of, before you start coding it
 
// Have a pseduo structure of code written down, (or in your mand), before writing the same
 
// Write code in batches, keep testing every independent function, before writing next part
 
// Check for long long, too many mods
 
// Always take a look at the examples if stuck, or can't come with a solution for a while, normally examples contain a hint.
 
 
// <------ For finding solution to adhoc problem ------> 
 
// ** Draw lots of small cases to gain a better understanding of the problem. If you're having trouble debugging, draw more cases. If you don't know how to start with a problem, draw more cases. Whenever you don't know how to further approach a problem, you're probably massing an important observation, so draw more cases and make observations about properties of the problem.
 
// ** Whenever you find an observation that seems useful, write it down! Writing down ideas lets you easily come back to them later, and makes sure you don't forget about ideas that could potentially be the solution.
 
// ** Don't get stuck on any specific idea, unless you see an entire solution.
 
// ** Try to approach the problem from a lot of different perspectives. Try to mess around with formulas or draw a visual depiction of the problem. One of the most helpful things you can do when solving ad hoc problems is to keep trying ideas until you make progress. This is something you get better at as you do more problems.

Compilation message

tracks.cpp: In function 'int main()':
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
tracks.cpp:188:9: note: in expansion of macro 'rep'
  188 |         rep(i,h) rep(j,w) cin>>ar[i][j];
      |         ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
tracks.cpp:188:18: note: in expansion of macro 'rep'
  188 |         rep(i,h) rep(j,w) cin>>ar[i][j];
      |                  ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
tracks.cpp:201:13: note: in expansion of macro 'rep'
  201 |             rep(i,4){
      |             ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
tracks.cpp:220:9: note: in expansion of macro 'rep'
  220 |         rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
      |         ^~~
tracks.cpp:39:29: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   39 | #define FOR(a, b, c) for (ll(a) = (b); (a) < (c); ++(a))
      |                             ^
tracks.cpp:45:19: note: in expansion of macro 'FOR'
   45 | #define rep(i, n) FOR(i, 0, n)
      |                   ^~~
tracks.cpp:220:18: note: in expansion of macro 'rep'
  220 |         rep(i,h) rep(j,w) if(ar[i][j]!='.') ans=max(ans,dist[i][j]);
      |                  ^~~
tracks.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         freopen("threesum.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |         freopen("threesum.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2648 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 6 ms 2332 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 408 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 860 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 5 ms 1348 KB Output is correct
13 Correct 2 ms 1116 KB Output is correct
14 Correct 2 ms 1116 KB Output is correct
15 Correct 11 ms 2652 KB Output is correct
16 Correct 14 ms 2764 KB Output is correct
17 Correct 9 ms 2652 KB Output is correct
18 Correct 7 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1372 KB Output is correct
2 Correct 47 ms 14428 KB Output is correct
3 Correct 315 ms 143740 KB Output is correct
4 Correct 89 ms 34064 KB Output is correct
5 Correct 201 ms 81224 KB Output is correct
6 Correct 783 ms 170432 KB Output is correct
7 Correct 2 ms 1376 KB Output is correct
8 Correct 2 ms 1372 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 1372 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 49 ms 14580 KB Output is correct
14 Correct 29 ms 8540 KB Output is correct
15 Correct 25 ms 9308 KB Output is correct
16 Correct 23 ms 6236 KB Output is correct
17 Correct 126 ms 36764 KB Output is correct
18 Correct 95 ms 36448 KB Output is correct
19 Correct 88 ms 34140 KB Output is correct
20 Correct 72 ms 31152 KB Output is correct
21 Correct 185 ms 84048 KB Output is correct
22 Correct 196 ms 81492 KB Output is correct
23 Correct 245 ms 69972 KB Output is correct
24 Correct 169 ms 81904 KB Output is correct
25 Correct 548 ms 143744 KB Output is correct
26 Correct 611 ms 210692 KB Output is correct
27 Correct 717 ms 180456 KB Output is correct
28 Correct 793 ms 170488 KB Output is correct
29 Correct 797 ms 165700 KB Output is correct
30 Correct 759 ms 175316 KB Output is correct
31 Correct 574 ms 93228 KB Output is correct
32 Correct 697 ms 177472 KB Output is correct