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

// By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
using namespace std;

int nxt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, cn, cm, dn, dm, ans;
char s[1005][1005];
int inque[1005][1005], dis[1005][1005];

struct Node {
  int x, y, dis;
  Node() {}
  Node(int a, int b, int c) : x(a), y(b), dis(c) {}
  ~Node() {}
  friend bool operator<(const Node &a, const Node &b) { return a.dis > b.dis; }
};
priority_queue<Node> q;

void floodFillDfs(int x, int y, int d) {
  dis[x][y] = d;
  if (!inque[x][y]) {
    q.push(Node(x, y, d));
    inque[x][y] = 1;
  }
  //	printf("FLOODFILLDFS(%d, %d, %d)\\n", x, y, d);
  for (int i = 0; i < 4; i++) {
    int tx = x + nxt[i][0];
    int ty = y + nxt[i][1];
    if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' ||
        dis[tx][ty] <= d)
      continue;
    floodFillDfs(tx, ty, d);
  }
}
void bfs() {
  while (!q.empty()) {
    int x = q.top().x, y = q.top().y;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + nxt[i][0];
      int ty = y + nxt[i][1];
      if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' ||
          dis[tx][ty] <= dis[x][y])
        continue;
      floodFillDfs(tx, ty, dis[x][y]);
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int tx = x + i;
        int ty = y + j;
        if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' ||
            dis[tx][ty] <= dis[x][y] + 1)
          continue;
        floodFillDfs(tx, ty, dis[x][y] + 1);
      }
    }
    if (dis[dn][dm] != 0x3f3f3f3f)
      return;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d", &cn, &cm);
  scanf("%d%d", &dn, &dm);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[i][j] = getchar();
      while (s[i][j] != '.' && s[i][j] != '#')
        s[i][j] = getchar();
      dis[i][j] = 0x3f3f3f3f;
    }
  }
  floodFillDfs(cn, cm, 0);
  bfs();
  if (dis[dn][dm] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\\n", dis[dn][dm]);
  return 0;
}

Error: TLE

Fixed submission:

// By: Luogu@rui_er(122461)
#include <bits/stdc++.h>
using namespace std;

int nxt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, m, cn, cm, dn, dm, ans;
char s[1005][1005];
int dis[1005][1005];

void dfs(int x, int y) {
  if (x == dn && y == dm)
    return;
  for (int i = 0; i < 4; i++) {
    int tx = x + nxt[i][0];
    int ty = y + nxt[i][1];
    if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' ||
        dis[tx][ty] <= dis[x][y])
      continue;
    dis[tx][ty] = dis[x][y];
    dfs(tx, ty);
  }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (!i && !j)
        continue;
      int tx = x + i;
      int ty = y + j;
      if (tx < 1 || tx > n || ty < 1 || ty > m || s[tx][ty] == '#' ||
          dis[tx][ty] <= dis[x][y] + 1)
        continue;
      dis[tx][ty] = dis[x][y] + 1;
      dfs(tx, ty);
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d", &cn, &cm);
  scanf("%d%d", &dn, &dm);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[i][j] = getchar();
      while (s[i][j] != '.' && s[i][j] != '#')
        s[i][j] = getchar();
      dis[i][j] = 0x3f3f3f3f;
    }
  }
  dis[cn][cm] = 0;
  dfs(cn, cm);
  if (dis[dn][dm] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\\n", dis[dn][dm]);
  return 0;
}