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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl "\\n"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define fl(a, x) memset(a, x, sizeof(a));
#define pt(a, x) cout << get<x>(a) << " ";
#define mt make_tuple
#define vec vector

#define int long long

const int mod = (int)1e9 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e18;

typedef vector<int> vii;
typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<vector<int>> vv;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

void solve(int test) {
  int h, w;
  cin >> h >> w;
  int a, b, x, y;
  cin >> a >> b >> x >> y;
  --a, --b, --x, --y;
  char c[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];

  deque<pii> dq;

  int dist[h][w];
  rep(i, 0, h) rep(j, 0, w) dist[i][j] = INF;
  dist[a][b] = 0;

  dq.push_back({a, b});

  while (not dq.empty()) {
    int row = dq.front().ff, col = dq.front().ss;
    dq.pop_front();

    if (row == x and col == y) {
      break;
    }

    for (int dr = -2; dr <= 2; ++dr) {
      for (int dc = -2; dc <= 2; ++dc) {
        int new_row = row + dr;
        int new_col = col + dc;

        if (new_row == row and new_col == col)
          continue;
        if (min(new_col, new_row) < 0 or new_row >= h or new_col >= w)
          continue;
        if (c[new_row][new_col] == '#')
          continue;
        int W = 1;
        if ((abs(new_col - col) == 0 and abs(new_row - row) == 1) or
            (abs(new_col - col) == 1 and abs(new_row - row) == 0))
          W = 0;

        if (dist[new_row][new_col] > dist[row][col] + W) {
          dist[new_row][new_col] = W + dist[row][col];
          if (W == 0)
            dq.push_front({new_row, new_col});
          else
            dq.push_back({new_row, new_col});
        }
      }
    }
  }

  // rep(i,0,h) {
  // 	rep(j,0,w) cout << dist[i][j] << " " ; cout << nl;
  // }

  if (dist[x][y] == INF)
    cout << -1;
  else
    cout << dist[x][y];
}

int32_t main() {

  fast;
  int t = 1;
  // cin>>t;
  rep(i, 1, t + 1) { solve(i); }

  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long

#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

typedef pair<int, int> pii;

const ll INF = 1e18;

const int N = 1e3 + 5;
char c[N][N];
ll mn[N][N];
int h, w, X, Y, A, B;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int Dx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
            0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
int Dy[] = {2,  1,  0, -1, -2, 2,  1,  0, -1, -2, 2,  1, 0,
            -1, -2, 2, 1,  0,  -1, -2, 2, 1,  0,  -1, -2};

int32_t main() {

  fast;

  cin >> h >> w;
  cin >> A >> B;
  cin >> X >> Y;

  bool f = 0;

  rep(i, 1, h + 1) rep(j, 1, w + 1) {
    cin >> c[i][j], mn[i][j] = INF;
    if (c[i][j] == '#')
      f = 1;
  }
  if (f == 0) {
    cout << 0;
    return 0;
  }
  mn[A][B] = 0;

  queue<pii> q;
  q.push({A, B});

  while (not q.empty()) {
    int x = q.front().ff;
    int y = q.front().ss;
    q.pop();

    rep(i, 0, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx > h or nx < 1 or ny > w or ny < 1)
        continue;
      if (c[nx][ny] == '#')
        continue;

      if (mn[nx][ny] > mn[x][y]) {
        mn[nx][ny] = mn[x][y];
        q.push({nx, ny});
      }
    }

    rep(i, 0, 25) {
      int nx = x + Dx[i], ny = y + Dy[i];
      if (nx > h or nx < 1 or ny > w or ny < 1)
        continue;

      if (c[nx][ny] == '#')
        continue;
      if (mn[nx][ny] > 1 + mn[x][y]) {

        mn[nx][ny] = 1 + mn[x][y];
        q.push({nx, ny});
      }
    }
  }

  if (mn[X][Y] == INF)
    cout << -1;
  else
    cout << mn[X][Y];

  return 0;
}