Advertisement
Josif_tepe

Untitled

May 12th, 2024
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int n, m;
  6.     cin >> n >> m;
  7.  
  8.     int si, sj, ei, ej;
  9.     char mat[n][m];
  10.     bool visited1[n][m], visited2[n][m], visited3[n][m];
  11.     for(int i = 0; i < n; i++) {
  12.         for(int j = 0; j < m; j++) {
  13.             cin >> mat[i][j];
  14.  
  15.             if(mat[i][j] == 'P') {
  16.                 si = i;
  17.                 sj = j;
  18.             }
  19.             if(mat[i][j] == 'K') {
  20.                 ei = i;
  21.                 ej = j;
  22.             }
  23.             visited1[i][j] = false;
  24.             visited2[i][j] = false;
  25.             visited3[i][j] = false;
  26.         }
  27.     }
  28.  
  29.     int di1[] = {-1, 1, 0, 0};
  30.     int dj1[] = {0, 0, -1, 1};
  31.     int di2[] = {-2, 2, 0, 0};
  32.     int dj2[] = {0, 0, -2, 2};
  33.     int di3[] = {-3, 3, 0, 0};
  34.     int dj3[] = {0, 0, -3, 3};
  35.  
  36.  
  37.     queue<int> q;
  38.     q.push(si);
  39.     q.push(sj);
  40.     q.push(1);
  41.     q.push(0);
  42.  
  43.     while(!q.empty()) {
  44.         int ci = q.front();
  45.         q.pop();
  46.         int cj = q.front();
  47.         q.pop();
  48.         int vid_na_cekor = q.front();
  49.         q.pop();
  50.         int cekor = q.front();
  51.         q.pop();
  52.  
  53.         if(ci == ei and cj == ej) {
  54.             cout << cekor << endl;
  55.             break;
  56.         }
  57.         if(vid_na_cekor == 1) {
  58.             for(int i = 0; i < 4; i++) {
  59.                 int ti = ci + di1[i];
  60.                 int tj = cj + dj1[i];
  61.  
  62.                 if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited1[ti][tj]) {
  63.                     q.push(ti);
  64.                     q.push(tj);
  65.                     q.push(2);
  66.                     q.push(cekor + 1);
  67.                     visited1[ti][tj] = true;
  68.                 }
  69.             }
  70.         }
  71.         else if(vid_na_cekor == 2) {
  72.             for(int i = 0; i < 4; i++) {
  73.                 int ti = ci + di1[i];
  74.                 int tj = cj + dj1[i];
  75.                 if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#') {
  76.                     ti = ci + di2[i];
  77.                     tj = cj + dj2[i];
  78.  
  79.                     if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited2[ti][tj]) {
  80.                         q.push(ti);
  81.                         q.push(tj);
  82.                         q.push(3);
  83.                         q.push(cekor + 1);
  84.                         visited2[ti][tj] = true;
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         else {
  90.             for(int i = 0; i < 4; i++) {
  91.                 int ti = ci + di1[i];
  92.                 int tj = cj + dj1[i];
  93.                 if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#') {
  94.                     ti = ci + di2[i];
  95.                     tj = cj + dj2[i];
  96.                     if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#') {
  97.                         ti = ci + di3[i];
  98.                         tj = cj + dj3[i];
  99.                         if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited3[ti][tj]) {
  100.                             q.push(ti);
  101.                             q.push(tj);
  102.                             q.push(1);
  103.                             q.push(cekor + 1);
  104.                             visited3[ti][tj] = true;
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     return 0;
  113. }
  114. /*
  115.  
  116. 3
  117. ..#K#
  118. .##.#
  119. .....
  120. ...P.
  121. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement