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 <fstream>
#include <iostream>
#include <vector>
#define grid(n,m) for (ll i=1;i<=n;i++){for (ll j=1;j<=m;j++) cin>>graph[i][j];}
#define vll vector<ll>
#define qll queue<ll>
#define pll pair<ll,ll>
#define str string
#define pb push_back
#define ll long long
using namespace std;
const ll inf=2*1e5+1;
const ll graph_size=4000;
// Fast Input/Output
void fastio(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
}
// File Input/Output
str fileio(const string&filePath=__FILE__){
size_t lastSlash=filePath.find_last_of('/');
size_t lastDot=filePath.rfind('.');
return filePath.substr(lastSlash+1,lastDot-lastSlash-1);
}
ll n,m;
// Floodfill
char graph[graph_size+2][graph_size+2];
vector<pll> directions={{0,-1},{-1,0},{1,0},{0,1}};
vector<pll> path;
vector<pll> path1;
ll curr=0;
void floodfill_dfs(pll coord,char color){
ll x=coord.first;
ll y=coord.second;
if (graph[x][y]=='.' or graph[x][y]!=color){
return;
}
curr++;
graph[x][y]='.';
path1.pb({x,y});
for (auto i:directions){
floodfill_dfs({x+i.first,y+i.second},color);
}
}
int main(){
// auto start_time=chrono::steady_clock::now();
fastio();
// str filename=fileio();
// ifstream cin(filename+".in");
// ofstream cout(filename+".out");
ll t=1;
// cin>>t;
while (t--){
cin>>n>>m;
grid(n,m);
ll tracks=0;
for (ll i=1;i<=n;i++){
for (ll j=1;j<=m;j++){
if (graph[i][j]!='.'){
tracks++;
}
}
}
for (ll i=0;i<=n;i++){
graph[i][0]='.';
graph[i][m+1]='.';
}
for (ll i=0;i<=m;i++){
graph[0][i]='.';
graph[n+1][i]='.';
}
char animal=graph[1][1];
ll ans=0;
path.pb({n,m});
while (curr<tracks){
ans++;
for (auto i:path){
for (auto j:directions){
floodfill_dfs({i.first+j.first,i.second+j.second},animal);
}
}
path=path1;
path1.clear();
if (animal=='F'){
animal='R';
}
else{
animal='F';
}
}
cout<<ans<<"\n";
}
// auto end_time=chrono::steady_clock::now();
// auto elapsed_time=chrono::duration_cast<chrono::milliseconds>(end_time-start_time);
// cout<<"Elapsed time: "<<elapsed_time.count()<<" milliseconds\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |