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

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

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

char a[1005][1005];
vector<pair<int, int>> v;
queue<pair<int, int>> Q, nq;
int vs[1005][1005];

int main(void) {
  int h, w, sy, sx, ey, ex, i, j, dir, magic;

  scanf("%d %d %d %d %d %d", &h, &w, &sy, &sx, &ey, &ex);
  for (i = 1; i <= h; i++)
    scanf("%s", a[i] + 1);

  Q.push(make_pair(sy, sx));
  vs[sy][sx] = 1;

  for (magic = 0;; magic++) {
    if (Q.empty())
      break;

    v.clear();

    while (!Q.empty()) {
      int nowy = Q.front().first;
      int nowx = Q.front().second;
      Q.pop();

      v.push_back(make_pair(nowy, nowx));

      if (nowy == ey && nowx == ex) {
        printf("%d", magic);
        return 0;
      }
      // printf("%d %d\\n", nowy, nowx);
      for (dir = 0; dir < 4; dir++) {
        int nexty = nowy + dy[dir];
        int nextx = nowx + dx[dir];
        // printf("> %d %d\\n", nexty, nextx);
        if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w &&
              a[nexty][nextx] == '.' && !vs[nexty][nextx]))
          continue;

        Q.push(make_pair(nexty, nextx));
        vs[nexty][nextx] = 1;
      }
    }

    for (pair<int, int> val : v) {
      for (i = -2; i <= 2; i++)
        for (j = -2; j <= 2; j++) {
          int nexty = val.first + i;
          int nextx = val.second + j;
          // printf("> %d %d\\n", nexty, nextx);
          if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w &&
                a[nexty][nextx] == '.' && !vs[nexty][nextx]))
            continue;

          nq.push(make_pair(nexty, nextx));
          vs[nexty][nextx] = 1;
        }
    }

    while (!nq.empty()) {
      // printf("> %d %d\\n", nq.front().first, nq.front().second);
      Q.push(nq.front());
      nq.pop();
    }
  }
  puts("-1");
  return 0;
}

Error: TLE

Fixed submission:

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

const int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};
const int INF = (int)1e9;

char a[1005][1005];
queue<pair<int, int>> Q;
int vs[1005][1005];

int main(void) {
  int h, w, sy, sx, ey, ex, i, j, dir, magic;

  scanf("%d %d %d %d %d %d", &h, &w, &sy, &sx, &ey, &ex);
  for (i = 1; i <= h; i++)
    scanf("%s", a[i] + 1);

  for (i = 1; i <= h; i++)
    for (j = 1; j <= w; j++)
      vs[i][j] = INF;

  Q.push({sy, sx});
  vs[sy][sx] = 0;

  while (!Q.empty()) {
    int nowy = Q.front().first;
    int nowx = Q.front().second;
    Q.pop();

    for (dir = 0; dir < 4; dir++) {
      int nexty = nowy + dy[dir];
      int nextx = nowx + dx[dir];

      if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w &&
            a[nexty][nextx] == '.' && vs[nexty][nextx] > vs[nowy][nowx]))
        continue;

      Q.push({nexty, nextx});
      vs[nexty][nextx] = vs[nowy][nowx];
    }

    for (i = -2; i <= 2; i++)
      for (j = -2; j <= 2; j++) {
        int nexty = nowy + i;
        int nextx = nowx + j;

        if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w &&
              a[nexty][nextx] == '.' && vs[nexty][nextx] > vs[nowy][nowx] + 1))
          continue;

        Q.push({nexty, nextx});
        vs[nexty][nextx] = vs[nowy][nowx] + 1;
      }
  }

  printf("%d", vs[ey][ex] == INF ? -1 : vs[ey][ex]);
  return 0;
}