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 <iostream>
#include <queue>
#define NMAX 4000
using namespace std;
int n, m, viz[NMAX+10][NMAX+10], v[NMAX+10][NMAX+10], ans;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
queue <pair <int, int> > Q[2], auxQ;
void init_bfs()
{ while(!auxQ.empty())
{ pair <int, int> a = auxQ.front();
auxQ.pop();
for(int i=0; i<4; i++)
{ pair <int, int> vec = {dx[i] + a.first, dy[i] + a.second};
if(vec.first && vec.first <= n && vec.second && vec.second <= m
&& !viz[vec.first][vec.second] && v[vec.first][vec.second] == v[1][1])
{ viz[vec.first][vec.second] = 1;
auxQ.push(vec);
Q[0].push(vec);
}
}
}
}
void bfs(char ch, int t)
{ if(Q[t].empty()) return;
ans++;
while(!Q[t].empty())
{ pair <int, int> a = Q[t].front();
Q[t].pop();
for(int i=0; i<4; i++)
{ pair <int, int> vec = {dx[i] + a.first, dy[i] + a.second};
if(vec.first && vec.first <= n && vec.second && vec.second <= m
&& !viz[vec.first][vec.second] && v[vec.first][vec.second] == ch)
{ viz[vec.first][vec.second] = 1;
Q[t].push(vec);
Q[1-t].push(vec);
}
}
}
if(ch == 'F') bfs('R', 1 - t);
else bfs('F', 1 - t);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++)
{ string s;
cin >> s;
for(int j=0; j<m; j++) v[i][j+1] = s[j];
}
auxQ.push({1, 1});
Q[0].push({1, 1});
viz[1][1] = 1;
init_bfs();
if(v[1][1] == 'F') bfs('R', 0);
else bfs('F', 0);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |