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

#include <bits/stdc++.h>

using namespace std;

typedef int li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pi;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define B begin()
#define E end()
#define f(i, l, r) for (li i = l; i <= r; ++i)
#define fr(i, l, r) for (li i = l; i >= r; --i)
#define u_map unordered_map
#define endl "\\n"
#define debug(x) cout << #x << " = " << x << endl;

li fastpow(li base, li exp, li M) {
  li res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % M;

    base = (base * base) % M;
    exp >>= 1;
  }
  return res;
}

const li N = 1e3 + 5;

vector<vi> vis(N, vi(N, 0));
char a[N][N];

vi dx = {-1, 1, 0, 0};
vi dy = {0, 0, 1, -1};

vi dd = {-2, -1, 0, 1, 2};

li n, m;

bool ok(li x, li y) {
  if (x < 0 || x >= n || y >= m || y < 0)
    return false;

  if (a[x][y] == '#')
    return false;

  if (vis[x][y])
    return false;

  return true;
}

void solve() {
  cin >> n >> m;

  li x1, y1, x2, y2;

  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --x2;
  --y1;
  --y2;

  f(i, 0, n - 1) {
    f(j, 0, m - 1) { cin >> a[i][j]; }
  }

  priority_queue<pair<li, pi>, vector<pair<li, pi>>, greater<pair<li, pi>>> pq;

  pq.push({0, {x1, y1}});

  while (!pq.empty()) {
    pair<li, pi> tp = pq.top();
    pq.pop();

    li val = tp.F, x = tp.S.F, y = tp.S.S;

    if (x == x2 && y == y2) {
      cout << val;
      return;
    }

    if (vis[x][y])
      continue;

    vis[x][y] = 1;

    f(i, 0, 3) {
      li xx = x + dx[i];
      li yy = y + dy[i];

      if (ok(xx, yy)) {
        pq.push({val, {xx, yy}});
      }
    }

    f(i, 0, 4) {
      f(j, 0, 4) {
        li xx = x + dd[i];
        li yy = y + dd[j];

        if (ok(xx, yy)) {
          pq.push({val + 1, {xx, yy}});
        }
      }
    }
  }

  cout << -1;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  li t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pi;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define B begin()
#define E end()
#define f(i, l, r) for (li i = l; i <= r; ++i)
#define fr(i, l, r) for (li i = l; i >= r; --i)
#define u_map unordered_map
#define endl "\\n"
#define debug(x) cout << #x << " = " << x << endl;

li fastpow(li base, li exp, li M) {
  li res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % M;

    base = (base * base) % M;
    exp >>= 1;
  }
  return res;
}
const li N = 1e3 + 5;

vector<vi> dp(N, vi(N, 2e9));
vector<vi> vis(N, vi(N, 0));

char a[N][N];

vi dx = {-1, 1, 0, 0};
vi dy = {0, 0, 1, -1};

vi dd = {-2, -1, 0, 1, 2};

li n, m;

void dfs(li x, li y, li val) {
  if (x < 0 || x >= n || y < 0 || y >= m)
    return;

  if (a[x][y] == '#')
    return;

  if (val < dp[x][y]) {
    dp[x][y] = val;
  } else {
    return;
  }

  // dp[x][y]=min(dp[x][y],val);

  // if(vis[x][y])
  //     return;

  // vis[x][y]=1;

  f(i, 0, 3) {
    li xx = x + dx[i];
    li yy = y + dy[i];

    dfs(xx, yy, val);
  }

  f(i, 0, 4) {
    f(j, 0, 4) {
      li xx = x + dd[i];
      li yy = y + dd[j];

      dfs(xx, yy, val + 1);
    }
  }
}

void solve() {
  // li n,m;

  cin >> n >> m;

  li x1, y1, x2, y2;

  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --x2;
  --y1;
  --y2;

  f(i, 0, n - 1) {
    f(j, 0, m - 1) { cin >> a[i][j]; }
  }

  dfs(x1, y1, 0);

  // f(i,0,n-1)
  // {
  //     f(j,0,m-1)
  //     {
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }

  if (dp[x2][y2] == 2e9) {
    cout << -1;
  } else {
    cout << dp[x2][y2];
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  li t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}