Josif_tepe

Untitled

Aug 27th, 2025
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. const int maxn = 305;
  7. char mat[maxn][maxn];
  8.  
  9. bool visited[maxn][maxn][4];
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n, m;
  13.     cin >> n >> m;
  14.    
  15.     int si, sj, ei, ej;
  16.     for(int i = 0; i < n; i++) {
  17.         for(int j = 0; j < m; j++) {
  18.             cin >> mat[i][j];
  19.            
  20.             if(mat[i][j] == 'P') {
  21.                 si = i;
  22.                 sj = j;
  23.             }
  24.             if(mat[i][j] == 'K') {
  25.                 ei = i;
  26.                 ej = j;
  27.             }
  28.         }
  29.     }
  30.     memset(visited, false, sizeof visited);
  31.    
  32.     queue<int> q;
  33.     q.push(si);
  34.     q.push(sj);
  35.     q.push(1);
  36.     q.push(0);
  37.    
  38.     int di[] = {-1, 1, 0, 0};
  39.     int dj[] = {0, 0, -1, 1};
  40.     while(!q.empty()) {
  41.         int ci = q.front(); q.pop();
  42.         int cj = q.front(); q.pop();
  43.         int step = q.front(); q.pop();
  44.         int shortest_dist = q.front(); q.pop();
  45.        
  46.         if(ci == ei and cj == ej) {
  47.             cout << shortest_dist << endl;
  48.             return 0;
  49.         }
  50.        
  51.         for(int i = 0; i < 4; i++) {
  52.             bool ok = true;
  53.             for(int j = 1; j <= step; j++) {
  54.                 int ti = di[i] * j + ci;
  55.                 int tj = dj[i] * j + cj;
  56.                
  57.                 if(ti < 0 or ti >= n or tj < 0 or tj >= m or mat[ti][tj] == '#') {
  58.                     ok = false;
  59.                     break;
  60.                 }
  61.                
  62.                
  63.             }
  64.             if(ok) {
  65.                 int new_step = step + 1;
  66.                 if(new_step > 3) {
  67.                     new_step = 1;
  68.                 }
  69.                
  70.                 int ti = di[i] * step + ci;
  71.                 int tj = dj[i] * step + cj;
  72.                
  73.                 if(!visited[ti][tj][new_step]) {
  74.                     q.push(ti);
  75.                     q.push(tj);
  76.                     q.push(new_step);
  77.                     q.push(shortest_dist + 1);
  78.                    
  79.                     visited[ti][tj][new_step] = true;
  80.                 }
  81.             }
  82.         }
  83.        
  84.     }
  85.    
  86.    
  87.    
  88.     return 0;
  89. }
  90. /*
  91.  P......
  92.  #.##K..
  93.  .#.#..#
  94.  ##..##.
  95.  
  96.  di[] = {-1, 1, 0, 0}
  97.  dj[] = {0, 0, -1, 1}
  98.  
  99.  cekor = 3
  100.  
  101.  
  102.  **/
  103.  
Advertisement
Add Comment
Please, Sign In to add comment