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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> s;
  int ch, cw;
  cin >> ch >> cw;
  --ch, --cw;
  s = make_pair(ch, cw);
  pair<int, int> g;
  int dh, dw;
  cin >> dh >> dw;
  --dh, --dw;
  g = make_pair(dh, dw);
  vector<vector<char>> hw(h, vector<char>(w));
  rep(i, h) rep(j, w) { cin >> hw[i][j]; }
  // queue<pair<int, int>> que;
  deque<pair<int, int>> que;
  // que.push(s);
  que.push_back(s);
  vector<vector<int>> cnt(h, vector<int>(w, 1e8));
  cnt[s.first][s.second] = 0;
  int dy[4] = {0, 0, 1, -1};
  int dx[4] = {1, -1, 0, 0};
  while (!que.empty()) {
    // pair<int, int> now = que.front(); que.pop();
    pair<int, int> now = que.front();
    que.pop_front();
    // cout << now.first << " " << now.second << endl;
    rep(ri, 4) {
      int y = now.first + dy[ri];
      int x = now.second + dx[ri];
      if (y < 0 || y >= h || x < 0 || x >= w)
        continue;
      if (hw[y][x] == '#')
        continue;
      if (cnt[y][x] > cnt[now.first][now.second]) {
        cnt[y][x] = cnt[now.first][now.second];
        que.push_front(make_pair(y, x));
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int y = now.first + i;
        int x = now.second + j;
        if (y < 0 || y >= h || x < 0 || x >= w)
          continue;
        if (hw[y][x] == '#')
          continue;
        // cout << "pretest" << endl;
        // if ((i == 1 && j == 0) || (i == -1 && j == 0) || (i == 0 && j == 1)
        // || (i == 0 && j == -1)) {
        //     //cout << "pretest1" << endl;
        //     if (cnt[y][x] > cnt[now.first][now.second]) {
        //         //cout << "test1" << endl;
        //         cnt[y][x] = cnt[now.first][now.second];
        //         if (y == g.first && x == g.second) {
        //             break;
        //         }
        //         que.push(make_pair(y, x));
        //     }
        // }
        // else {
        // cout << "pretest2" << endl;
        if (cnt[y][x] <= cnt[now.first][now.second] + 1)
          continue;
        // cout << "test2" << endl;
        cnt[y][x] = cnt[now.first][now.second] + 1;
        // que.push(make_pair(y, x));
        que.push_back(make_pair(y, x));
        //}
      }
    }
  }
  // rep(i, h) {
  //     rep(j, w) cout << cnt[i][j] << " ";
  //     cout << endl;
  // }
  if (cnt[g.first][g.second] == 1e8)
    cout << -1 << endl;
  else
    cout << cnt[g.first][g.second] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> s;
  int ch, cw;
  cin >> ch >> cw;
  --ch, --cw;
  s = make_pair(ch, cw);
  pair<int, int> g;
  int dh, dw;
  cin >> dh >> dw;
  --dh, --dw;
  g = make_pair(dh, dw);
  vector<vector<char>> hw(h, vector<char>(w));
  rep(i, h) rep(j, w) { cin >> hw[i][j]; }
  queue<pair<int, int>> que;
  que.push(s);
  vector<vector<int>> cnt(h, vector<int>(w, 1e8));
  cnt[s.first][s.second] = 0;
  while (!que.empty()) {
    pair<int, int> now = que.front();
    que.pop();
    // cout << now.first << " " << now.second << endl;
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int y = now.first + i;
        int x = now.second + j;
        if (y < 0 || y >= h || x < 0 || x >= w)
          continue;
        if (hw[y][x] == '#')
          continue;
        // cout << "pretest" << endl;
        if ((i == 1 && j == 0) || (i == -1 && j == 0) || (i == 0 && j == 1) ||
            (i == 0 && j == -1)) {
          // cout << "pretest1" << endl;
          if (cnt[y][x] > cnt[now.first][now.second]) {
            // cout << "test1" << endl;
            cnt[y][x] = cnt[now.first][now.second];
            if (y == g.first && x == g.second) {
              break;
            }
            que.push(make_pair(y, x));
          }
        } else {
          // cout << "pretest2" << endl;
          if (cnt[y][x] <= cnt[now.first][now.second] + 1)
            continue;
          // cout << "test2" << endl;
          cnt[y][x] = cnt[now.first][now.second] + 1;
          que.push(make_pair(y, x));
        }
      }
    }
  }
  // rep(i, h) {
  //     rep(j, w) cout << cnt[i][j] << " ";
  //     cout << endl;
  // }
  if (cnt[g.first][g.second] == 1e8)
    cout << -1 << endl;
  else
    cout << cnt[g.first][g.second] << endl;
  return 0;
}