Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // dequef
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using ll = long long;
using namespace std;
int movex[] = {0, 0, -1, 1};
int movey[] = {-1, 1, 0, 0};
int warpx[] = {-2, -1, 0,  1, 2, -2, -1, 0, 1,  2,
               -2, -2, -2, 2, 2, 2,  -1, 1, -1, 1};
int warpy[] = {-2, -2, -2, -2, -2, 2, 2,  2,  2, 2,
               -1, 0,  1,  -1, 0,  1, -1, -1, 1, 1};
int main() {
  int inf = 100000000;
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vvi cnt(h, vi(w, inf));
  rep(i, h) {
    rep(j, w) {
      char t;
      cin >> t;
      if (t == '#')
        cnt[i][j] = -1;
    }
  }
  deque<pii> que;
  cnt[ch][cw] = 0;
  que.push_back(make_pair(ch, cw));
  while (que.size()) {
    auto num = que.front();
    que.pop_front();
    int posy = num.first, posx = num.second;
    rep(i, 4) {
      int y = posy + movey[i], x = posx + movex[i];
      if (y >= 0 && x >= 0 && y < h && x < w) {
        if (cnt[y][x] > cnt[posy][posx]) {
          cnt[y][x] = cnt[posy][posx];
          que.push_front(make_pair(y, x));
        }
      }
    }
    rep(i, 20) {
      int y = posy + warpy[i], x = posx + warpx[i];
      if (y >= 0 && x >= 0 && y < h && x < w) {
        if (cnt[y][x] > cnt[posy][posx] + 1) {
          cnt[y][x] = cnt[posy][posx] + 1;
          que.push_back(make_pair(y, x));
        }
      }
    }
  }
  if (cnt[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << cnt[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // dequef
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using ll = long long;
using namespace std;
int movex[] = {0, 0, -1, 1};
int movey[] = {-1, 1, 0, 0};
int warpx[] = {-2, -1, 0,  1, 2, -2, -1, 0, 1,  2,
               -2, -2, -2, 2, 2, 2,  -1, 1, -1, 1};
int warpy[] = {-2, -2, -2, -2, -2, 2, 2,  2,  2, 2,
               -1, 0,  1,  -1, 0,  1, -1, -1, 1, 1};
int main() {
  int inf = 100000000;
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vvi cnt(h, vi(w, inf));
  rep(i, h) {
    rep(j, w) {
      char t;
      cin >> t;
      if (t == '#')
        cnt[i][j] = -1;
    }
  }
  deque<pii> que;
  cnt[ch][cw] = 0;
  que.push_back(make_pair(ch, cw));
  while (que.size()) {
    auto num = que.front();
    que.pop_front();
    int posy = num.first, posx = num.second;
    rep(i, 4) {
      int y = posy + movey[i], x = posx + movex[i];
      if (y >= 0 && x >= 0 && y < h && x < w) {
        if (cnt[y][x] > cnt[posy][posx]) {
          cnt[y][x] = cnt[posy][posx];
          que.push_front(make_pair(y, x));
        }
      }
    }
    rep(i, 20) {
      int y = posy + warpy[i], x = posx + warpx[i];
      if (y >= 0 && x >= 0 && y < h && x < w) {
        if (cnt[y][x] > cnt[posy][posx] + 1) {
          cnt[y][x] = cnt[posy][posx] + 1;
          que.push_front(make_pair(y, x));
        }
      }
    }
  }
  if (cnt[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << cnt[dh][dw] << endl;
}