Advertisement
Josif_tepe

Untitled

Feb 26th, 2024
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cmath>
  4. #include <set>
  5. #include <vector>
  6. #include <queue>
  7. using namespace std;
  8. const int maxn = 1050;
  9. char mat[maxn][maxn];
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n, m, k;
  13.     cin >> n >> m >> k;
  14.     int si, sj, ei, ej;
  15.     for(int i = 0; i < n; i++) {
  16.         for(int j = 0; j < m; j++) {
  17.             cin >> mat[i][j];
  18.             if(mat[i][j] == 'M') {
  19.                 si = i;
  20.                 sj = j;
  21.             }
  22.             if(mat[i][j] == 'V') {
  23.                 ei = i;
  24.                 ej = j;
  25.             }
  26.         }
  27.     }
  28.     mat[si][sj] = '.';
  29.     mat[ei][ej] = '.';
  30.     int di[] = {-1, 1, 0, 0};
  31.     int dj[] = {0, 0, -1, 1};
  32.      
  33.     vector<vector<bool> > visited(n + 1, vector<bool> (m + 1, false));
  34.     visited[si][sj] = true;
  35.     queue<int> q;
  36.     q.push(si);
  37.     q.push(sj);
  38.     vector<pair<int, int> > points_from_walls;
  39.     while(!q.empty()) {
  40.         int ci = q.front(); q.pop();
  41.         int cj = q.front(); q.pop();
  42.         if(ci == ei and cj == ej) {
  43.             cout << "DA" << endl;
  44.             return 0;
  45.         }
  46.         for(int i = 0; i < 4; i++) {
  47.             int ti = ci + di[i];
  48.             int tj = cj + dj[i];
  49.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  50.                 if(mat[ti][tj] == '#') {
  51.                     points_from_walls.push_back(make_pair(ci, cj));
  52.                 }
  53.                 else {
  54.                     visited[ti][tj] = true;
  55.                     q.push(ti);
  56.                     q.push(tj);
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     vector<vector<int> > dist(n + 1, vector<int>(m + 1, -1));
  62.  
  63.     for(pair<int, int> x : points_from_walls) {
  64.         q.push(x.first);
  65.         q.push(x.second);
  66.         dist[x.first][x.second] = 0;
  67.     }
  68.      
  69.     dist[si][sj] = 0;
  70.     while(!q.empty()) {
  71.         int ci = q.front(); q.pop();
  72.         int cj = q.front(); q.pop();
  73.          
  74.         for(int i = 0; i < 4; i++) {
  75.             int ti = ci + di[i];
  76.             int tj = cj + dj[i];
  77.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and dist[ti][tj] == -1) {
  78.                 dist[ti][tj] = dist[ci][cj] + 1;
  79.                 q.push(ti);
  80.                 q.push(tj);
  81.             }
  82.         }
  83.          
  84.     }
  85.     q.push(ei);
  86.     q.push(ej);
  87.     for(int i = 0; i < n; i++) {
  88.         for(int j = 0; j < m; j++) {
  89.             visited[i][j] = false;
  90.         }
  91.     }
  92.     visited[ei][ej] = true;
  93.     while(!q.empty()) {
  94.         int ci = q.front(); q.pop();
  95.         int cj = q.front(); q.pop();
  96.         if(mat[ci][cj] == '.' and dist[ci][cj] <= k) {
  97.             cout << "DA" << endl;
  98.             return 0;
  99.         }
  100.         for(int i = 0; i < 4; i++) {
  101.             int ti = ci + di[i];
  102.             int tj = cj + dj[i];
  103.             if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited[ti][tj]) {
  104.                 visited[ti][tj] = true;
  105.                 q.push(ti);
  106.                 q.push(tj);
  107.             }
  108.         }
  109.     }
  110.      
  111.     cout << "NE" << endl;
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement