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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> node;
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}
const int N = 1e3 + 5;
const int inf = 0x3f3f3f3f;
const int dx[] = {-2, -1, 0, 1, 2};
const int dy[] = {-2, -1, 0, 1, 2};
const int ddx[] = {-1, 0, 1, 0};
const int ddy[] = {0, 1, 0, -1};
int n, m, sx, sy, ex, ey, F[N][N];
char str[N][N];
deque<node> q;
inline bool vaild(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return 0;
  return str[x][y] == '.';
}
inline void spfa() {
  memset(F, 0x3f, sizeof(F));
  if (str[sx][sy] == '#' || str[ex][ey] == '#')
    return;
  F[sx][sy] = 0;
  q.push_back(make_pair(sx, sy));

  int x, y;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int tx = x + ddx[i], ty = y + ddy[i];
      if (!vaild(tx, ty))
        continue;
      if (F[x][y] < F[tx][ty]) {
        F[tx][ty] = F[x][y];
        q.push_front(make_pair(tx, ty));
      }
    }
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        int tx = x + dx[i], ty = y + dy[j];
        if (!vaild(tx, ty))
          continue;
        if (F[x][y] + 1 < F[tx][ty]) {
          F[tx][ty] = F[x][y] + 1;
          q.push_back(make_pair(tx, ty));
        }
      }
  }
}
int main() {
  read(n);
  read(m);
  read(sx);
  read(sy);
  read(ex);
  read(ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", str[i] + 1);
  spfa();
  if (F[ex][ey] == inf)
    puts("-1");
  else
    printf("%d\\n", F[ex][ey]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> node;
template <typename T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}
const int N = 1e3 + 5;
const int inf = 0x3f3f3f3f;
const int dx[] = {-2, -1, 0, 1, 2};
const int dy[] = {-2, -1, 0, 1, 2};
const int ddx[] = {-1, 0, 1, 0};
const int ddy[] = {0, 1, 0, -1};
int n, m, sx, sy, ex, ey, F[N][N];
char str[N][N];
queue<node> q;
bool inq[N][N];
inline bool vaild(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return 0;
  return str[x][y] == '.';
}
inline void spfa() {
  memset(F, 0x3f, sizeof(F));
  if (str[sx][sy] == '#' || str[ex][ey] == '#')
    return;
  F[sx][sy] = 0;
  q.push(make_pair(sx, sy));
  inq[sx][sy] = 1;

  int x, y;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    inq[x][y] = 0;
    for (int i = 0; i < 4; i++) {
      int tx = x + ddx[i], ty = y + ddy[i];
      if (!vaild(tx, ty))
        continue;
      if (F[x][y] < F[tx][ty]) {
        F[tx][ty] = F[x][y];
        if (!inq[tx][ty]) {
          inq[tx][ty] = 1;
          q.push(make_pair(tx, ty));
        }
      }
    }
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        int tx = x + dx[i], ty = y + dy[j];
        if (!vaild(tx, ty))
          continue;
        if (F[x][y] + 1 < F[tx][ty]) {
          F[tx][ty] = F[x][y] + 1;
          if (!inq[tx][ty]) {
            inq[tx][ty] = 1;
            q.push(make_pair(tx, ty));
          }
        }
      }
  }
}
int main() {
  read(n);
  read(m);
  read(sx);
  read(sy);
  read(ex);
  read(ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", str[i] + 1);
  spfa();
  if (F[ex][ey] == inf)
    puts("-1");
  else
    printf("%d\\n", F[ex][ey]);
  return 0;
}