Advertisement
sherry_ahmos

Untitled

Oct 14th, 2022
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. using namespace std;
  14.  
  15. #define ll long long
  16. #define nl endl
  17. #define cy cout << "YES\n"
  18. #define cn cout << "NO\n"
  19. #define sz s.size()
  20. #define allv v.begin(), v.end()
  21. void sherry()
  22. {
  23.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  24. #ifndef ONLINE_JUDGE
  25.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28. vector<vector<char>> grid;
  29. vector<vector<bool>> vis;
  30. int rows, cols, l, f, cnt = 0;
  31.  
  32. bool valid(int r, int c)
  33. {
  34.     return r < rows && c < cols && r >= 0 && c >= 0;
  35. }
  36. void find_path(int r, int c)
  37. {
  38.     if (!valid(r, c) || grid[r][c] == '*' || vis[r][c] == 1)
  39.         return;
  40.     vis[r][c] = 1;
  41.     cnt++;
  42.     find_path(r - 1, c);
  43.     find_path(r + 1, c);
  44.     find_path(r, c - 1);
  45.     find_path(r, c + 1);
  46. }
  47. void solve()
  48. {
  49.     cin >> rows >> cols;
  50.     vis.assign(rows, vector<bool>(cols));
  51.     grid.assign(rows, vector<char>(cols));
  52.     for (int i = 0; i < rows; i++)
  53.     {
  54.         for (int j = 0; j < cols; j++)
  55.         {
  56.             cin >> grid[i][j];
  57.         }
  58.     }
  59.     cin >> l >> f;
  60.     find_path(--l, --f);
  61.     cout << cnt << nl;
  62. }
  63. int main()
  64. {
  65.     sherry();
  66.     ll t = 1;
  67.     // cin >> t;
  68.     while (t--)
  69.     {
  70.         solve();
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement