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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second

// char arr[1003][1003];
string arr[1003];
// int dist[1003][1003];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int m, n, x1, y1, x2, y2;
  cin >> m >> n; // scanf("%d %d", &m, &n);
  // scanf("%d %d", &x1, &y1);	scanf("%d %d", &x2, &y2);
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  int inf = INT_MAX - 100;
  vector<vector<int>> dist(m, vector<int>(n, inf));
  vector<vector<bool>> visited(m, vector<bool>(n, false));

  for (int i = 0; i < m; i++)
    cin >> arr[i];

  /*for(int i=0; i<m; i++) {
          for(int j=0; j<n; j++) printf("%c", arr[i][j]);
          printf(" [%d]\\n", i);
  } */

  int ans = -1, f = 0;

  deque<pair<int, pair<int, int>>> Q;
  dist[x1][y1] = 0; // visited[x1][y1] = true;
  Q.push_front({0, {x1, y1}});
  pair<int, pair<int, int>> temp;

  while (!Q.empty()) {
    temp = Q.front();
    Q.pop_front();

    // printf("[%d, %d] -> dist:%d\\n", temp.s.f, temp.s.s, temp.f);
    // if(visited[temp.s.f][temp.s.s]) continue;

    if (temp.s.f == x2 && temp.s.s == y2) {
      ans = temp.f;
      break;
    }

    int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

    for (int i = 0; i < 4; i++) {
      int r = temp.s.f + dir[i][0], c = temp.s.s + dir[i][1];

      if (r >= 0 && c >= 0 && r < m && c < n && arr[r][c] != '#' &&
          !visited[r][c])
        if (dist[r][c] > dist[temp.s.f][temp.s.s]) {
          dist[r][c] = dist[temp.s.f][temp.s.s];
          // visited[r][c] = true;

          Q.push_front({dist[r][c], {r, c}});
        }
    }

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) == 1)
          continue;

        int r = temp.s.f + i, c = temp.s.s + j;
        // printf("[%d, %d], ", r, c);
        if (r >= 0 && c >= 0 && r < m && c < n && arr[r][c] != '#' &&
            !visited[r][c])
          if (dist[r][c] > 1 + dist[temp.s.f][temp.s.s]) {
            dist[r][c] = 1 + dist[temp.s.f][temp.s.s];
            // visited[r][c] = true;

            Q.push_back({dist[r][c], {r, c}});
          }
      } // printf("\\n");
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second

// char arr[1003][1003];
string arr[1003];
// int dist[1003][1003];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int m, n, x1, y1, x2, y2;
  cin >> m >> n; // scanf("%d %d", &m, &n);
  // scanf("%d %d", &x1, &y1);	scanf("%d %d", &x2, &y2);
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  int inf = INT_MAX - 100;
  vector<vector<int>> dist(m, vector<int>(n, inf));

  for (int i = 0; i < m; i++) {
    cin >> arr[i];
    // for(int j=0; j<n; j++) dist[i][j] = INT_MAX;
  }

  /*for(int i=0; i<m; i++) {
          for(int j=0; j<n; j++) printf("%c", arr[i][j]);
          printf(" [%d]\\n", i);
  } */

  int ans = 0, f = 0;
  queue<pair<int, int>> Q;
  dist[x1][y1] = 0;
  Q.push({x1, y1});
  pair<int, int> temp;

  while (!Q.empty()) {
    temp = Q.front();
    Q.pop();

    int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

    for (int i = 0; i < 4; i++) {
      int r = temp.f + dir[i][0], c = temp.s + dir[i][1];

      if (r >= 0 && c >= 0 && r < m && c < n && arr[r][c] != '#')
        if (dist[r][c] > dist[temp.f][temp.s]) {
          dist[r][c] = dist[temp.f][temp.s];
          Q.push({r, c});
        }
    }

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) == 1)
          continue;

        int r = temp.f + i, c = temp.s + j;

        if (r >= 0 && c >= 0 && r < m && c < n && arr[r][c] != '#')
          if (dist[r][c] > 1 + dist[temp.f][temp.s]) {
            dist[r][c] = 1 + dist[temp.f][temp.s];
            Q.push({r, c});
          }
      }
  }

  if (dist[x2][y2] != inf)
    printf("%d\\n", dist[x2][y2]);
  else
    printf("-1\\n");

  return 0;
}