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

#include <bits/stdc++.h>

using namespace std;

struct Point {
  int nx, ny, dis;
  Point(int _nx, int _ny, int _dis) {
    nx = _nx;
    ny = _ny;
    dis = _dis;
  }
};

bool operator<(const Point &x, const Point &y) { return x.dis > y.dis; }

int n, m, c1, d1, c2, d2;
char G[1010][1010];
bool vis[1010][1010];
int dis[1010][1010];

void pri_dij() {
  priority_queue<Point> pq;
  while (!pq.empty())
    pq.pop();
  memset(dis, 0x3f, sizeof(dis));
  dis[c1][d1] = 0;
  pq.push(Point(c1, d1, 0));
  while (!pq.empty()) {
    int x = pq.top().nx, y = pq.top().ny;
    pq.pop();
    if (vis[x][y])
      continue;
    vis[x][y] = true;
    for (int nx = x - 2; nx <= x + 2; nx++) {
      if (nx < 1 || nx > n)
        continue;
      for (int ny = y - 2; ny <= y + 2; ny++) {
        if (ny < 1 || ny > m)
          continue;
        if (G[nx][ny] == '#')
          continue;
        if (abs(nx - x) + abs(ny - y) == 1) {
          if (dis[nx][ny] > dis[x][y]) {
            dis[nx][ny] = dis[x][y];
            pq.push(Point(nx, ny, dis[nx][ny]));
          }
        } else {
          if (dis[nx][ny] > dis[x][y] + 1) {
            dis[nx][ny] = dis[x][y] + 1;
            pq.push(Point(nx, ny, dis[nx][ny]));
          }
        }
      }
    }
  }
}

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &c1, &d1, &c2, &d2);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> G[i][j];
  pri_dij();
  if (dis[c2][d2] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d", dis[c2][d2]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Node {
  int px, py;
  Node(int _px, int _py) {
    px = _px;
    py = _py;
  }
};

int n, m, c1, d1, c2, d2, cnt;
char G[1010][1010];
int p[1010][1010];
bool flag[1010][1010];
int dis[1010][1010];
vector<Node> zhe[1010][1010];

int fx[] = {1, 0, -1, 0};
int fy[] = {0, -1, 0, 1};

inline void dfs1(int x, int y, int lastx, int lasty) {
  for (int i = 0; i < 4; i++) {
    int nx = x + fx[i], ny = y + fy[i];
    if (nx == lastx && ny == lasty)
      continue;
    if (nx > n || nx < 1 || ny > m || ny < 1)
      continue;
    if (G[nx][ny] == '#')
      continue;
    if (dis[nx][ny] <= dis[x][y])
      continue;
    dis[nx][ny] = dis[x][y];
    dfs1(nx, ny, x, y);
  }
  for (int i = 0; i < zhe[x][y].size(); i++) {
    int nx = zhe[x][y][i].px, ny = zhe[x][y][i].py;
    if (dis[nx][ny] <= dis[x][y] + 1)
      continue;
    dis[nx][ny] = dis[x][y] + 1;
    dfs1(nx, ny, x, y);
  }
}

inline void dfs2(int x, int y) {
  p[x][y] = cnt;
  for (int i = 0; i < 4; i++) {
    int nx = x + fx[i], ny = y + fy[i];
    if (p[nx][ny])
      continue;
    if (nx > n || nx < 1 || ny > m || ny < 1)
      continue;
    if (G[nx][ny] == '#')
      continue;
    dfs2(nx, ny);
  }
}

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &c1, &d1, &c2, &d2);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> G[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (G[i][j] == '#')
        continue;
      ++cnt;
      if (!p[i][j])
        dfs2(i, j);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int nx = i - 2; nx <= i + 2; nx++) {
        if (nx > n || nx < 1)
          continue;
        for (int ny = j - 2; ny <= j + 2; ny++) {
          if (ny > m || ny < 1)
            continue;
          if (G[nx][ny] == '#')
            continue;
          if (p[nx][ny] != p[i][j]) {
            zhe[i][j].push_back(Node(nx, ny));
          }
        }
      }
    }
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[c1][d1] = 0;
  dfs1(c1, d1, -1, -1);
  if (dis[c2][d2] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d", dis[c2][d2]);
  return 0;
}