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

#include <bits/stdc++.h>

using namespace std;

#define intt long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 1005;
const int INF = 1000006;

int n, m;

char a[N][N];
int d[N][N];

int d1x[] = {1, -1, 0, 0};
int d1y[] = {0, 0, 1, -1};

int d2x[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
             0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int d2y[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
             2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

bool valid(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }

void bfs(int vx, int vy) {
  d[vx][vy] = 0;
  deque<pair<int, int>> dq;
  dq.push_front({vx, vy});
  while (!dq.empty()) {
    int fromx = dq.front().first;
    int fromy = dq.front().second;
    dq.pop_front();
    for (int i = 0; i < 4; i++) {
      int tox = fromx + d1x[i];
      int toy = fromy + d1y[i];
      if (valid(tox, toy) && a[tox][toy] == '.' &&
          d[fromx][fromy] + 0 < d[tox][toy]) {
        d[tox][toy] = d[fromx][fromy] + 0;
        dq.push_front({tox, toy});
      }
    }
    for (int i = 0; i < 20; i++) {
      int tox = fromx + d2x[i];
      int toy = fromy + d2y[i];
      if (valid(tox, toy) && a[tox][toy] == '.' &&
          d[fromx][fromy] + 1 < d[tox][toy]) {
        d[tox][toy] = d[fromx][fromy] + 1;
        dq.push_back({tox, toy});
      }
    }
  }
}

int main() {

  FAST;

  cin >> n >> m;
  int sx, sy, fx, fy;
  cin >> sx >> sy;
  cin >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = INF;
    }
  }
  bfs(sx, sy);
  cout << ((d[fx][fy] != INF) ? d[fx][fy] : -1);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define intt long long
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 1005;
const int INF = 1000006;

int n, m;

char a[N][N];

map<pair<int, int>, int> d;

int d1x[] = {1, -1, 0, 0};
int d1y[] = {0, 0, 1, -1};

int d2x[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
             0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int d2y[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
             2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

bool valid(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }

void bfs(int vx, int vy) {
  d[{vx, vy}] = 0;
  deque<pair<int, int>> dq;
  dq.push_front({vx, vy});
  while (!dq.empty()) {
    int fromx = dq.front().first;
    int fromy = dq.front().second;
    dq.pop_front();
    for (int i = 0; i < 4; i++) {
      int tox = fromx + d1x[i];
      int toy = fromy + d1y[i];
      if (valid(tox, toy) && a[tox][toy] == '.' &&
          d[{fromx, fromy}] + 0 < d[{tox, toy}]) {
        d[{tox, toy}] = d[{fromx, fromy}] + 0;
        dq.push_front({tox, toy});
      }
    }
    for (int i = 0; i < 20; i++) {
      int tox = fromx + d2x[i];
      int toy = fromy + d2y[i];
      if (valid(tox, toy) && a[tox][toy] == '.' &&
          d[{fromx, fromy}] + 1 < d[{tox, toy}]) {
        d[{tox, toy}] = d[{fromx, fromy}] + 1;
        dq.push_back({tox, toy});
      }
    }
  }
}

int main() {

  FAST;

  cin >> n >> m;
  int sx, sy, fx, fy;
  cin >> sx >> sy;
  cin >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[{i, j}] = INF;
    }
  }
  bfs(sx, sy);
  cout << ((d[{fx, fy}] != INF) ? d[{fx, fy}] : -1);
  return 0;
}