Advertisement
Josif_tepe

Untitled

Mar 20th, 2024
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     int m;
  10.     cin>>n>>m;
  11.     int n1;
  12.     int m1;
  13.     int k;
  14.     int k1;
  15.  
  16.  
  17.     queue<int>q;
  18.     char matrica[n][m];
  19.     for(int x=0; x<n; x++){
  20.         for(int x1=0; x1<m; x1++){
  21.             cin>>matrica[x][x1];
  22.         }
  23.     }
  24.     for(int y=0; y<n; y++){
  25.         for(int y1=0; y1<m; y1++){
  26.         if(matrica[y][y1]=='P'){
  27.         n1=y;
  28.         m1=y1;
  29.         }
  30.         if(matrica[y][y1]=='K'){
  31.         k=y;
  32.         k1=y1;
  33.         }
  34.         }
  35.     }
  36.     bool matrica1[n][m];
  37.     bool matrica2[n][m];
  38.     bool matrica3[n][m];
  39.     for(int l=0; l<n; l++){
  40.         for(int l1=0; l1<m; l1++){
  41.             matrica1[l][l1]=false;
  42.             matrica2[l][l1]=false;
  43.             matrica3[l][l1]=false;
  44.         }
  45.     }
  46.     q.push(n1);
  47.     q.push(m1);
  48.     q.push(0);
  49.     q.push(1);
  50.  
  51.     int di1[]={-1,1,0,0,};
  52.     int dj1[]={0,0,-1,1};
  53.     int di2[]={-2,2,0,0};
  54.     int dj2[]={0,0,-2,2};
  55.     int di3[]={-3,3,0,0};
  56.     int dj3[]={0,0,-3,3};
  57.  
  58.     int ti;
  59.     int tj;
  60.  
  61.     while(!q.empty()){
  62.     int ci=q.front();
  63.     q.pop();
  64.     int cj=q.front();
  65.     q.pop();
  66.     int cekori=q.front();
  67.     q.pop();
  68.     int brcekori=q.front();
  69.     q.pop();
  70.     if((ci==k)and(cj==k1)){
  71.         cout<<cekori;
  72.         break;
  73.     }
  74.     if(brcekori==1){
  75.     for(int i=0; i<4; i++){
  76.     ti=ci+di1[i];
  77.     tj=cj+dj1[i];
  78.     if((ti>=0)and(ti<n)){
  79.         if((tj>=0)and(tj<m)){
  80.             if(matrica[ti][tj]!='#'){
  81.                 if(matrica1[ti][tj]==false){
  82.                  q.push(ti);
  83.                  q.push(tj);
  84.                  q.push(cekori+1);
  85.                  q.push(brcekori=2);
  86.                  matrica1[ti][tj]=true;
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     }
  92.     }
  93.     else if(brcekori==2){
  94.     for(int i=0; i<4; i++){
  95.         ti=ci+di1[i];
  96.         tj=cj+dj1[i];
  97.         if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
  98.         ti=ci+di2[i];
  99.         tj=cj+dj2[i];
  100.         if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')and(matrica2[ti][tj]==false)){
  101.         q.push(ti);
  102.         q.push(tj);
  103.         q.push(cekori+1);
  104.         q.push(brcekori=3);
  105.         matrica2[ti][tj]=true;
  106.         }
  107.     }
  108.  
  109.     }
  110.     }
  111.     else if(brcekori==3){
  112.     for(int i=0; i<4; i++){
  113.         ti=ci+di1[i];
  114.         tj=cj+dj1[i];
  115.         if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
  116.         ti=ci+di2[i];
  117.         tj=cj+dj2[i];
  118.         if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')){
  119.         ti=ci+di3[i];
  120.         tj=cj+dj3[i];
  121.         if((ti>=0)and(ti<n)and(tj>=0)and(tj<m)and(matrica[ti][tj]!='#')and(matrica3[ti][tj]==false)){
  122.         q.push(ti);
  123.         q.push(tj);
  124.         q.push(cekori+1);
  125.         q.push(brcekori=1);
  126.         matrica3[ti][tj]=true;
  127.         }
  128.         }
  129.     }
  130.     }
  131.     }
  132.     }
  133.      
  134.  
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement