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

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

#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define print(a, n)                                                            \\
  for (auto _ = 0; _ < n; _++)                                                 \\
    cout << a[_] << " ";                                                       \\
  cout << "\\n"
#define prints(a)                                                              \\
  for (auto _ : a)                                                             \\
    cout << _ << " ";                                                          \\
  cout << "\\n"
#define read(a, n)                                                             \\
  for (auto _ = 0; _ < n; _++)                                                 \\
  cin >> a[_]

const int dr4[] = {1, -1, 0, 0};
const int dc4[] = {0, 0, 1, -1};

int main() {
  fastio;
  int n, m, sr, sc, er, ec;
  cin >> n >> m;
  cin >> sr >> sc >> er >> ec;

  char maze[n][m];
  int ans[n][m];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      ans[i][j] = INT_MAX;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cin >> maze[i][j];
  }

  deque<array<int, 2>> q;
  q.push_back({sr - 1, sc - 1});
  ans[sr - 1][sc - 1] = 0;

  while (!q.empty()) {
    auto u = q.front();
    q.pop_front();

    for (int i = 0; i < 4; i++) {
      int r = u[0] + dr4[i];
      int c = u[1] + dc4[i];
      if (r >= 0 && c >= 0 && r < n && c < m && maze[r][c] != '#' &&
          ans[r][c] > ans[u[0]][u[1]])
        q.push_front({r, c}), ans[r][c] = ans[u[0]][u[1]];
    }

    for (int i = max(0, u[0] - 2); i <= min(n - 1, u[0] + 2); i++) {
      for (int j = max(0, u[1] - 2); j <= min(m - 1, u[1] + 2); j++) {
        if (maze[i][j] != '#' && ans[i][j] > ans[u[0]][u[1]] + 1)
          q.push_back({i, j}), ans[i][j] = ans[u[0]][u[1]] + 1;
      }
    }
  }

  if (ans[er - 1][ec - 1] == INT_MAX)
    cout << -1 << "\\n";
  else
    cout << ans[er - 1][ec - 1] << "\\n";

  exit(0);
}


Error: TLE

Fixed submission:

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

#define LL long long
#define MOD 1000000007
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define print(a, n)                                                            \\
  for (auto _ = 0; _ < n; _++)                                                 \\
    cout << a[_] << " ";                                                       \\
  cout << "\\n"
#define prints(a)                                                              \\
  for (auto _ : a)                                                             \\
    cout << _ << " ";                                                          \\
  cout << "\\n"
#define read(a, n)                                                             \\
  for (auto _ = 0; _ < n; _++)                                                 \\
  cin >> a[_]

const int dr4[] = {1, -1, 0, 0};
const int dc4[] = {0, 0, 1, -1};

int main() {
  fastio;
  int n, m, sr, sc, er, ec;
  cin >> n >> m;
  cin >> sr >> sc >> er >> ec;

  char maze[n][m];
  int ans[n][m];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      ans[i][j] = INT_MAX;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cin >> maze[i][j];
  }

  queue<array<int, 2>> q;
  q.push({sr - 1, sc - 1});
  ans[sr - 1][sc - 1] = 0;

  while (!q.empty()) {
    auto u = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int r = u[0] + dr4[i];
      int c = u[1] + dc4[i];
      if (r >= 0 && c >= 0 && r < n && c < m && maze[r][c] != '#' &&
          ans[r][c] > ans[u[0]][u[1]])
        q.push({r, c}), ans[r][c] = ans[u[0]][u[1]];
    }

    for (int i = max(0, u[0] - 2); i <= min(n - 1, u[0] + 2); i++) {
      for (int j = max(0, u[1] - 2); j <= min(m - 1, u[1] + 2); j++) {
        if (maze[i][j] != '#' && ans[i][j] > ans[u[0]][u[1]] + 1)
          q.push({i, j}), ans[i][j] = ans[u[0]][u[1]] + 1;
      }
    }
  }

  if (ans[er - 1][ec - 1] == INT_MAX)
    cout << -1 << "\\n";
  else
    cout << ans[er - 1][ec - 1] << "\\n";

  exit(0);
}