#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
int n, m;
vector<vector<bool>> visited;
vector<vector<char>> mtx;
queue<ii> q;
// dr, dc, mov
tuple<int, int, char> moves[]{{1, 0, 'D'}, {-1, 0, 'U'}, {0, 1, 'R'}, {0, -1, 'L'}};
bool is_possible(ii pos){
auto [r, c] = pos;
if(r < 0 || r >= n || c < 0 || c >= m) return false;
return true;
}
int bfs(ii src){
int qtt = 1;
char sy = mtx[src.first][src.second];
q.emplace(src);
while(!q.empty()){
ii curr = q.front();
q.pop();
visited[curr.first][curr.second] = true;
for(auto [dr, dc, mov] : moves){
int new_r = curr.first + dr;
int new_c = curr.second + dc;
if(is_possible({new_r, new_c})){
if(visited[new_r][new_c]) continue;
if(mtx[new_r][new_c] == '.') continue;
if(mtx[new_r][new_c] != sy) qtt = 2;
visited[new_r][new_c] = true;
q.emplace(new_r, new_c);
}
}
}
return qtt;
}
void solve(){
// int n, m;
cin >> n >> m;
vector<ii> cands;
mtx.assign(n, vector<char>(m));
visited.assign(n, vector<bool>(m, false));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> mtx[i][j];
if(mtx[i][j] == 'R' || mtx[i][j] == 'F'){
cands.emplace_back(i, j);
}
}
}
int ans = 0;
for(auto [a, b] : cands){
if(!visited[a][b]){
ans += bfs({a, b});
}
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |