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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

int d[1010][1010];

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];

  rep(i, h + 10) rep(j, w + 10) d[i][j] = INF;
  d[ch][cw] = 0;
  queue<P> que;
  que.push(P(cw, ch));
  while (que.size()) {
    P p = que.front();
    que.pop();
    queue<P> que2;
    que2.push(p);
    vector<P> v;
    v.push_back(p);
    while (que2.size()) {
      P p2 = que2.front();
      que2.pop();
      for (int my = -1; my <= 1; my++) {
        for (int mx = -1; mx <= 1; mx++) {
          if (abs(mx) + abs(my) != 1)
            continue;
          int nx = p2.first + mx, ny = p2.second + my;
          if (nx < 1 || w < nx || ny < 1 || h < ny)
            continue;
          if (s[ny - 1][nx - 1] == '#')
            continue;
          if (d[ny][nx] > d[p2.second][p2.first]) {
            que2.push(P(nx, ny));
            v.push_back(P(nx, ny));
            d[ny][nx] = d[p2.second][p2.first];
          }
        }
      }
    }
    for (auto x : v) {
      for (int my = -2; my <= 2; my++) {
        for (int mx = -2; mx <= 2; mx++) {
          if (mx == 0 && my == 0)
            continue;
          int nx = x.first + mx, ny = x.second + my;
          if (nx < 1 || w < nx || ny < 1 || h < ny)
            continue;
          if (s[ny - 1][nx - 1] == '#')
            continue;
          if (d[ny][nx] > d[x.second][x.first] + 1) {
            que.push(P(nx, ny));
            d[ny][nx] = d[x.second][x.first] + 1;
          }
        }
      }
    }
  }
  cout << (d[dh][dw] != INF ? d[dh][dw] : -1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

int d[1010][1010];

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];

  rep(i, h + 10) rep(j, w + 10) d[i][j] = INF;
  d[ch][cw] = 0;

  queue<P> que;
  que.push(P(cw, ch));
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int my = -2; my <= 2; my++) {
      for (int mx = -2; mx <= 2; mx++) {
        if (mx == 0 && my == 0)
          continue;
        int nx = p.first + mx, ny = p.second + my;
        if (nx < 1 || w < nx || ny < 1 || h < ny)
          continue;
        if (s[ny - 1][nx - 1] == '#')
          continue;
        if (d[ny][nx] > d[p.second][p.first] + 1) {
          que.push(P(nx, ny));
          d[ny][nx] = d[p.second][p.first] + 1;
        }
      }
    }
    for (int my = -1; my <= 1; my++) {
      for (int mx = -1; mx <= 1; mx++) {
        if (abs(mx) + abs(my) != 1)
          continue;
        int nx = p.first + mx, ny = p.second + my;
        if (nx < 1 || w < nx || ny < 1 || h < ny)
          continue;
        if (s[ny - 1][nx - 1] == '#')
          continue;
        if (d[ny][nx] > d[p.second][p.first]) {
          que.push(P(nx, ny));
          d[ny][nx] = d[p.second][p.first];
        }
      }
    }
  }

  cout << (d[dh][dw] != INF ? d[dh][dw] : -1) << endl;

  return 0;
}