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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

const int siz = 1e3 + 10;
const int SIZ = 1e6 + 10;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "1";

int m, n;
ii s, t;
char a[siz][siz];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int mx[20] = {1, -1, 1, -1, -2, -2, -2, -2, -2, -1,
              0, 1,  2, 2,  2,  2,  2,  1,  0,  -1};
int my[20] = {1, -1, -1, 1, -2, -1, 0,  1,  2,  2,
              2, 2,  2,  1, 0,  -1, -2, -2, -2, -2};
int d[siz][siz];
bool done[siz][siz];

bool check(ii p) {
  return (1 <= p.fi && p.fi <= m && 1 <= p.se && p.se <= n &&
          a[p.fi][p.se] == '.');
}

int Dijkstra() {
  priority_queue<iii, vector<iii>, greater<iii>> pq;
  memset(d, 0x3f, sizeof(d));
  d[s.fi][s.se] = 0;
  pq.push(iii(d[s.fi][s.se], s));

  while (!pq.empty()) {
    ii u = pq.top().se;
    pq.pop();

    if (done[u.fi][u.se]) {
      continue;
    }
    done[u.fi][u.se] = true;

    for (int i = 0; i < 4; i++) {
      ii v = ii(u.fi + dx[i], u.se + dy[i]);

      if (!check(v)) {
        continue;
      }

      if (d[v.fi][v.se] > d[u.fi][u.se]) {
        d[v.fi][v.se] = d[u.fi][u.se];
        pq.push(iii(d[v.fi][v.se], v));
      }
    }

    for (int i = 0; i < 20; i++) {
      ii v = ii(u.fi + mx[i], u.se + my[i]);

      if (!check(v)) {
        continue;
      }

      if (d[v.fi][v.se] > d[u.fi][u.se] + 1) {
        d[v.fi][v.se] = d[u.fi][u.se] + 1;
        pq.push(iii(d[v.fi][v.se], v));
      }
    }
  }

  if (d[t.fi][t.se] > m * n) {
    return -1;
  }
  return d[t.fi][t.se];
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  cin >> m >> n;
  cin >> s.fi >> s.se >> t.fi >> t.se;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }

  cout << Dijkstra();

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;

const int siz = 1e3 + 10;
const int SIZ = 1e6 + 10;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "1";

int m, n;
ii s, t;
char a[siz][siz];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int mx[20] = {1, -1, 1, -1, -2, -2, -2, -2, -2, -1,
              0, 1,  2, 2,  2,  2,  2,  1,  0,  -1};
int my[20] = {1, -1, -1, 1, -2, -1, 0,  1,  2,  2,
              2, 2,  2,  1, 0,  -1, -2, -2, -2, -2};
int d[siz][siz];

bool check(ii p) {
  return (1 <= p.fi && p.fi <= m && 1 <= p.se && p.se <= n &&
          a[p.fi][p.se] == '.');
}

int bfs() {
  queue<ii> qu;
  qu.push(s);
  memset(d, 0x3f, sizeof(d));
  d[s.fi][s.se] = 0;

  while (!qu.empty()) {
    ii u = qu.front();
    qu.pop();

    for (int i = 0; i < 4; i++) {
      ii v = ii(u.fi + dx[i], u.se + dy[i]);

      if (!check(v)) {
        continue;
      }

      if (d[v.fi][v.se] > d[u.fi][u.se]) {
        d[v.fi][v.se] = d[u.fi][u.se];
        qu.push(v);
      }
    }

    for (int i = 0; i < 20; i++) {
      ii v = ii(u.fi + mx[i], u.se + my[i]);

      if (!check(v)) {
        continue;
      }

      if (d[v.fi][v.se] > d[u.fi][u.se] + 1) {
        d[v.fi][v.se] = d[u.fi][u.se] + 1;
        qu.push(v);
      }
    }
  }

  if (d[t.fi][t.se] > m * n) {
    return -1;
  }
  return d[t.fi][t.se];
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  //    freopen ((file + ".inp").c_str(), "r", stdin);
  //    freopen ((file + ".out").c_str(), "w", stdout);

  cin >> m >> n;
  cin >> s.fi >> s.se >> t.fi >> t.se;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }

  cout << bfs();

  //    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";

  return 0;
}