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

#include "bits/stdc++.h"
using namespace std;
#define PIGRECO 3.141592653589793

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
#define int long long
const int maxn = 1e3 + 3;
const int INF = 1e18;

char grid[maxn][maxn];

bool visited[maxn][maxn];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(grid, '#', sizeof(grid));

  // memset(dist, INF, sizeof(dist));

  int h, w;
  cin >> h >> w;

  pair<int, int> start;
  pair<int, int> end;
  cin >> start.first >> start.second;
  cin >> end.first >> end.second;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }

  deque<pair<int, int>> q;

  vector<vector<int>> dist(h + 1, vector<int>(w + 1, INF));

  q.push_front(start);

  dist[start.first][start.second] = 0;

  while (!q.empty()) {

    pair<int, int> v = q.front();

    q.pop_front();

    // visited[v.first][v.second] = true;
    bool nxt = false;

    for (int i = 0; i < 4; i++) {

      if (v.first + dx[i] >= 1 && v.second + dy[i] >= 1 &&
          v.first + dx[i] <= h && v.second + dy[i] <= w &&
          grid[v.first + dx[i]][v.second + dy[i]] != '#') {

        if (dist[v.first][v.second] < dist[v.first + dx[i]][v.second + dy[i]]) {

          dist[v.first + dx[i]][v.second + dy[i]] = dist[v.first][v.second];

          q.push_front({v.first + dx[i], v.second + dy[i]});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {

        if (v.first + i >= 1 && v.second + j >= 1 && v.first + i <= h &&
            v.second + j <= w && grid[v.first + i][v.second + j] != '#') {

          if (dist[v.first][v.second] + 1 < dist[v.first + i][v.second + j]) {
            q.push_back({v.first + i, v.second + j});
            dist[v.first + i][v.second + j] = dist[v.first][v.second] + 1;
          }
        }
      }
    }
  }

  // debug() << imie(dist);

  if (dist[end.first][end.second] == INF) {
    cout << -1 << endl;
    return 0;
  }

  cout << dist[end.first][end.second] << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define PIGRECO 3.141592653589793

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
const int maxn = 1e3 + 3;
const int INF = 1e9;

char grid[maxn][maxn];

bool visited[maxn][maxn];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(grid, '#', sizeof(grid));

  // memset(dist, INF, sizeof(dist));

  int h, w;
  cin >> h >> w;

  pair<int, int> start;
  pair<int, int> end;
  cin >> start.first >> start.second;
  cin >> end.first >> end.second;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }

  queue<pair<int, int>> q;

  vector<vector<int>> dist(h + 1, vector<int>(w + 1, INF));

  q.push(start);

  dist[start.first][start.second] = 0;

  while (!q.empty()) {

    pair<int, int> v = q.front();
    q.pop();

    visited[v.first][v.second] = true;

    bool nxt = false;

    for (int i = 0; i < 4; i++) {

      if (v.first + dx[i] >= 1 && v.second + dy[i] >= 1 &&
          v.first + dx[i] <= h && v.second + dy[i] <= w &&
          grid[v.first + dx[i]][v.second + dy[i]] != '#' &&
          !visited[v.first + dx[i]][v.second + dy[i]]) {
        dist[v.first + dx[i]][v.second + dy[i]] = min(
            dist[v.first + dx[i]][v.second + dy[i]], dist[v.first][v.second]);
        q.push({v.first + dx[i], v.second + dy[i]});
        nxt = true;
      }
    }

    if (!nxt) {

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {

          if (v.first + i >= 1 && v.second + j >= 1 && v.first + i <= h &&
              v.second + j <= w && grid[v.first + i][v.second + j] != '#' &&
              !visited[v.first + i][v.second + j]) {
            dist[v.first + i][v.second + j] = min(
                dist[v.first + i][v.second + j], dist[v.first][v.second] + 1);
            q.push({v.first + i, v.second + j});
          }
        }
      }
    }
  }

  // debug() << imie(dist);

  if (dist[end.first][end.second] == INF) {
    cout << -1 << endl;
    return 0;
  }

  cout << dist[end.first][end.second] << endl;
}