Advertisement
Mehulcoder

Confluent2020A

Oct 7th, 2020
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using pll = pair<ll, ll>;
  10. #define f first
  11. #define s second
  12. #define vll vector<long long>
  13. #define vvll vector<vector<ll>>
  14. #define vset(v, n, val) v.clear(); v.resize(n, val)
  15. #define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
  16.  
  17. vector<string> grid;
  18. set<pll> vis;
  19. ll n, m, k;
  20.  
  21. ll dx[4] = {0, 1, 0, -1};
  22. ll dy[4] = {1, 0, -1, 0};
  23.  
  24. bool isOk(ll i, ll j) {
  25.     return (i < n && j < m && i >= 0 && j >= 0 && grid[i][j] == '.');
  26. }
  27.  
  28. void solve() {
  29.     cin >> n;
  30.     cin >> k;
  31.  
  32.     vset(grid, n, "");
  33.     vis.clear();
  34.     rep(i, n) cin >> grid[i];
  35.     m = grid[0].size();
  36.  
  37.     queue<vll> q;
  38.     //x, y, dis
  39.     q.push({0, 0, 0});
  40.     while (!q.empty()) {
  41.         auto temp = q.front();
  42.         q.pop();
  43.         if (temp[0] == n - 1 && temp[1] == m - 1) {
  44.             cout << ((temp[2] <= k) ? "Yes" : "No") << '\n';
  45.             return;
  46.         }
  47.         if (vis.find({temp[0], temp[1]}) != vis.end()) continue;
  48.         vis.insert({temp[0], temp[1]});
  49.  
  50.         rep(k, 4) {
  51.             ll ni = temp[0] + dx[k];
  52.             ll nj = temp[1] + dy[k];
  53.  
  54.             if (isOk(ni, nj)) q.push({ni, nj, temp[2] + 1});
  55.         }
  56.     }
  57.  
  58.     cout << "No" << '\n';
  59.     return;
  60. }
  61.  
  62. int main(int argc , char ** argv) {
  63.     ios_base::sync_with_stdio(false) ;
  64.     cin.tie(NULL) ;
  65.  
  66.     ll t = 1;
  67.     while (t--) {
  68.         solve();
  69.     }
  70.  
  71.     return 0 ;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement