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

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dist[1001][1001];
bool vis[1001][1001];

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy;
  cin >> sx >> sy;
  int ex, ey;
  cin >> ex >> ey;
  sx--, sy--, ex--, ey--;

  vector<string> v(n);

  for (auto &i : v)
    cin >> i;

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dist[i][j] = 2e9;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      vis[i][j] = 0;

  deque<pair<pair<int, int>, int>> dq;
  dist[sx][sy] = 0;
  dq.push_back({{sx, sy}, 0});

  while (dq.size()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int cst = dq.front().second;
    dq.pop_front();

    if (vis[x][y])
      continue;
    vis[x][y] = 1;

    for (int i = 0; i < 4; i++) {
      int tox = x + dx[i];
      int toy = y + dy[i];

      if (tox < 0 || toy < 0 || tox >= n || toy >= m || v[tox][toy] == '#') {
        continue;
      }
      if (dist[tox][toy] > dist[x][y]) {
        dist[tox][toy] = dist[x][y];
        dq.push_front({{tox, toy}, cst});
      }
    }
    for (int j = -2; j <= 2; j++)
      for (int i = -2; i <= 2; i++) {
        int tox = x + j;
        int toy = y + i;

        if (tox < 0 || toy < 0 || tox >= n || toy >= m || v[tox][toy] == '#') {
          continue;
        }

        if (dist[tox][toy] > dist[x][y] + 1) {
          dist[tox][toy] = dist[x][y] + 1;
          dq.push_back({{tox, toy}, cst + 1});
        }
      }
  }

  cout << (dist[ex][ey] == 2e9 ? -1 : dist[ex][ey]) << "\\n";
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pr(args...)                                                            \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
#define all(v) (v).begin(), (v).end()
#define int long long
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dist[1001][1001];
bool vis[1001][1001];

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy;
  cin >> sx >> sy;
  int ex, ey;
  cin >> ex >> ey;

  vector<string> v(n);

  for (auto &i : v)
    cin >> i;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      dist[i][j] = 1e9;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      vis[i][j] = 0;

  deque<pair<pair<int, int>, int>> dq;
  dist[sx - 1][sy - 1] = 0;
  dq.push_back({{sx - 1, sy - 1}, 0});

  while (dq.size()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int cst = dq.front().second;
    dq.pop_front();

    if (dist[x][y] < cst)
      continue;

    for (int i = 0; i < 4; i++) {
      int tox = x + dx[i];
      int toy = y + dy[i];

      if (tox < 0 || toy < 0 || tox >= n || toy >= m || v[tox][toy] == '#') {
        continue;
      }
      if (dist[tox][toy] > dist[x][y]) {
        dist[tox][toy] = dist[x][y];
        dq.push_back({{tox, toy}, cst});
      }
    }
    for (int j = -2; j <= 2; j++)
      for (int i = -2; i <= 2; i++) {
        int tox = x + j;
        int toy = y + i;

        if (tox < 0 || toy < 0 || tox >= n || toy >= m || v[tox][toy] == '#') {
          continue;
        }

        if (dist[tox][toy] > dist[x][y] + 1) {
          dist[tox][toy] = dist[x][y] + 1;
          dq.push_front({{tox, toy}, cst + 1});
        }
      }
  }

  cout << (dist[ex - 1][ey - 1] == 1e9 ? -1 : dist[ex - 1][ey - 1]) << "\\n";
}

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}