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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, pair<int, int>> pipii;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> m(h);
  for (int i = 0; i < h; i++)
    cin >> m[i];
  priority_queue<pipii, vector<pipii>, greater<>> p;
  bool used[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      used[i][j] = false;
    }
  }
  p.push({0, {ch, cw}});
  vector<pair<int, int>> dis = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  while (!p.empty()) {
    pipii tar = p.top();
    p.pop();
    int cost = tar.first;
    int x = tar.second.first;
    int y = tar.second.second;
    if (used[x][y])
      continue;
    used[x][y] = true;
    if (x == dh && y == dw) {
      cout << cost << endl;
      return 0;
    }
    for (auto d : dis) {
      int xx = x + d.first;
      int yy = y + d.second;
      if (0 <= xx && xx < h && 0 <= yy && yy < w && m[xx][yy] == '.') {
        p.push({cost, {xx, yy}});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xx = x + i;
        int yy = y + j;
        if (0 <= xx && xx < h && 0 <= yy && yy < w && m[xx][yy] == '.') {
          p.push({cost + 1, {xx, yy}});
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define <- プログラムの文字列を置換する
// typedef <- 新しい型に名前を付けるためのキーワード

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const long long MOD = 1e9 + 7;
const long long INF = 1e15;
const int INF_INT = 1e9;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class TYPE> struct edge {
  TYPE to, cost;
  edge(TYPE t, TYPE c) {
    to = t;
    cost = c;
  }
};

const int MAXH = 1000;
const int MAXW = 1000;
ll d[MAXH][MAXW];
bool used[MAXH][MAXW];
vector<string> m;
int H, W;

void dfs(int x, int y) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
      used[i][j] = false;
    }
  }
  d[x][y] = 0;
  while (true) {
    int xx = -1;
    int yy = -1;
    bool temp = false;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (!used[i][j] && (temp == false || d[i][j] < d[xx][yy])) {
          xx = i;
          yy = j;
          temp = true;
        }
      }
    }
    if (!temp)
      break;
    used[xx][yy] = true;
    vector<pair<int, int>> vec = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    for (auto p : vec) {
      int xxx = xx + p.first;
      int yyy = yy + p.second;
      if (0 <= xxx && xxx < H && 0 <= yyy && yyy < W && m[xxx][yyy] == '.') {
        d[xxx][yyy] = min(d[xx][yy], d[xxx][yyy]);
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int xxx = xx + i;
        int yyy = yy + j;
        if (0 <= xxx && xxx < H && 0 <= yyy && yyy < W && m[xxx][yyy] == '.') {
          d[xxx][yyy] = min(d[xx][yy] + 1, d[xxx][yyy]);
        }
      }
    }
  }
}

int main() {
  int Ch, Cw, Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  m.resize(H);
  for (int i = 0; i < H; ++i) {
    cin >> m[i];
  }
  dfs(Ch, Cw);
  if (d[Dh][Dw] >= INF)
    cout << -1 << endl;
  else
    cout << d[Dh][Dw] << endl;
}