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 forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
#define fi first
#define se second
#define pb push_back
#define pu push
#define all(a) a.begin(),a.end()
#define getbit(mask,i) ((mask>>(i))&1)
#define minimize(a,b) (a)=min((a),(b))
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll maxN=5000+5;
const ll mod=1e9+7;
const ll oo=1e18;
const int tx[4]={0,1,0,-1};
const int ty[4]={1,0,-1,0};
bool vis[maxN][maxN];
char a[maxN][maxN];
int n,m,ans;
void change(int x, int y)
{
if (a[x][y]=='R') a[x][y]='F';
else a[x][y]='R';
return;
}
bool outmap(int x, int y)
{
if (1<=x && x<=n)
if (1<=y && y<=m) return 0;
return 1;
}
int bfs()
{
queue<pii> p;
memset(vis,0,sizeof vis);
p.pu({1,1});
vis[1][1]=1;
char c=a[1][1];
change(1,1);
int cnt=1;
while (p.size())
{
int x=p.front().fi;
int y=p.front().se;
p.pop();
forw(i,0,3)
{
int xx=x+tx[i];
int yy=y+ty[i];
if (!vis[xx][yy] && !outmap(xx,yy) && a[xx][yy]==c)
{
change(xx,yy);
vis[xx][yy]=1;
cnt++;
p.pu({xx,yy});
}
}
}
return cnt;
}
void solve()
{
cin>>n>>m;
forw(i,1,n)
forw(j,1,m) cin>>a[i][j];
int cnt=0;
while (1<2)
{
int tmp=bfs();
if (cnt==tmp) break;
cnt=tmp;
ans++;
}
cout<<ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("bruh.inp","r",stdin);
//freopen("bruh.out","w",stdout);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |