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>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<utility>
#include<set>
#include<unordered_set>
#include<list>
#include<iterator>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<random>
#include<map>
#include<unordered_map>
#include<stdio.h>
#include<complex>
#include<math.h>
#include<cstring>
#include<chrono>
#include<string>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize("fast-math")
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define vi vector <int>
#define vvi vector <vi>
#define pii pair<int, int>
#define ppi pair<pii, int>
#define vii vector<pii>
#define mii map<int, int>
#define mci map<char, int>
#define miv map<int, vi>
#define mis map<int, set<int>>
#define setbits(n) __builtin_popcount(n)
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define in(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define show2(a, b) cout<<a<<' '<<b<<endl
#define show3(a, b, c) cout<<a<<' '<<b<<' '<<c<<endl
#define show(arr) for (auto i:arr) cout<<i<<' ';
#define Endl endl
const long long N=1e5+5;
const long long mod=1000000007; //998244353;
int h,w,ans,val,c;
vector<string> s;
vector<vector<bool>> vis;
bool inrange(int x, int y)
{
return x>=0 && x<h && y>=0 && y<w;
}
bool bfs(int sx, int sy)
{
ans++;
queue<pii> q;
int a[]={-1,0,0,1};
int b[]={0,-1,1,0};
q.push({sx,sy});
while(q.size())
{
auto p=q.front();q.pop();
int x=p.ff,y=p.ss;
// show2(x,y);
vis[x][y]=true;
s[x][y]=char(val-c);
for(int k=0;k<4;k++)
{
int u=x+a[k],v=y+b[k];
if(inrange(u,v) && s[u][v]==c)
q.push({u,v});
}
}
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)if(s[i][j]!='.' && !vis[i][j])return false;
}
return true;
}
void cases(){
cin>>h>>w;
val='R'+'F';
ans=0;
s.resize(h);in(s,h);
c=s[0][0];
vis.resize(h,vector<bool>(w));
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)if(s[i][j]==c)
{
if(bfs(i,j))
{
cout<<ans<<endl;
return;
}
c=char(val-c);
// for(auto x:s)
// {
// cout<<x<<endl;
// }
}
}
cout<<ans<<endl;
cout<<ans<<endl;
}
int32_t main(){
std::ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t=1;
// cin>>t;
for (int i=0; i<t; i++){
//cout<<"Case #"<<i+1<<": ";
cases();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |