Advertisement
AlejandroGY

Untitled

Jan 2nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <utility>
  5.  
  6. struct solve {
  7.     int x, y, d, v;
  8.     solve(int x1, int y1, int d1, int v1)
  9.     : x(x1), y(y1), d(d1), v(v1)
  10.     {
  11.     }
  12. };
  13.  
  14. int bfs (int xi, int yi, int xf, int yf, int lobos, int n, char mat[102][102], bool visitado[102][102])
  15. {
  16.     std::queue<solve> cola;
  17.     solve temp(xi, yi, 0, lobos);
  18.     cola.push(temp);
  19.    
  20.     while (!cola.empty( )) {
  21.         solve actual = cola.front( );
  22.         cola.pop( );
  23.        
  24.         if (actual.x == xf && actual.y == yf) {
  25.             return actual.d;
  26.         }
  27.         if (visitado[actual.x][actual.y]) {
  28.             continue;
  29.         }
  30.         visitado[actual.x][actual.y] = true;
  31.        
  32.         std::pair<int, int> vecinos[4] = {
  33.             { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 }
  34.         };
  35.        
  36.         for (int i = 0; i < 4; ++i) {
  37.             int x = actual.x + vecinos[i].first;
  38.             int y = actual.y + vecinos[i].second;
  39.            
  40.             if (x >= 0 && x < n && y >= 0 && y < n && mat[x][y] != '#' && actual.v >= 0) {
  41.                 if (mat[x][y] == '*') {
  42.                     solve test(x, y, actual.d + 1, actual.v - 1);
  43.                     cola.push(test);
  44.                 } else {
  45.                     solve test(x, y, actual.d + 1, actual.v);
  46.                     cola.push(test);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.    
  52.     return -1;
  53. }
  54.  
  55. int main ( )
  56. {
  57.     std::ios_base::sync_with_stdio(0);
  58.     std::cin.tie(0);
  59.     std::cout.tie(0);
  60.    
  61.     int l, n;
  62.     std::cin >> l >> n;
  63.    
  64.     int xi, yi, xf, yf;
  65.     char mat[102][102];
  66.     for (int i = 0; i < n; ++i) {
  67.         for (int j = 0; j < n; ++j) {
  68.             std::cin >> mat[i][j];
  69.             if (mat[i][j] == 'E') {
  70.                 xi = i;
  71.                 yi = j;
  72.             } else if (mat[i][j] == 'S') {
  73.                 xf = i;
  74.                 yf = j;
  75.             }
  76.         }
  77.     }
  78.    
  79.     bool visitado[102][102];
  80.     std::fill(&visitado[0][0], &visitado[0][102], false);
  81.    
  82.     int res = bfs(xf, yf, xi, yi, l, n, mat, visitado);
  83.     std::cout << res << std::endl;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement