Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
// Ace
#include <bits/stdc++.h>
using namespace std;
using in = long long;
using str = string;
using pi = pair<in, in>;
using vi = vector<in>;
#define F first
#define S second
#define pb push_back
#define rep(i, k, n) for (in i = k; i < n; ++i)
#define repr(i, k, n) for (in i = k; i >= n; --i)
#define sz(x) (in) x.size()
#define all(x) begin(x), end(x)
const int MOD = 1e9 + 7;
const in N = 1e3 + 1;
in n, w;
in mx, my;
in dx, dy;
str g[N];
in vis[N][N];
in dist[N][N];

map<pi, in> dir;

in valid(in x, in y) { return (x >= 0 && x < n && y >= 0 && y < w); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  memset(vis, 0, sizeof(vis));
  rep(i, 0, N) rep(j, 0, N) dist[i][j] = INT_MAX;

  rep(i, 0, 5) {
    rep(j, 0, 5) {
      in dx = abs(i - 2);
      in dy = abs(j - 2);

      if (dx + dy <= 1) {
        if (dx + dy != 0)
          dir[{i - 2, j - 2}] = 0;
      } else {
        dir[{i - 2, j - 2}] = 1;
      }
    }
  }

  cin >> n >> w;
  cin >> mx >> my;
  cin >> dx >> dy;

  rep(i, 0, n) cin >> g[i];

  priority_queue<pair<in, pi>, vector<pair<in, pi>>, greater<pair<in, pi>>> q;
  q.push({0, {mx - 1, my - 1}});
  dist[mx - 1][my - 1] = 0;

  while (!q.empty()) {
    pair<in, pi> node = q.top();
    q.pop();
    in d = dist[node.S.F][node.S.S];
    vis[node.S.F][node.S.S] = 1;
    if (node.S.F == dx - 1 && node.S.S == dy - 1) {
      cout << d;
      return 0;
    }
    for (auto i : dir) {
      in xn = node.S.F + i.F.F;
      in yn = node.S.S + i.F.S;
      if (valid(xn, yn) && vis[xn][yn] == 0 && g[xn][yn] == '.') {
        dist[xn][yn] = min(dist[xn][yn], d + i.S);
        q.push({d + i.S, {xn, yn}});
      }
    }
  }
  cout << -1;

  return 0;
}

Fixed submission:
// Ace
#include <bits/stdc++.h>
using namespace std;
using in = long long;
using str = string;
using pi = pair<in, in>;
using vi = vector<in>;
#define F first
#define S second
#define pb push_back
#define rep(i, k, n) for (in i = k; i < n; ++i)
#define repr(i, k, n) for (in i = k; i >= n; --i)
#define sz(x) (in) x.size()
#define all(x) begin(x), end(x)
const int MOD = 1e9 + 7;
const in N = 1e3 + 1;
in n, w;
in mx, my;
in dx, dy;
str g[N];
in vis[N][N];
in dist[N][N];

map<pi, in> dir;

in valid(in x, in y) { return (x >= 0 && x < n && y >= 0 && y < w); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  memset(vis, 0, sizeof(vis));
  rep(i, 0, N) rep(j, 0, N) dist[i][j] = INT_MAX;

  rep(i, 0, 5) {
    rep(j, 0, 5) {
      in dx = abs(i - 2);
      in dy = abs(j - 2);

      if (dx + dy <= 1) {
        if (dx + dy != 0)
          dir[{i - 2, j - 2}] = 0;
      } else {
        dir[{i - 2, j - 2}] = 1;
      }
    }
  }

  cin >> n >> w;
  cin >> mx >> my;
  cin >> dx >> dy;

  rep(i, 0, n) cin >> g[i];

  priority_queue<pair<in, pi>, vector<pair<in, pi>>, greater<pair<in, pi>>> q;
  q.push({0, {mx - 1, my - 1}});
  dist[mx - 1][my - 1] = 0;

  while (!q.empty()) {
    pair<in, pi> node = q.top();
    q.pop();
    in d = dist[node.S.F][node.S.S];
    vis[node.S.F][node.S.S] = 1;
    if (node.S.F == dx - 1 && node.S.S == dy - 1) {
      cout << d;
      return 0;
    }
    for (auto i : dir) {
      in xn = node.S.F + i.F.F;
      in yn = node.S.S + i.F.S;
      if (valid(xn, yn) && vis[xn][yn] == 0 && g[xn][yn] == '.') {
        if (dist[xn][yn] > d + i.S) {
          dist[xn][yn] = d + i.S;
          q.push({dist[xn][yn], {xn, yn}});
        }
      }
    }
  }
  cout << -1;

  return 0;
}