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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    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 hr() { cerr << "--------------" << endl; }
const int INF = 1001001001;
const int MOD = 1000000007;

int status[1005][1005];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int h, w;
/*
void f() {
   rep(i, 0, h) {
      rep(j, 0, w) printf("%d ", status[i][j]);
      printf("\\n");
   }
   printf("=======================\\n");
}
*/
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> h >> w;
  P s, g;
  cin >> s.first >> s.second;
  s.first--, s.second--;
  cin >> g.first >> g.second;
  g.first--, g.second--;
  vector<string> maze(h);
  rep(i, 0, h) cin >> maze[i];

  rep(i, 0, h) rep(j, 0, w) status[i][j] = INF;

  deque<P> deq;
  deq.push_front(s);
  status[s.first][s.second] = 0;
  while (!deq.empty()) {
    P p = deq.front();
    deq.pop_front();
    int cx = p.first, cy = p.second;
    rep(i, 0, 4) {
      int nx = cx + dx[i], ny = cy + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (maze[nx][ny] == '#')
        continue;
      if (status[nx][ny] > status[cx][cy]) {
        status[nx][ny] = status[cx][cy];
        deq.push_front(P(nx, ny));
        // f();
      }
    }
    rep(i, -2, 3) {
      rep(j, -2, 3) {
        int nx = cx + i, ny = cy + j;
        if (nx == cx && ny == cy)
          continue;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (maze[nx][ny] == '#')
          continue;
        if (status[nx][ny] > status[cx][cy] + 1) {
          status[nx][ny] = status[cx][cy] + 1;
          deq.emplace_back(nx, ny);
          // f();
        }
      }
    }
  }
  int ans = status[g.first][g.second];
  if (status[g.first][g.second] == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    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 hr() { cerr << "--------------" << endl; }
const int INF = 1001001001;
const int MOD = 1000000007;

int status[1005][1005];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int h, w;
/*
void f() {
   rep(i, 0, h) {
      rep(j, 0, w) printf("%d ", status[i][j]);
      printf("\\n");
   }
   printf("=======================\\n");
}
*/
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> h >> w;
  P s, g;
  cin >> s.first >> s.second;
  s.first--, s.second--;
  cin >> g.first >> g.second;
  g.first--, g.second--;
  vector<string> maze(h);
  rep(i, 0, h) cin >> maze[i];

  rep(i, 0, h) rep(j, 0, w) status[i][j] = INF;

  queue<P> q;
  q.push(s);
  status[s.first][s.second] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int cx = p.first, cy = p.second;
    rep(i, 0, 4) {
      int nx = cx + dx[i], ny = cy + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (maze[nx][ny] == '#')
        continue;
      if (status[nx][ny] > status[cx][cy]) {
        status[nx][ny] = status[cx][cy];
        q.push(P(nx, ny));
        // f();
      }
    }
    rep(i, -2, 3) {
      rep(j, -2, 3) {
        int nx = cx + i, ny = cy + j;
        if (nx == cx && ny == cy)
          continue;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (maze[nx][ny] == '#')
          continue;
        if (status[nx][ny] > status[cx][cy] + 1) {
          status[nx][ny] = status[cx][cy] + 1;
          q.push(P(nx, ny));
          // f();
        }
      }
    }
  }
  int ans = status[g.first][g.second];
  if (status[g.first][g.second] == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}