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

#include <bits/stdc++.h>
using namespace std;
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;
}

int main() {
  int h, w, start_y, start_x, goal_y, goal_x;
  cin >> h >> w >> start_y >> start_x >> goal_y >> goal_x;
  start_y--;
  start_x--;
  goal_y--;
  goal_x--;
  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s.at(i).at(j);
    }
  }

  deque<vector<int64_t>> que;
  que.emplace_back(vector<int64_t>{start_y, start_x, 0});
  vector<vector<int64_t>> x(h, vector<int64_t>(w, INT64_MAX));
  while (!que.empty()) {
    vector<int64_t> cur = que.front();
    que.pop_front();
    int64_t cur_y = cur.at(0);
    int64_t cur_x = cur.at(1);
    int64_t cnt = cur.at(2);
    chmin(x.at(cur_y).at(cur_x), cnt);
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int64_t next_y = cur_y + i;
        int64_t next_x = cur_x + j;
        if (next_y < 0 || h <= next_y || next_x < 0 || w <= next_x) {
          continue;
        }
        if (s.at(next_y).at(next_x) == '#') {
          continue;
        }
        int64_t next_cnt = cnt;
        bool magic = false;
        // if (cur_y == 0 && cur_x == 0 && i == 0 && j == 1) {
        //     cout << "!!!" << abs(i) + abs(j) << endl;
        // }
        if (abs(i) + abs(j) >= 2) {
          magic = true;
        }
        if (magic) {
          next_cnt++;
        }
        if (x.at(next_y).at(next_x) <= next_cnt) {
          continue;
        }
        vector<int64_t> next = {next_y, next_x, next_cnt};
        // if (cur_y == 0 && cur_x == 0) {
        //     cout << next_y << " " << next_x << " " << next_cnt << endl;
        // }
        if (magic) {
          que.emplace_back(next);
        } else {
          que.emplace_front(next);
        }
      }
    }
  }
  int64_t ans = x.at(goal_y).at(goal_x);
  if (ans == INT64_MAX) {
    ans = -1;
  }
  cout << ans << endl;

  // for (int i = 0; i < h; i++) {
  //     for (int j = 0; j < w; j++) {
  //         cout << x.at(i).at(j) << " ";
  //     }
  //     cout << endl;
  // }
  // return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w;
  int ch, cw;
  int dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s.at(i).at(j);
    }
  }

  vector<vector<int>> x(h, vector<int>(w, INT_MAX));

  queue<vector<int>> que;
  que.emplace(vector<int>{ch, cw, 0});

  while (!que.empty()) {
    int cur_y, cur_x, cnt;
    vector<int> cur = que.front();
    que.pop();
    cur_y = cur.at(0);
    cur_x = cur.at(1);
    cnt = cur.at(2);

    if (cur_y < 0 || h <= cur_y || cur_x < 0 || w <= cur_x) {
      continue;
    }

    if (s.at(cur_y).at(cur_x) == '#') {
      continue;
    }

    if (x.at(cur_y).at(cur_x) <= cnt) {
      continue;
    }

    x.at(cur_y).at(cur_x) = cnt;

    vector<pair<int, int>> y = {make_pair(0, 1), make_pair(0, -1),
                                make_pair(1, 0), make_pair(-1, 0)};
    for (auto iter = y.begin(); iter < y.end(); iter++) {
      int next_y = cur_y + (*iter).first;
      int next_x = cur_x + (*iter).second;
      que.emplace(vector<int>{next_y, next_x, cnt});
    }

    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (abs(i) <= 1 && abs(j) <= 1 && !(abs(i) == 1 && abs(j) == 1)) {
          continue;
        }
        int next_y = cur_y + i;
        int next_x = cur_x + j;
        que.emplace(vector<int>{next_y, next_x, cnt + 1});
      }
    }
  }

  if (x.at(dh).at(dw) == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << x.at(dh).at(dw) << endl;
  }

  return 0;
}