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

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 2;
string a[N];

int main() {
  int h, w;
  cin >> h >> w;
  int sh, sw, eh, ew;
  cin >> sh >> sw >> eh >> ew;
  --sh;
  --sw;
  --eh;
  --ew;
  for (int i = 0; i < h; ++i)
    cin >> a[i];

  deque<pair<int, int>> q;
  q.push_front({sh, sw});
  vector<vector<int>> dist(h, vector<int>(w, 1e7));
  dist[sh][sw] = 0;
  while (!q.empty()) {
    auto top = q.front();
    q.pop_front();
    int x = top.second, y = top.first;
    for (int i = y - 2; i <= y + 2; ++i) {
      for (int j = x - 2; j <= x + 2; ++j) {
        if (i < 0 || i >= h || j < 0 || j >= w)
          continue;
        if (i == y && j == x)
          continue;
        if (a[i][j] == '#')
          continue;
        int w = 1;
        if ((abs(y - i) == 1 && !(x - j)) || abs(x - j) == 1 && !(y - i))
          w = 0;
        if (dist[i][j] > dist[y][x] + w) {
          dist[i][j] = w + dist[y][x];
          if (w)
            q.push_back({i, j});
          else
            q.push_front({i, j});
        }
      }
    }
  }
  if (dist[eh][ew] == (int)1e7)
    cout << -1;
  else
    cout << dist[eh][ew];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N = 1e3 + 2;
int h, w;
int cx, cy, dtx, dty;

bool check(int i, int j) { return i > 0 && i <= h && j > 0 && j <= w; }

string a[N];
int gr[N][N];
int da[][2] = {{-2, 0},  {-2, 1},  {-2, 2}, {-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}};

int timer = 1;
int dt[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
void dfs(int i, int j) {
  if (a[i][j - 1] == '#')
    return;
  if (gr[i][j] != 0)
    return;
  gr[i][j] = timer;
  for (int k = 0; k < 4; ++k) {
    int y = i + dt[k][0], x = j + dt[k][1];
    if (check(y, x))
      dfs(y, x);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> h >> w;
  cin >> cy >> cx >> dty >> dtx;
  int mod = 1e4;
  for (int i = 1; i <= h; ++i)
    cin >> a[i];
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (gr[i][j] == 0 && a[i][j - 1] != '#') {
        dfs(i, j);
        ++timer;
      }
    }
  }
  queue<int> q;
  vector<int> q1, tmp;
  q.push(cy * mod + cx);
  set<int> st;
  st.insert(gr[cy][cx]);
  st.insert(0);
  int res = -1;
  for (int warp = 0; !q.empty(); ++warp) {
    for (int s = q.size(); s > 0; --s) {
      q1.push_back(q.front());
      q.pop();
      for (int level = 0; q1.size(); ++level) {
        tmp.clear();
        for (int sz = 0; sz < q1.size(); ++sz) {
          int top = q1[sz];
          int x = top % mod, y = top / mod;
          if (x == dtx && y == dty) {
            res = warp;
          }
          a[y][x - 1] = '#';
          for (int k = 0; k < 4; ++k) {
            int dy = y + dt[k][0], dx = x + dt[k][1];
            if (check(dy, dx) && a[dy][dx - 1] != '#')
              tmp.push_back(dy * mod + dx);
          }
          for (int k = 0; k < 20; ++k) {
            int dy = y + da[k][0], dx = x + da[k][1];
            if (check(dy, dx) && a[dy][dx - 1] != '#' &&
                !st.count(gr[dy][dx])) {
              // cout << x << " " << y << " " << dx << " " << dy << endl;
              q.push(dy * mod + dx);
              st.insert(gr[dy][dx]);
            }
          }
        }
        q1 = tmp;
      }
    }
  }
  cout << res;
}