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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
using namespace std;
inline int read() {
  int num = 0, f = 1;
  char x = getchar();
  while (x < '0' || x > '9') {
    if (x == '-')
      f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') {
    num = num * 10 + x - '0';
    x = getchar();
  }
  return num * f;
}
const int maxn = 1005;
int h, w;
int bx, by, ex, ey;
char s[maxn][maxn];
int mx[25] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
              0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
int my[25] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
              1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
typedef pair<int, int> pr;
bool vst[maxn * maxn];
int dis[maxn * maxn];
int prt[maxn * maxn];
bool met[maxn][maxn];
int bh(int x, int y) { return (x - 1) * w + y; }
int getf(int x) {
  if (x == prt[x])
    return x;
  else
    return prt[x] = getf(prt[x]);
}
void dfs(int x, int y) {
  met[x][y] = true;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx <= 0 || nx > h || ny <= 0 || ny > w || s[nx][ny] == '#')
      continue;
    if (met[nx][ny])
      continue;
    int f1 = getf(bh(x, y)), f2 = getf(bh(nx, ny));
    prt[f1] = f2;
    dfs(nx, ny);
  }
}
vector<int> e[maxn * maxn];
map<int, bool> m[maxn * maxn];
#define INF 1e9
queue<int> Q;
void bfs() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      dis[bh(i, j)] = INF;
  int b = getf(bh(bx, by));
  dis[b] = 0;
  Q.push(b);
  while (Q.size()) {
    int x = Q.front();
    Q.pop();
    for (auto y : e[x]) {
      if (dis[y] > dis[x] + 1) {
        dis[y] = dis[x] + 1;
        Q.push(y);
      }
    }
  }
  int ed = getf(bh(ex, ey));
  if (dis[ed] == INF)
    puts("-1");
  else
    printf("%d\\n", dis[ed]);
}
int main() {
  h = read();
  w = read();
  bx = read();
  by = read();
  ex = read();
  ey = read();
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      prt[bh(i, j)] = bh(i, j);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (!met[i][j])
        dfs(i, j);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (s[i][j] != '#') {
        for (int k = 0; k < 25; ++k) {
          int nx = i + mx[k], ny = j + my[k];
          if (nx <= 0 || nx > h || ny <= 0 || ny > w || s[nx][ny] == '#')
            continue;
          int f1 = getf(bh(i, j)), f2 = getf(bh(nx, ny));
          if (f1 > f2)
            swap(f1, f2);
          if (f1 == f2 || m[f1][f2] == true)
            continue;
          m[f1][f2] = true;
          e[f1].push_back(f2);
          e[f2].push_back(f1);
        }
      }
  bfs();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
inline int read() {
  int num = 0, f = 1;
  char x = getchar();
  while (x < '0' || x > '9') {
    if (x == '-')
      f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') {
    num = num * 10 + x - '0';
    x = getchar();
  }
  return num * f;
}
const int maxn = 1005;
int h, w;
int bx, by, ex, ey;
char s[maxn][maxn];
int mx[25] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
              0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
int my[25] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
              1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
typedef pair<int, int> pr;
int dis[maxn][maxn];
queue<pr> Q;
bool vst[maxn][maxn];
#define INF 1e9
void spfa() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      dis[i][j] = INF;
  // cout<<dis[ex][ey];
  Q.push(pr(bx, by));
  dis[bx][by] = 0;
  vst[bx][by] = true;
  while (Q.size()) {
    pr t = Q.front();
    Q.pop();
    vst[t.first][t.second] = false;
    for (int i = 0; i < 4; ++i) {
      int nx = t.first + dx[i], ny = t.second + dy[i];
      if (nx <= 0 || nx > h || ny <= 0 || ny > w || s[nx][ny] == '#')
        continue;
      if (dis[nx][ny] > dis[t.first][t.second]) {
        dis[nx][ny] = dis[t.first][t.second];
        if (!vst[nx][ny]) {
          Q.push(pr(nx, ny));
          vst[nx][ny] = true;
        }
      }
    }
    for (int i = 0; i < 25; ++i) {
      int nx = t.first + mx[i], ny = t.second + my[i];
      if (nx <= 0 || nx > h || ny <= 0 || ny > w || s[nx][ny] == '#')
        continue;
      if (dis[nx][ny] > dis[t.first][t.second] + 1) {
        dis[nx][ny] = dis[t.first][t.second] + 1;
        if (!vst[nx][ny]) {
          Q.push(pr(nx, ny));
          vst[nx][ny] = true;
        }
      }
    }
  }
  if (dis[ex][ey] == INF)
    puts("-1");
  else
    printf("%d\\n", dis[ex][ey]);
}
int main() {
  h = read();
  w = read();
  bx = read();
  by = read();
  ex = read();
  ey = read();
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  spfa();
  return 0;
}