제출 #535534

#제출 시각아이디문제언어결과실행 시간메모리
535534Koful123Tracks in the Snow (BOI13_tracks)C++17
100 / 100
838 ms890684 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const int N = 4005;
int vis[N][N],cnt,n,m,ans;
string s[N];
queue<pair<int,int>> q,tmp;

void dfs(int x,int y,char c){
	if(x >= n || y >= m || y < 0 || x < 0) return;
	if(vis[x][y]) return;
	if(!vis[x][y] && s[x][y] != c){
		tmp.push({x,y});
		return;
	}

	if(!vis[x][y]){
		vis[x][y] = 1;
		cnt++;
	}

	dfs(x+1,y,c);
	dfs(x,y+1,c);
	dfs(x,y-1,c);
	dfs(x-1,y,c);
}

void solve(){		

	cin >> n >> m;

	for(int i=0;i<n;i++)
		cin >> s[i];

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(s[i][j] == '.'){
				vis[i][j] = 1;
				cnt++;
			}
		}
	}

	char c = s[n-1][m-1];
	q.push({n-1,m-1});

	while(cnt != n*m){
		while(q.size()){
			auto tp = q.front();
			dfs(tp.ff,tp.ss,c);
			q.pop();
		}

		while(tmp.size()){
			q.push(tmp.front());
			tmp.pop();
		}

		if(c == 'F') c = 'R';
		else c = 'F';
		ans++;
	}

	cout << ans << endl;
}

signed main(){

	ios_base::sync_with_stdio(0);	
	cin.tie(0);	

	#ifndef ONLINE_JUDGE
	//	freopen("in.txt","r",stdin);
	//	freopen("out.txt","w",stdout);
	#endif
 
	int t = 1;
	//cin >> t;

 	for(int i=1;i<=t;i++){
 		solve();
	}

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...