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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

int H, W;
int D[2];
int mp[1000][1000];

int n_added;
int added[1000 * 1000];

int find(int y, int x, int level) {
  if (y == D[1] && x == D[0])
    return level;

  if (mp[y][x] != -1)
    return -1;
  // cout << "(" << x << "," << y << ")" << level << endl;
  mp[y][x] = level;
  added[n_added++] = y * 1000 + x;

  if (0 < x) {
    int v = find(y, x - 1, level);
    if (0 <= v)
      return v;
  }
  if (0 < y) {
    int v = find(y - 1, x, level);
    if (0 <= v)
      return v;
  }
  if (x < W - 1) {
    int v = find(y, x + 1, level);
    if (0 <= v)
      return v;
  }

  if (y < H - 1) {
    int v = find(y + 1, x, level);
    if (0 <= v)
      return v;
  }

  return -1;
}

void execute() {
  int C[2];
  string S[1000];

  cin >> H >> W;
  cin >> C[1] >> C[0];
  C[0]--;
  C[1]--;
  cin >> D[1] >> D[0];
  D[0]--;
  D[1]--;
  for (int y = 0; y < H; y++) {
    cin >> S[y];
    for (int x = 0; x < W; x++) {
      mp[y][x] = (S[y][x] == '.' ? -1 : -2);
      // cout << -mp[y][x];
    }
    // cout << endl;
  }

  int level = 0;
  int n_que = 0;
  int deque[1000 * 1000];

  deque[n_que++] = C[1] * 1000 + C[0];
  do {
    n_added = 0;
    for (int i = 0; i < n_que; i++) {
      int v = find(deque[i] / 1000, deque[i] % 1000, level);
      if (0 <= v) {
        cout << v << endl;
        return;
      }
    }

    n_que = 0;
    for (int i = 0; i < n_added; i++) {
      int y = added[i] / 1000;
      int x = added[i] % 1000;
      for (int dy = -2; dy <= 2; dy++) {
        int yy = y + dy;
        if (yy < 0 || H <= yy)
          continue;
        for (int dx = -2; dx <= 2; dx++) {
          int xx = x + dx;
          if (xx < 0 || W <= xx)
            continue;
          if (mp[yy][xx] == -1) {
            deque[n_que++] = yy * 1000 + xx;
          }
        }
      }
    }
    n_added = 0;

    level++;
  } while (0 < n_que);

  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

int H, W;
int D[2];
int mp[1000][1000];

int find(int y, int x, int level) {
  if (y == D[1] && x == D[0])
    return level;

  if (mp[y][x] != -1)
    return -1;
  // cout << "(" << x << "," << y << ")" << level << endl;
  mp[y][x] = level;

  if (0 < x) {
    int v = find(y, x - 1, level);
    if (0 <= v)
      return v;
  }
  if (0 < y) {
    int v = find(y - 1, x, level);
    if (0 <= v)
      return v;
  }
  if (x < W - 1) {
    int v = find(y, x + 1, level);
    if (0 <= v)
      return v;
  }

  if (y < H - 1) {
    int v = find(y + 1, x, level);
    if (0 <= v)
      return v;
  }

  return -1;
}

void execute() {
  int C[2];
  string S[1000];

  cin >> H >> W;
  cin >> C[1] >> C[0];
  C[0]--;
  C[1]--;
  cin >> D[1] >> D[0];
  D[0]--;
  D[1]--;
  for (int y = 0; y < H; y++) {
    cin >> S[y];
    for (int x = 0; x < W; x++) {
      mp[y][x] = (S[y][x] == '.' ? -1 : -2);
      // cout << -mp[y][x];
    }
    // cout << endl;
  }

  int level = 0;
  int n_que = 0;
  int deque[1000 * 1000];

  deque[n_que++] = C[1] * 1000 + C[0];
  do {
    for (int i = 0; i < n_que; i++) {
      int v = find(deque[i] / 1000, deque[i] % 1000, level);
      if (0 <= v) {
        cout << v << endl;
        return;
      }
    }

    n_que = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (mp[y][x] == level) {
          for (int dy = -2; dy <= 2; dy++) {
            int yy = y + dy;
            if (yy < 0 || H <= yy)
              continue;
            for (int dx = -2; dx <= 2; dx++) {
              int xx = x + dx;
              if (xx < 0 || W <= xx)
                continue;
              if (mp[yy][xx] == -1) {
                deque[n_que++] = yy * 1000 + xx;
              }
            }
          }
        }
      }
    }
    level++;
  } while (0 < n_que);

  cout << -1 << endl;
}