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

#include <bits/stdc++.h>
using namespace std;

const int mx = 2e3, INF = 1e9;

int h, w, S[mx][mx];

using pii = pair<int, int>;

pii C, D;

struct shortest_path {
  int djikstra() {
    int d[mx][mx];
    for (int i = 0; i <= h + 5; ++i) {
      for (int j = 0; j <= w + 5; ++j) {
        d[i][j] = INF;
      }
    }
    d[C.first][C.second] = 0;
    priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                   greater<pair<int, pii>>>
        q;
    q.push({0, C});
    while (!q.empty()) {
      pii v = q.top().second;
      int d_v = q.top().first;
      q.pop();
      if (d_v != d[v.first][v.second])
        continue;
      for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
          if (abs(i) == abs(j))
            continue;
          pii to = {v.first + i, v.second + j};
          if (to.first > h || to.second > w || to.first < 1 || to.second < 1 ||
              !S[to.first][to.second])
            continue;
          if (d[v.first][v.second] < d[to.first][to.second]) {
            d[to.first][to.second] = d[v.first][v.second];
            q.push({d[to.first][to.second], to});
          }
        }
      }
      for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
          pii to = {v.first + i, v.second + j};
          if (to.first > h || to.second > w || to.first < 1 || to.second < 1 ||
              !S[to.first][to.second])
            continue;
          if ((d[v.first][v.second] + 1) < d[to.first][to.second]) {
            d[to.first][to.second] = d[v.first][v.second] + 1;
            q.push({d[to.first][to.second], to});
          }
        }
      }
    }
    if (d[D.first][D.second] == INF)
      return -1;
    else
      return d[D.first][D.second];
  }
};

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d", &C.first, &C.second);
  scanf("%d%d", &D.first, &D.second);
  for (int i = 1; i <= h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j) {
      if (s[j] == '.')
        S[i][j + 1] = 1;
    }
  }
  shortest_path pq;
  printf("%d\\n", pq.djikstra());
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int mx = 2e3, INF = 1e9;

int h, w, S[mx][mx];

using pii = pair<int, int>;

pii C, D;

struct shortest_path {
  int djikstra() {
    map<pii, int> d;
    for (int i = 0; i <= h + 5; ++i) {
      for (int j = 0; j <= w + 5; ++j) {
        d[{i, j}] = INF;
      }
    }
    d[C] = 0;
    priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                   greater<pair<int, pii>>>
        q;
    q.push({0, C});
    while (!q.empty()) {
      pii v = q.top().second;
      int d_v = q.top().first;
      q.pop();
      if (d_v != d[v])
        continue;
      for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
          if (abs(i) == abs(j))
            continue;
          pii to = {v.first + i, v.second + j};
          if (to.first > h || to.second > w || to.first < 1 || to.second < 1 ||
              !S[to.first][to.second])
            continue;
          if (d[v] < d[to]) {
            d[to] = d[v];
            q.push({d[to], to});
          }
        }
      }
      for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
          pii to = {v.first + i, v.second + j};
          if (to.first > h || to.second > w || to.first < 1 || to.second < 1 ||
              !S[to.first][to.second])
            continue;
          if ((d[v] + 1) < d[to]) {
            d[to] = d[v] + 1;
            q.push({d[to], to});
          }
        }
      }
    }
    if (d[D] == INF)
      return -1;
    else
      return d[D];
  }
};

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d", &C.first, &C.second);
  scanf("%d%d", &D.first, &D.second);
  for (int i = 1; i <= h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j) {
      if (s[j] == '.')
        S[i][j + 1] = 1;
    }
  }
  shortest_path pq;
  printf("%d\\n", pq.djikstra());
  return 0;
}