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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w;
  cin >> h >> w;
  int cy, cx, ty, tx;
  cin >> cy >> cx >> ty >> tx;
  --cy, --cx, --ty, --tx;
  char s[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> s[i][j];
  int seen[h][w];
  rep(i, 0, h) rep(j, 0, w) seen[i][j] = -1;
  seen[cy][cx] = 0;
  deque<pint> q;
  q.push_front({cy, cx});
  while (!q.empty()) {
    int y = q.front().first, x = q.front().second;
    q.pop_front();
    rep(i, 0, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w || s[ny][nx] == '#')
        continue;
      if (seen[ny][nx] == -1) {
        seen[ny][nx] = seen[y][x];
        q.push_front({ny, nx});
      } else if (seen[ny][nx] > seen[y][x]) {
        seen[ny][nx] = seen[y][x];
        q.push_front({ny, nx});
      }
    }
    rep(i, -2, 3) {
      rep(j, -2, 3) {
        int ny = y + i, nx = x + j;
        if (ny < 0 || nx < 0 || ny >= h || nx >= w || s[ny][nx] == '#')
          continue;
        if (seen[ny][nx] == -1) {
          seen[ny][nx] = seen[y][x] + 1;
          q.push_back({ny, nx});
        } else if (seen[ny][nx] > seen[y][x] + 1) {
          seen[ny][nx] = seen[y][x] + 1;
          q.push_back({ny, nx});
        }
      }
    }
  }
  /*
  rep(i, 0, h) {
      rep(j, 0, w) cout << seen[i][j] << " ";
      cout << "\\n";
  }
  */
  cout << seen[ty][tx] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w;
  cin >> h >> w;
  int cy, cx, ty, tx;
  cin >> cy >> cx >> ty >> tx;
  --cy, --cx, --ty, --tx;
  char s[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> s[i][j];
  int seen[h][w];
  rep(i, 0, h) rep(j, 0, w) seen[i][j] = -1;
  seen[cy][cx] = 0;
  queue<pint> q;
  q.push({cy, cx});
  while (!q.empty()) {
    while (!q.empty()) {
      int y = q.front().first, x = q.front().second;
      q.pop();
      rep(i, 0, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || nx < 0 || ny >= h || nx >= w || s[ny][nx] == '#')
          continue;
        if (seen[ny][nx] == -1) {
          seen[ny][nx] = seen[y][x];
          q.push({ny, nx});
        } else if (seen[ny][nx] > seen[y][x]) {
          seen[ny][nx] = seen[y][x];
          q.push({ny, nx});
        }
      }
    }
    rep(y, 0, h) {
      rep(x, 0, w) {
        if (seen[y][x] == -1)
          continue;
        rep(i, -2, 3) {
          rep(j, -2, 3) {
            int ny = y + i, nx = x + j;
            if (ny < 0 || nx < 0 || ny >= h || nx >= w || s[ny][nx] == '#')
              continue;
            if (seen[ny][nx] == -1) {
              seen[ny][nx] = seen[y][x] + 1;
              q.push({ny, nx});
            } else if (seen[ny][nx] > seen[y][x] + 1) {
              seen[ny][nx] = seen[y][x] + 1;
              q.push({ny, nx});
            }
          }
        }
      }
    }
  }
  /*
  rep(i, 0, h) {
      rep(j, 0, w) cout << seen[i][j] << " ";
      cout << "\\n";
  }
  */
  cout << seen[ty][tx] << "\\n";
}