Submission #585164

#TimeUsernameProblemLanguageResultExecution timeMemory
585164darshitjnTracks in the Snow (BOI13_tracks)C++14
100 / 100
675 ms235836 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
//using namespace __gnu_pbds;
#define gc getchar_unlocked
#define fo(i,n) for(ll i=0;i<n;i++)
#define Fo(i,k,n) for(ll i=k;i<n;i++)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%I64d",&x)
#define ss(s)	scanf("%s",s)
#define pb push_back
#define F first
#define S second
#define clr(x) memset(x, 0, sizeof(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define all(x) x.begin(), x.end()
#define PI 3.1415926535897932384626
#define deb(x) cout << #x << " " << x << "\n";
#define INP(v) for(auto &x:v){cin >> x;}
#define OUT(v) for(auto &x:v){cout << x << " ";}
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pll>		vpll;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
const ll mod = 1000000007;
const ll N = 2e5;
const ll Fact_Length = 5e5 + 100;
//template<class T> using oset =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;

// A.find_by_order(k-1) // gives kth smallest element
// A.order_of_key(x) // no. of elements which are less than x

//Takes a&b as input and Returns : The power (a,b), (a^b)%mod
/*ll Power(ll base, ll expo)
{
   ll $result=1; base%=mod;
   while(expo) {   if(expo%2==1)  $result=($result*base)%mod;     base=(base*base)%mod;     expo/=2; }
   return $result;
}
 
// It give the modulo inverse of a, (1/a)%mod
ll Mod_Inv(ll $a) { return Power($a,mod-2); }
 
ll Factorial[Fact_Length];
// It make the above Factorial[i] = i! array
ll Make_Factorial()
{
   Factorial[0]=1;
   for(ll i=1;i<Fact_Length;i++) { Factorial[i]=(i*Factorial[i-1])%mod; } return 0;
}
ll Implement_Make_Factorial=Make_Factorial();      //To Implement Make_Factorial
 
// Takes n&r as input and Returns : nCr%mod
ll nCr(ll $n,ll $r)
{
   if($n<$r || $n<0 || $r<0) return 0;
   //if($n>Fact_Length) { cout<<"Error"<<endl; return; }
   ll $ans=(Factorial[$n]*Mod_Inv(Factorial[$r]))%mod;
   $ans=($ans*Mod_Inv(Factorial[$n-$r]))%mod;
   return $ans;
}*/

/*void seive(ll n){           
    for(ll i=2; i*i<=n; i++){
        if(a[i]==0){
            for(ll j=i; i*j<=n; j++){
                a[i*j] = 1;
            }
        }
    }
}*/

/*ll pw(ll a,ll b){
    if(b==0)return 1;
    ll t=pw(a,b/2);
    if(b%2)return ((a*t*t)%mod);
    else return ((t*t)%mod);
}
*/
ll depth[4000][4000];
string snow[4000];
ll dx[] = {0,0,1,-1};
ll dy[] = {1,-1,0,0};
ll n,m;

bool valid(ll x,ll y){
    if(x<0 || x>n-1){return false;}
    if(y<0 || y>m-1){return false;}
    return (snow[x][y] != '.');
}

void solve(){
    cin >> n >> m;
    fo(i,n){
        cin >> snow[i];
    }
    deque <pll> q;
    q.pb({0,0});
    ll ans = 1;
    depth[0][0] = 1;
    while(!q.empty()){
        pll p = q.front();
        q.pop_front();
        ans = max(ans,depth[p.F][p.S]);
        fo(i,4){
            ll x = p.F+dx[i];
            ll y = p.S+dy[i];
            if(valid(x,y) && depth[x][y]==0){
                if(snow[x][y]==snow[p.F][p.S]){
                    q.push_front({x,y});
                    depth[x][y] = depth[p.F][p.S];
                }
                else{
                    q.push_back({x,y});
                    depth[x][y] = depth[p.F][p.S]+1;
                }
            }
        }
    }
    cout << ans << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    
    ll darshit = 1;
	//cin >> darshit;
    
    for(ll i=1;i<=darshit;i++)
    {
    //cout <<"Case #" << i <<": ";  
    solve();
    }


	return 0;
}

Compilation message (stderr)

tracks.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("unroll-loops")
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...