Advertisement
AlejandroGY

Minecraft

Apr 20th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. struct solve {
  4.     int x, y, d;
  5.     solve(int x1, int y1, int d1)
  6.     : x(x1), y(y1), d(d1)
  7.     {
  8.     }
  9. };
  10.  
  11. constexpr int MAX = 502;
  12.  
  13. int n, m;
  14. int s, t, u, v;
  15. char mat[MAX][MAX];
  16. int memo[MAX][MAX];
  17.  
  18. void bfs( ) {
  19.     std::queue<solve> cola;
  20.     cola.push(solve( s, t, 0 ));
  21.    
  22.     do {
  23.         solve actual = cola.front( );
  24.         cola.pop( );
  25.        
  26.         if (actual.x == u && actual.y == v) {
  27.             memo[u][v] = std::min(memo[u][v], actual.d);
  28.         }
  29.        
  30.         std::pair<int, int> vecinos[] = {
  31.             { actual.x + 1, actual.y },
  32.             { actual.x - 1, actual.y },
  33.             { actual.x, actual.y + 1 },
  34.             { actual.x, actual.y - 1 }
  35.         };
  36.        
  37.         for (auto it : vecinos) {
  38.             int dx = it.first;
  39.             int dy = it.second;
  40.             if (dx >= 1 && dx <= n && dy >= 1 && dy <= m && mat[dx][dy] != '#') {
  41.                 if (mat[dx][dy] == '.' && (actual.d + 1) < memo[dx][dy]) {
  42.                     cola.push(solve( dx, dy, actual.d + 1 ));
  43.                     memo[dx][dy] = actual.d + 1;
  44.                 } else if (mat[dx][dy] != '.') {
  45.                     int costo = (mat[dx][dy] - '0') + 1;
  46.                     if (actual.d + costo < memo[dx][dy]) {
  47.                         cola.push(solve( dx, dy, actual.d + costo ));
  48.                         memo[dx][dy] = actual.d + costo;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     } while (!cola.empty( ));
  54. }
  55.  
  56. int main( ) {
  57.     std::ios_base::sync_with_stdio(0);
  58.     std::cin.tie(0);
  59.  
  60.     std::fill(&memo[0][0], &memo[MAX][0], INT_MAX);
  61.  
  62.     std::cin >> n >> m >> s >> t >> u >> v;
  63.     for (int i = 1; i <= n; ++i) {
  64.         for (int j = 1; j <= m; ++j) {
  65.             char op;
  66.             std::cin >> op;
  67.             mat[i][j] = op;
  68.         }
  69.     }
  70.     bfs( );
  71.     std::cout << memo[u][v] << '\n';
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement