Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  std::cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  std::vector<std::string> S(H);
  for (int i = 0; i < H; i++) {
    std::cin >> S[i];
  }

  std::vector<std::vector<int>> warp_min(H, std::vector<int>(W, 1e9));

  std::deque<int> H_pos;
  std::deque<int> W_pos;
  std::deque<int> warp_cnt;

  H_pos.push_back(Ch - 1);
  W_pos.push_back(Cw - 1);
  warp_cnt.push_back(0);

  while (!H_pos.empty()) {
    int cur_H = H_pos[0];
    int cur_W = W_pos[0];
    int cur_warp = warp_cnt[0];
    H_pos.pop_front();
    W_pos.pop_front();
    warp_cnt.pop_front();

    if (cur_H < 0 || cur_H >= H) {
      continue;
    }
    if (cur_W < 0 || cur_W >= W) {
      continue;
    }
    if (warp_min[cur_H][cur_W] <= cur_warp) {
      continue;
    }
    if (S[cur_H][cur_W] == '#') {
      continue;
    }

    warp_min[cur_H][cur_W] = cur_warp;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int dist = abs(i) + abs(j);
        if (dist == 0) {
          continue;
        } else if (dist == 1) {
          H_pos.push_back(cur_H + i);
          W_pos.push_back(cur_W + j);
          warp_cnt.push_back(cur_warp);
        } else {
          H_pos.push_back(cur_H + i);
          W_pos.push_back(cur_W + j);
          warp_cnt.push_back(cur_warp + 1);
        }
      }
    }
  }

  // for(int i=0; i<H; i++){
  //     for(int j=0; j<W; j++){
  //         std::cout << warp_min[i][j] << " ";
  //     }
  //     std::cout << std::endl;
  // }

  if (warp_min[Dh - 1][Dw - 1] == 1e9) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << warp_min[Dh - 1][Dw - 1] << std::endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  std::cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  std::vector<std::string> S(H);
  for (int i = 0; i < H; i++) {
    std::cin >> S[i];
  }

  std::vector<std::vector<int>> warp_min(H, std::vector<int>(W, 1e9));

  std::deque<int> H_pos;
  std::deque<int> W_pos;
  std::deque<int> warp_cnt;

  H_pos.push_back(Ch - 1);
  W_pos.push_back(Cw - 1);
  warp_cnt.push_back(0);

  while (!H_pos.empty()) {
    int cur_H = H_pos[0];
    int cur_W = W_pos[0];
    int cur_warp = warp_cnt[0];
    H_pos.pop_front();
    W_pos.pop_front();
    warp_cnt.pop_front();

    if (cur_H < 0 || cur_H >= H) {
      continue;
    }
    if (cur_W < 0 || cur_W >= W) {
      continue;
    }
    if (warp_min[cur_H][cur_W] <= cur_warp) {
      continue;
    }
    if (S[cur_H][cur_W] == '#') {
      continue;
    }

    warp_min[cur_H][cur_W] = cur_warp;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int dist = abs(i) + abs(j);
        if (dist == 0) {
          continue;
        } else if (dist == 1) {
          H_pos.push_front(cur_H + i);
          W_pos.push_front(cur_W + j);
          warp_cnt.push_front(cur_warp);
        } else {
          H_pos.push_back(cur_H + i);
          W_pos.push_back(cur_W + j);
          warp_cnt.push_back(cur_warp + 1);
        }
      }
    }
  }

  // for(int i=0; i<H; i++){
  //     for(int j=0; j<W; j++){
  //         std::cout << warp_min[i][j] << " ";
  //     }
  //     std::cout << std::endl;
  // }

  if (warp_min[Dh - 1][Dw - 1] == 1e9) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << warp_min[Dh - 1][Dw - 1] << std::endl;
  }

  return 0;
}