Advertisement
AlejandroGY

Untitled

Jan 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <utility>
  5. #include <algorithm>
  6.  
  7. struct solve {
  8.    int x, y, d;
  9.    solve(int x_, int y_, int d_)
  10.    : x(x_), y(y_), d(d_)
  11.    {
  12.    }
  13. };
  14.  
  15. int main( )
  16. {
  17.    std::ios_base::sync_with_stdio(false);
  18.    std::cin.tie(nullptr);
  19.    std::cout.tie(nullptr);
  20.    //freopen("in.txt", "r", stdin);
  21.  
  22.    int n, m, k;
  23.    std::cin >> n >> m >> k;
  24.  
  25.    bool memo[n][m];
  26.    std::fill(&memo[0][0], &memo[n][0], false);
  27.    char mapa[n][m];
  28.    std::vector<std::pair<int, int>> especiales;
  29.    std::pair<int, int> mario, peach;
  30.  
  31.    for (int i = 0; i < n; ++i) {
  32.       for (int j = 0; j < m; ++j) {
  33.          std::cin >> mapa[i][j];
  34.          if (mapa[i][j] == 'M') {
  35.             mario = { i, j };
  36.          } else if (mapa[i][j] == 'P') {
  37.             peach = { i, j };
  38.          } else if (mapa[i][j] == 'E') {
  39.             especiales.push_back({ i, j });
  40.          }
  41.       }
  42.    }
  43.  
  44.    /*BFS*/
  45.    int res = 0, pasos = 0;
  46.    std::queue<solve> cola;
  47.    cola.push(solve( mario.first, mario.second, 0 ));
  48.  
  49.    while (!cola.empty( )) {
  50.       ++pasos;
  51.       solve actual = cola.front( );
  52.       cola.pop( );
  53.  
  54.       if (actual.x == peach.first && actual.y == peach.second) {
  55.          res = actual.d;
  56.          break;
  57.       }
  58.       if (memo[actual.x][actual.y]) {
  59.          continue;
  60.       }
  61.       memo[actual.x][actual.y] = true;
  62.  
  63.       if (pasos == k) {
  64.          for (auto vecino : especiales) {
  65.             if (!memo[vecino.first][vecino.second]) {
  66.                cola.push(solve( vecino.first, vecino.second, k ));
  67.             } else if () {
  68.            
  69.             }
  70.          }
  71.       }
  72.  
  73.       for (auto vecino : std::initializer_list<std::pair<int, int>>{ { actual.x + 1, actual.y }, { actual.x - 1, actual.y }, { actual.x, actual.y + 1 }, { actual.x, actual.y - 1 } }) {
  74.          if (vecino.first >= 0 && vecino.first < n && vecino.second >= 0 && vecino.second < m && !memo[vecino.first][vecino.second]) {
  75.             if (mapa[vecino.first][vecino.second] != '#') {
  76.                cola.push(solve( vecino.first, vecino.second, actual.d + 1 ));
  77.             } else {
  78.                cola.push(solve( vecino.first, vecino.second, actual.d + 2 ));
  79.             }
  80.          }
  81.       }
  82.    }
  83.    /*Fin BFS*/
  84.  
  85.  
  86.    std::cout << res << "\n";
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement