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

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, m, xs, ys, xt, yt, ans[10005][10005];
int yi[10] = {0, 0, 0, 1, -1};
int xi[10] = {0, 1, -1, 0, 0};
int ym[30] = {-2, -2, -2, -2, -2, -1, 0, 1,  2,  2,
              2,  2,  2,  1,  0,  -1, 1, -1, -1, 1};
int xm[30] = {-2, -1, 0,  1,  2,  2,  2, 2, 2,  1,
              0,  -1, -2, -2, -2, -2, 1, 1, -1, -1};
char a[1005][1005];
struct node {
  int x, y, w;
  node() {}
  node(int X, int Y, int W) {
    x = X;
    y = Y;
    w = W;
  }
  bool operator<(const node o) const { return w > o.w; }
};
inline bool chcek(int xx, int yy, int ww) {
  if (a[xx][yy] == '#' || xx < 1 || xx > n || yy < 1 || yy > m ||
      ans[xx][yy] <= ww)
    return 0;
  return 1;
}
inline void bfs() {
  priority_queue<node> q;
  node now;
  q.push(node(xs, ys, 0));
  while (q.size()) {
    now = q.top();
    q.pop();
    if (ans[now.x][now.y] < now.w)
      continue;
    ans[now.x][now.y] = now.w;
    for (int i = 1; i <= 4; i++)
      if (chcek(now.x + xi[i], now.y + yi[i], now.w))
        q.push(node(now.x + xi[i], now.y + yi[i], now.w));
    for (int i = 1; i <= 20; i++)
      if (chcek(now.x + xm[i], now.y + ym[i], now.w + 1))
        q.push(node(now.x + xm[i], now.y + ym[i], now.w + 1));
  }
}
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x;
}
inline char read1() {
  char x = getchar();
  while (x != '.' && x != '#')
    x = getchar();
  return x;
}
int main() {
  n = read();
  m = read();
  xs = read();
  ys = read();
  xt = read();
  yt = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read1();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ans[i][j] = 0x3f3f3f3f;
  ans[xs][ys] = 0;
  bfs();
  if (ans[xt][yt] == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", ans[xt][yt]);
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, m, xs, ys, xt, yt, ans[10005][10005];
int yi[10] = {0, 0, 0, 1, -1};
int xi[10] = {0, 1, -1, 0, 0};
int ym[30] = {-2, -2, -2, -2, -2, -1, 0, 1,  2,  2,
              2,  2,  2,  1,  0,  -1, 1, -1, -1, 1};
int xm[30] = {-2, -1, 0,  1,  2,  2,  2, 2, 2,  1,
              0,  -1, -2, -2, -2, -2, 1, 1, -1, -1};
char a[1005][1005];
struct node {
  int x, y, w;
  node() {}
  node(int X, int Y, int W) {
    x = X;
    y = Y;
    w = W;
  }
};
inline bool chcek(int xx, int yy, int ww) {
  if (a[xx][yy] == '#' || xx < 1 || xx > n || yy < 1 || yy > m ||
      ans[xx][yy] <= ww)
    return 0;
  ans[xx][yy] = ww;
  return 1;
}
inline void bfs() {
  queue<node> q;
  node now;
  q.push(node(xs, ys, 0));
  while (q.size()) {
    now = q.front();
    q.pop();
    for (int i = 1; i <= 4; i++)
      if (chcek(now.x + xi[i], now.y + yi[i], now.w))
        q.push(node(now.x + xi[i], now.y + yi[i], now.w));
    for (int i = 1; i <= 20; i++)
      if (chcek(now.x + xm[i], now.y + ym[i], now.w + 1))
        q.push(node(now.x + xm[i], now.y + ym[i], now.w + 1));
  }
}
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x;
}
inline char read1() {
  char x = getchar();
  while (x != '.' && x != '#')
    x = getchar();
  return x;
}
int main() {
  n = read();
  m = read();
  xs = read();
  ys = read();
  xt = read();
  yt = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read1();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ans[i][j] = 0x3f3f3f3f;
  ans[xs][ys] = 0;
  bfs();
  if (ans[xt][yt] == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", ans[xt][yt]);
}