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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

int dp[1010][1010];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) rep(j, w) dp[i][j] = INF;
  int sy, sx;
  cin >> sy >> sx;
  --sy, --sx;
  int gy, gx;
  cin >> gy >> gx;
  --gy, --gx;
  char c[h][w];
  rep(i, h) rep(j, w) cin >> c[i][j];
  deque<tuple<int, int, int>> que;
  que.emplace_back(sy, sx, 0);
  dp[sy][sx] = 0;
  while (not que.empty()) {
    auto [y, x, co] = que.front();
    que.pop_front();
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 or h <= ny)
        continue;
      if (nx < 0 or w <= nx)
        continue;
      if (c[ny][nx] == '#')
        continue;
      if (co < dp[ny][nx]) {
        dp[ny][nx] = co;
        que.emplace_front(ny, nx, co);
      }
    }
    for (int dy = -2; dy <= 2; ++dy) {
      for (int dx = -2; dx <= 2; ++dx) {
        int ny = y + dy;
        int nx = x + dx;
        if (ny < 0 or h <= ny)
          continue;
        if (nx < 0 or w <= nx)
          continue;
        if (c[ny][nx] == '#')
          continue;
        if (co + 1 < dp[ny][nx]) {
          dp[ny][nx] = co + 1;
          que.emplace_back(ny, nx, co + 1);
        }
      }
    }
  }
  cout << (dp[gy][gx] == INF ? -1 : dp[gy][gx]) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

/*
 *    coder :: ATUL_PANDEY_2608
 *        >>> INDIA <<<
 */
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(), a.end()
#define mod 1000000007
// #define maxx 200006
#define ll long long
#define quick ios_base::sync_with_stdio(NULL), cin.tie(0);
#define listll vector<long long>
#define listi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define minheap priority_queue<long long, vector<long long>, greater<long long>>
#define rep(i, a, b) for (int i = a; i < b; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// end of #define

// define globals ...
const int maxx = 2e5 + 26;

// write function from here ...

ll a, b, x, y, n, m;
int dx[] = {0, 0, 1, -1, 0, 0, 2, -2};
int dy[] = {-1, 1, 0, 0, 2, -2, 0, 0};

vector<string> adj;

vector<listi> val;

bool check(int i, int j) {

  return i >= 0 && i < n && j >= 0 && j < m && (adj[i][j] == '.');
}

void dfs(int u, int v) {

  for (int i = 0; i < 4; i++) {

    if (i < 4) {

      if (check(u + dx[i], v + dy[i]) &&
          val[u + dx[i]][v + dy[i]] > val[u][v]) {
        val[u + dx[i]][v + dy[i]] = min(val[u + dx[i]][v + dy[i]], val[u][v]);
        dfs(u + dx[i], v + dy[i]);
      }
    }
  }

  for (int j = -2; j <= 2; j++) {
    for (int ii = -2; ii <= 2; ii++) {
      if (ii == 0 && j == 0)
        continue;

      if (check(u + j, v + ii) && val[u + j][v + ii] > val[u][v] + 1) {
        val[u + j][v + ii] = min(val[u + j][v + ii], val[u][v] + 1);
        dfs(u + j, v + ii);
      }
    }
  }
}

int Main() {

  cin >> n >> m;

  cin >> a >> b;
  cin >> x >> y;
  x--, y--;
  a--, b--;

  adj = vector<string>(n + 1);

  for (int i = 0; i < n; i++)
    cin >> adj[i];

  val = vector<listi>(n + 1, listi(m + 1, INT_MAX));

  val[a][b] = 0;

  dfs(a, b);

  // cout<<val[x][y]<<endl;

  if (val[x][y] != INT_MAX) {
    cout << val[x][y];
  } else
    cout << -1 << endl;

  return 0;
}

int main() {
  quick;
  int t = 1;
  // cin>>t;
  while (t--)
    Main();

  return 0;
}