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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pai 3.1415926535897932384

using namespace std;
using ll = long long;
using P = pair<int, int>;

int dx[24] = {1,  0, -1, 0, -2, -2, -2, -2, -2, -1, -1, -1,
              -1, 0, 0,  1, 1,  1,  1,  2,  2,  2,  2,  2};
int dy[24] = {0, 1,  0, -1, -2, -1, 0, 1,  2,  -2, -1, 1,
              2, -2, 2, -2, -1, 1,  2, -2, -1, 0,  1,  2};

int main(int argc, const char *argv[]) {

  int H, W;
  int CH, CW;
  int DH, DW;
  char S[1000][1000];
  int COST[1000][1000];

  cin >> H >> W;
  cin >> CH >> CW;
  cin >> DH >> DW;

  CH--;
  CW--;
  DH--;
  DW--;

  for (int h = 0; h < H; h++) {
    string s;
    cin >> s;

    for (int w = 0; w < W; w++) {
      S[h][w] = s[w];
      COST[h][w] = -1;
    }
  }

  deque<P> que;
  que.push_front(P(CH, CW));
  COST[CH][CW] = 0;

  while (que.size()) {

    P p = que.front();
    que.pop_front();

    for (int i = 0; i < 4; i++) {

      int ny = p.first + dy[i];
      int nx = p.second + dx[i];

      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (COST[ny][nx] <= COST[p.first][p.second] && COST[ny][nx] > -1)
        continue;
      if (S[ny][nx] == '.') {
        COST[ny][nx] = COST[p.first][p.second];
        que.push_front(P(ny, nx));
      }
    }

    for (int i = 4; i < 24; i++) {

      int ny = p.first + dy[i];
      int nx = p.second + dx[i];

      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (COST[ny][nx] > -1)
        continue;

      if (S[ny][nx] == '.') {
        COST[ny][nx] = COST[p.first][p.second] + 1;
        que.push_back(P(ny, nx));
      }
    }
  }

  cout << COST[DH][DW] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define Minf -1e9
#define scan(n) scanf("%lld", &n)
typedef long long ll;
const int maxn = 1 * 1e3 + 10;
const int INF = 1e9 + 100;
const int mod = 998244353;

int a[maxn];
char map[maxn][maxn];
int ans[maxn][maxn];
int dir_x[24] = {1,  0, -1, 0, -2, -2, -2, -2, -2, -1, -1, -1,
                 -1, 0, 0,  1, 1,  1,  1,  2,  2,  2,  2,  2};
int dir_y[24] = {0, 1,  0, -1, -2, -1, 0, 1,  2,  -2, -1, 1,
                 2, -2, 2, -2, -1, 1,  2, -2, -1, 0,  1,  2};
int n, m;
int check(int x, int y) {
  if (x >= 1 && y >= 1 && x <= n && y <= m && map[x][y] == '.')
    return 1;
  return 0;
}

int main() {

  int a1, a2, b1, b2;
  scanf("%d%d%d%d%d%d", &n, &m, &a1, &a2, &b1, &b2);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ans[i][j] = INF;
    }
  }
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      char te;
      scanf("%c", &map[i][j]);
    }
  }
  ans[a1][a2] = 0;
  queue<pair<int, int>> qu;
  pair<int, int> te;
  qu.push({a1, a2});
  while (!qu.empty()) {
    te = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int tx = te.first + dir_x[i];
      int ty = te.second + dir_y[i];
      if (check(tx, ty) && ans[te.first][te.second] < ans[tx][ty]) {
        ans[tx][ty] = ans[te.first][te.second];
        qu.push({tx, ty});
      }
    }
    for (int i = 4; i < 24; i++) {
      int tx = te.first + dir_x[i];
      int ty = te.second + dir_y[i];
      if (check(tx, ty) && ans[te.first][te.second] + 1 < ans[tx][ty]) {
        ans[tx][ty] = ans[te.first][te.second] + 1;
        qu.push({tx, ty});
      }
    }
  }

  if (ans[b1][b2] == INF)
    cout << "-1\\n";
  else
    cout << ans[b1][b2] << endl;
}