This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |