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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

struct Square {
  int h, w, cnt;
  Square(int _h, int _w, int _cnt) {
    h = _h;
    w = _w;
    cnt = _cnt;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> mp(h);
  rep(i, h) cin >> mp[i];

  vvi cnt(h, vi(w));
  list<Square> li;

  li.push_front(Square(ch, cw, 1));
  while (!li.empty()) {
    Square cur = li.front();
    li.pop_front();

    if (cur.h < 0 || cur.h >= h || cur.w < 0 || cur.w >= w)
      continue;
    if (cnt[cur.h][cur.w] != 0 && cnt[cur.h][cur.w] <= cur.cnt)
      continue;
    if (mp[cur.h][cur.w] == '#')
      continue;

    cnt[cur.h][cur.w] = cur.cnt;
    if (cur.h == dh && cur.w == dw)
      break;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        Square nxt = Square(cur.h + i, cur.w + j, cur.cnt);
        if (abs(i) + abs(j) == 1) {
          li.push_front(nxt);
        } else {
          nxt.cnt++;
          li.push_back(nxt);
        }
      }
    }
  }

  cout << cnt[dh][dw] - 1 << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}

struct Square {
  int h, w, cnt;
  Square(int _h, int _w, int _cnt) {
    h = _h;
    w = _w;
    cnt = _cnt;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> mp(h);
  rep(i, h) cin >> mp[i];

  vvi cnt(h, vi(w));
  queue<Square> que;

  que.push(Square(ch, cw, 1));
  while (!que.empty()) {
    Square cur = que.front();
    que.pop();

    if (cur.h < 0 || cur.h >= h || cur.w < 0 || cur.w >= w)
      continue;
    if (cnt[cur.h][cur.w] != 0 && cnt[cur.h][cur.w] <= cur.cnt)
      continue;
    if (mp[cur.h][cur.w] == '#')
      continue;

    cnt[cur.h][cur.w] = cur.cnt;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          que.push(Square(cur.h + i, cur.w + j, cur.cnt));
        } else {
          que.push(Square(cur.h + i, cur.w + j, cur.cnt + 1));
        }
      }
    }
  }

  cout << cnt[dh][dw] - 1 << endl;
}