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

// D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
using PR = pair<int, int>;       // <h,w>
using T3 = tuple<int, int, int>; // <h,w,gid>

int main() {
  int H, W;
  cin >> H >> W;
  int C[2], D[2];
  cin >> C[0] >> C[1] >> D[0] >> D[1];
  C[0]--, C[1]--, D[0]--, D[1]--;
  string S[H];
  rp(i, 0, H) cin >> S[i];

  PR dhw[] = {//<h,w>
              {1, 0},  {0, -1},  {0, 1},   {-1, 0},  {2, -2},  {2, -1},
              {2, 0},  {2, 1},   {2, 2},   {1, -2},  {1, -1},  {1, 1},
              {1, 2},  {0, -2},  {0, 2},   {-1, -2}, {-1, -1}, {-1, 1},
              {-1, 2}, {-2, -2}, {-2, -1}, {-2, 0},  {-2, 1},  {-2, 2}};

  vvi vis(H, vi(W, -1));
  deque<T3> dq;
  dq.push_back({C[0], C[1], 0});
  while (dq.size()) {
    int h, w, gid;
    tie(h, w, gid) = dq.front();
    dq.pop_front();
    if (0 <= vis[h][w] and vis[h][w] <= gid)
      continue;
    // if(vis[h][w] >= 0) continue;
    // if(vis[h][w] <= gid) continue;
    if (S[h][w] == '#')
      continue;
    vis[h][w] = gid;
    rp(i, 0, 4) {
      int nx_h = h + dhw[i].first;
      int nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h)
        continue;
      if (nx_w < 0 || W <= nx_w)
        continue;
      dq.push_front({nx_h, nx_w, gid});
    }

    rp(i, 4, 24) {
      int nx_h = h + dhw[i].first;
      int nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h)
        continue;
      if (nx_w < 0 || W <= nx_w)
        continue;
      dq.push_back({nx_h, nx_w, gid + 1});
    }
  }

  // rp(h,0,H){
  // 	rp(w,0,W){
  // 		if(vis[h][w] < 0) cout<<"X ";
  // 		else cout<<""<<vis[h][w]<<" ";
  // 	}
  // 	puts("");
  // }
  // puts("---");

  cout << vis[D[0]][D[1]] << endl;
}


Error: TLE

Fixed submission:

// D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
using PR = pair<int, int>;       // <h,w>
using T3 = tuple<int, int, int>; // <h,w,gid>

int main() {
  int H, W;
  cin >> H >> W;
  int C[2], D[2];
  cin >> C[0] >> C[1] >> D[0] >> D[1];
  C[0]--, C[1]--, D[0]--, D[1]--;
  string S[H];
  rp(i, 0, H) cin >> S[i];

  PR dhw[] = {//<h,w>
              {1, 0},  {0, -1},  {0, 1},   {-1, 0},  {2, -2},  {2, -1},
              {2, 0},  {2, 1},   {2, 2},   {1, -2},  {1, -1},  {1, 1},
              {1, 2},  {0, -2},  {0, 2},   {-1, -2}, {-1, -1}, {-1, 1},
              {-1, 2}, {-2, -2}, {-2, -1}, {-2, 0},  {-2, 1},  {-2, 2}};

  vvi vis(H, vi(W, -1));
  deque<T3> dq;
  dq.push_back({C[0], C[1], 0});
  while (dq.size()) {
    int h, w, gid;
    tie(h, w, gid) = dq.front();
    dq.pop_front();
    if (0 <= vis[h][w] and vis[h][w] <= gid)
      continue;
    // if(vis[h][w] >= 0) continue;
    // if(vis[h][w] <= gid) continue;
    if (S[h][w] == '#')
      continue;
    vis[h][w] = gid;
    rp(i, 0, 4) {
      int nx_h = h + dhw[i].first;
      int nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h)
        continue;
      if (nx_w < 0 || W <= nx_w)
        continue;
      dq.push_front({nx_h, nx_w, gid});
    }

    rp(i, 4, 24) {
      int nx_h = h + dhw[i].first;
      int nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h)
        continue;
      if (nx_w < 0 || W <= nx_w)
        continue;
      dq.push_front({nx_h, nx_w, gid + 1});
    }
  }

  // rp(h,0,H){
  // 	rp(w,0,W){
  // 		if(vis[h][w] < 0) cout<<"X ";
  // 		else cout<<""<<vis[h][w]<<" ";
  // 	}
  // 	puts("");
  // }
  // puts("---");

  cout << vis[D[0]][D[1]] << endl;
}