Submission #503894

#TimeUsernameProblemLanguageResultExecution timeMemory
503894zwickyTracks in the Snow (BOI13_tracks)C++14
14.27 / 100
2152 ms1048580 KiB
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define nl endl
#define loop(i,a,b) for(ll i=a;i<b;i++) 
#define rloop(i,a,b) for(ll i=b;i>=a;i--)
#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define cm cout<<-1<<endl
#define vl vector<ll>
#define vchar vector<char>
#define vvll vector<vector<ll>>
#define vvcr vector<vchar>
#define deb(x) cout<<#x<<" "<<x<<endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
using namespace std;

const ll MOD=1e9+7;

ll binepow(ll a,ll b,ll mod=-1){
	if(b==0){
		return 1ll;
	}
	if(mod!=-1){
		ll k=binepow(a,b/2,mod);
		if(b%2==0){
			return (k*k)%mod;
		}
		else{
			return (((k*a)%mod)*k)%mod;
		}
	}
	else{
		ll k=binepow(a,b/2,mod);
		if(b%2==0){
			return (k*k);
		}
		else{
			return k*k*a;
		}
	}
}

////////////////////////////////////////////////////////

void solve(){
	ll n,m;
	cin>>n>>m;
	vvcr a(n,vchar(m,'.'));
	ll total=0;
	loop(i,0,n){
		loop(j,0,m){
			cin>>a[i][j];
			if(a[i][j]!='.'){
				total++;
			}
		}
	}
	ll ans=0;
	char role=a[0][0];
	while (total!=0)
	{
		vvll vis(n,vl(m,0));
		queue<pair<ll,ll>> q;
		q.push({0,0});
		vis[0][0]=1;
		while (q.size()!=0)
		{
			if(a[q.front().first][q.front().second]==role){
				a[q.front().first][q.front().second]='x';
				total--;
			}
			ll x=q.front().first,y=q.front().second;
			vis[x][y]=1;
			if(x>0 && (a[x-1][y]==role || a[x-1][y]=='x' ) && vis[x-1][y]==0){
				q.push({x-1,y});
			}
			if(y>0 && (a[x][y-1]==role || a[x][y-1]=='x' ) && vis[x][y-1]==0){
				q.push({x,y-1});
			}
			if(x<n-1 && (a[x+1][y]==role || a[x+1][y]=='x' ) && vis[x+1][y]==0){
				q.push({x+1,y});
			}
			if(y<m-1 && (a[x][y+1]==role || a[x][y+1]=='x' ) && vis[x][y+1]==0){
				q.push({x,y+1});
			}
			q.pop();
		}
		ans++;
		if(role=='R'){
			role='F';
		}
		else{
			role='R';
		}
	}
	cout<<ans<<nl;
	return;
}

int main(){
   ios::sync_with_stdio(0);
   cin.tie(0);
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
	// ll t;
	// cin>>t;
	// while(t--){
	// 	solve();
	// }
	solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...