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

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

int main() {
  int h, w, si, sj, ti, tj;
  cin >> h >> w >> si >> sj >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s.at(i);
  }
  const int infty = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, infty));
  deque<pair<int, int>> q;
  const vector<int> dx = {0, 1, 0, -1};
  const vector<int> dy = {1, 0, -1, 0};
  dist.at(si).at(sj) = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist.at(i).at(j);
    for (int v = 0; v < 4; v++) {
      int ni = i + dx.at(v);
      int nj = j + dy.at(v);
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
        continue;
      }
      if (s.at(ni)[nj] == '#') {
        continue;
      }
      if (dist.at(ni).at(nj) <= d) {
        continue;
      }
      dist.at(ni).at(nj) = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
          continue;
        }
        if (s[ni][nj] == '#') {
          continue;
        }
        if (dist.at(ni).at(nj) <= d + 1) {
          continue;
        }
        dist.at(ni).at(nj) = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist.at(ti).at(tj);
  if (ans == infty) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch++;
  cw++;
  dh++;
  dw++;
  vector<vector<int>> s(h + 4);
  for (int i = 0; i < h + 4; i++) {
    s.at(i).resize(w + 4);
  }
  for (int i = 0; i < h + 4; i++) {
    for (int j = 0; j < w + 4; j++) {
      s.at(i).at(j) = -1;
    }
  }
  for (int i = 2; i < h + 2; i++) {
    for (int j = 2; j < w + 2; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        // 通れる場所を大きい値に設定
        s.at(i).at(j) = (1 << 21);
      }
    }
  }
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  s.at(ch).at(cw) = 0;
  pair<int, int> u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    bool flg = false;
    for (int i = 0; i < 4; i++) {
      pair<int, int> v;
      v.first = u.first + dx.at(i);
      v.second = u.second + dy.at(i);
      if (s.at(v.first).at(v.second) > s.at(u.first).at(u.second)) {
        q.push(v);
        s.at(v.first).at(v.second) = s.at(u.first).at(u.second);
        flg = true;
      }
    }
    if (!flg) {
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nowx = u.first + i;
          int nowy = u.second + j;
          if (s.at(nowx).at(nowy) > s.at(u.first).at(u.second)) {
            s.at(nowx).at(nowy) = s.at(u.first).at(u.second) + 1;
            pair<int, int> next;
            next.first = nowx;
            next.second = nowy;
            q.push(next);
          }
        }
      }
    }
  }

  if (s.at(dh).at(dw) != (1 << 21)) {
    cout << s.at(dh).at(dw) << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}