#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(v) v.begin(),v.end()
#define pb push_back
bool dfsF(int i , int j , vector <vector <char>> &grid , vector<pair<int,int>> &edges){
// if ((i == grid.size() - 1) && (j = grid[0].size() - 1)){
// return true;
// }
int ans = false;
for (auto [x , y] : edges){
int xx = x + i;
int yy = y + j;
if (xx >= 0 && xx < grid.size() && yy >= 0 && yy < grid[0].size() && grid[xx][yy] != '.' && grid[xx][yy] != 'R'){
grid[xx][yy] = 'R';
ans = ans | dfsF(xx , yy , grid , edges);
}
}
return ans;
}
bool dfsR(int i , int j , vector <vector <char>> &grid , vector<pair<int,int>> &edges){
int ans = false;
for (auto [x , y] : edges){
int xx = x + i;
int yy = y + j;
if (xx >= 0 && xx < grid.size() && yy >= 0 && yy < grid[0].size() && grid[xx][yy] != '.'){
grid[xx][yy] = '.';
ans = ans | dfsR(xx , yy , grid , edges);
}
}
return ans;
}
void solve(){
int h , w; cin >> h >> w;
vector <vector<char>> grid(h , vector <char> (w , '.'));
for (int i = 0; i<h; i++){
for (int j = 0; j<w; j++){
char c; cin >> c;
grid[i][j] = c;
}
}
vector<pair<int,int>> edges = {{1,0} , {-1 , 0} , {0 , 1} , {0 , -1}};
int counter = 0;
for (int i = 0; i<h; i++){
for (int j = 0; j<w; j++){
if (grid[i][j] == 'F'){
bool resp = dfsF( i , j , grid , edges);
counter ++;
}
}
}
for (int i = 0; i<h; i++){
for (int j = 0; j<w; j++){
if (grid[i][j] == 'R'){
bool resp = dfsR( i , j , grid , edges);
counter ++;
}
}
}
cout << counter;
}
int32_t main(){
// int n; cin >> n;
int n = 1;
for (int i = 0; i<n; i++){
solve();
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |