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

#include <bits/stdc++.h>
using namespace std;

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI = 3.14159265358979323846;

int N_MAX = 1030;
int h, w;
int ch, cw;
int dh, dw;
vector<string> m(N_MAX);
vector<vector<int>> res(N_MAX, vector<int>(N_MAX, INT_MAX));
vector<int> X = {-1, 0, 0, 1};
vector<int> Y = {0, 1, -1, 0};
queue<pair<int, int>> q, visited;

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

int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  rp(i, 0, h) cin >> m[i];
  ch--;
  cw--;
  dh--;
  dw--;
  res[ch][cw] = 0;
  q.emplace(ch, cw);
  while (!q.empty() || !visited.empty()) {
    if (!q.empty()) {
      int now_x = q.front().first;
      int now_y = q.front().second;
      q.pop();
      visited.push(make_pair(now_x, now_y));
      rp(i, 0, 4) {
        int next_x = now_x + X[i];
        int next_y = now_y + Y[i];
        if (is_in_map(next_x, next_y)) {
          if (m[next_x][next_y] == '.') {
            if (chmin(res[next_x][next_y], res[now_x][now_y]))
              q.push(make_pair(next_x, next_y));
          }
        }
      }
    } else {
      int x = visited.front().first;
      int y = visited.front().second;
      visited.pop();
      rp(i, -2, 3) {
        rp(j, -2, 3) {
          if (is_in_map(x + i, y + j)) {
            if (m[x + i][y + j] == '.') {
              if (chmin(res[x + i][y + j], res[x][y] + 1))
                q.push(make_pair(x + i, y + j));
            }
          }
        }
      }
    }
  }
  // rp(i, 0, h) {
  //     rp(j, 0, w) {
  //         if(res[i][j] == INT_MAX) printf("-1 ");
  //         else printf("%d ", res[i][j]);
  //     }
  //     cout << endl;
  // }
  if (res[dh][dw] == INT_MAX)
    cout << -1 << endl;
  else
    cout << res[dh][dw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI = 3.14159265358979323846;

int N_MAX = 1030;
int h, w;
int ch, cw;
int dh, dw;
vector<string> m(N_MAX);
vector<vector<int>> res(N_MAX, vector<int>(N_MAX, INT_MAX));
vector<int> X = {-1, 0, 0, 1};
vector<int> Y = {0, 1, -1, 0};
stack<pair<int, int>> st;

bool is_in_map(int i, int j) {
  return 0 <= i && i < h && 0 <= j && j < w && m[i][j] == '.';
}

void solve(int i, int j) {
  rp(k, 0, 4) {
    int x = X[k];
    int y = Y[k];
    if (is_in_map(i + x, j + y)) {
      if (chmin(res[i + x][j + y], res[i][j]))
        solve(i + x, j + y);
    }
  }
  rp(k, -2, 3) {
    rp(l, -2, 3) {
      if (is_in_map(i + k, j + l)) {
        if (chmin(res[i + k][j + l], res[i][j] + 1))
          st.emplace(i + k, j + l);
      }
    }
  }
}

int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  rp(i, 0, h) cin >> m[i];
  ch--;
  cw--;
  dh--;
  dw--;
  res[ch][cw] = 0;
  st.emplace(ch, cw);
  while (!st.empty()) {
    int i = st.top().first;
    int j = st.top().second;
    st.pop();
    solve(i, j);
  }
  // rp(i, 0, h) {
  //     rp(j, 0, w) {
  //         if(res[i][j] == INT_MAX) printf("-1 ");
  //         else printf("%d ", res[i][j]);
  //     }
  //     cout << endl;
  // }
  if (res[dh][dw] == INT_MAX)
    cout << -1 << endl;
  else
    cout << res[dh][dw] << endl;
  return 0;
}