Advertisement
Josif_tepe

Untitled

May 13th, 2022
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int redovi, koloni;
  7.     cin >> redovi >> koloni;
  8.    
  9.     char lavirint[redovi][koloni];
  10.     int poseteno[redovi][koloni], poseteno2[redovi][koloni], poseteno3[redovi][koloni];
  11.     int si, sj;
  12.     for(int i = 0; i < redovi; i++) {
  13.         for(int j = 0; j < koloni; j++) {
  14.             cin >> lavirint[i][j];
  15.             if(lavirint[i][j] == 'P') {
  16.                 si = i;
  17.                 sj = j;
  18.             }
  19.             poseteno[i][j] = 0;
  20.             poseteno2[i][j] = 0;
  21.             poseteno3[i][j] = 0;
  22.         }
  23.     }
  24.     int direction_i[] = {+1, -1, 0, 0};
  25.     int direction_j[] = {0,  0, +1,-1};
  26.    
  27.     int direction2_i[] = {+2, -2, 0, 0};
  28.     int direction2_j[] = {0, 0,  +2, -2};
  29.    
  30.     int direction3_i[] = {+3, -3, 0, 0};
  31.     int direction3_j[] = {0, 0,  +3, -3};
  32.     queue<int> Q;
  33.     Q.push(si);
  34.     Q.push(sj);
  35.     Q.push(1);
  36.     Q.push(0);
  37.     poseteno[si][sj] = 1;
  38.     while(Q.size() > 0) {
  39.         int ci = Q.front();
  40.         Q.pop();
  41.         int cj = Q.front();
  42.         Q.pop();
  43.         int tip_cekor = Q.front();
  44.         Q.pop();
  45.         int cekor = Q.front();
  46.         Q.pop();
  47.         cout << ci << " " << cj << endl;
  48.         if(lavirint[ci][cj] == 'K') {
  49.             cout << cekor << endl;
  50.             break;
  51.         }
  52.         if(tip_cekor == 1) {
  53.             for(int i = 0; i < 4; i++) {
  54.                 int ti = ci + direction_i[i];
  55.                 int tj = cj + direction_j[i];
  56.                 if(ti >= 0 and ti < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#' and poseteno[ti][tj] == 0) {
  57.                     Q.push(ti);
  58.                     Q.push(tj);
  59.                     Q.push(2);
  60.                     Q.push(cekor + 1);
  61.                     poseteno[ti][tj] = 1;
  62.                    
  63.                 }
  64.             }
  65.         }
  66.         else if(tip_cekor == 2) {
  67.             for(int i = 0; i < 4; i++) {
  68.                 int ti = ci + direction_i[i];
  69.                 int tj = cj + direction_j[i];
  70.                 if(ti >= 0 and ti < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#') {
  71.                     ti = ci + direction2_i[i];
  72.                     tj = cj + direction2_j[i];
  73.                     if(ti >= 0 and tj < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#' and poseteno2[ti][tj] == 0) {
  74.                         Q.push(ti);
  75.                         Q.push(tj);
  76.                         Q.push(3);
  77.                         Q.push(cekor + 1);
  78.                         poseteno2[ti][tj] = 1;
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         else if(tip_cekor == 3) {
  84.             for(int i = 0; i < 4; i++) {
  85.                 int ti = ci + direction_i[i];
  86.                 int tj = cj + direction_j[i];
  87.                 if(ti >= 0 and ti < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#') {
  88.                     ti = ci + direction2_i[i];
  89.                     tj = cj + direction2_j[i];
  90.                     if(ti >= 0 and ti < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#') {
  91.                         ti = ci + direction3_i[i];
  92.                         tj = cj + direction3_j[i];
  93.                         if(ti >= 0 and ti < redovi and tj >= 0 and tj < koloni and lavirint[ti][tj] != '#' and poseteno3[ti][tj] == 0) {
  94.                             Q.push(ti);
  95.                             Q.push(tj);
  96.                             Q.push(1);
  97.                             Q.push(cekor + 1);
  98.                             poseteno3[ti][tj] = 1;
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105.     return 0;
  106. }
  107. /*
  108.  4 7
  109.  P......
  110.  #.##K..
  111.  .#.#..#
  112.  ##..##.
  113.  
  114.  **/
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement