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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define pi pair<int, int>
#define MAXN 200005
using namespace std;
using namespace __gnu_pbds;

ll n, m, dp[1005][1005], vis[1005][1005], ch, dh, cw, dw;
char s[1005][1005];
ll r[] = {1,  -1, 0, 0, -2, -2, -2, -2, -2, -1, -1, -1,
          -1, 1,  1, 1, 1,  2,  2,  2,  2,  2,  0,  0};
ll c[] = {0, 0,  1,  -1, -2, -1, 0,  1, 2, -2, -1, 1,
          2, -2, -1, 1,  2,  -2, -1, 0, 1, 2,  -2, 2};

bool check(ll x, ll y) {
  if (x == 0 || y == 0 || x == n + 1 || y == m + 1)
    return 0;
  else
    return 1;
}

void bfs(ll x1, ll y1) {
  deque<pi> q;
  q.push_back(mp(x1, y1));
  while (!q.empty()) {
    pi p = q.front();
    q.pop_front();
    int x = p.ff, y = p.ss;
    vis[x][y] = 1;
    for (ll i = 0; i < 24; i++) {
      if (check(x + r[i], y + c[i]) && s[x + r[i]][y + c[i]] == '.' &&
          !vis[x + r[i]][y + c[i]]) {
        if (i < 4) {
          dp[x + r[i]][y + c[i]] = min(dp[x + r[i]][y + c[i]], dp[x][y]);
          q.push_front(mp(x + r[i], y + c[i]));
        } else {
          dp[x + r[i]][y + c[i]] = min(dp[x + r[i]][y + c[i]], dp[x][y] + 1);
          q.push_back(mp(x + r[i], y + c[i]));
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      dp[i][j] = INT_MAX;
      cin >> s[i][j];
    }
  }
  dp[ch][cw] = 0;
  bfs(ch, cw);
  // for (ll i = 1; i <= n; i++)
  // {
  //     for (ll j = 1; j <= m; j++)
  //         cout << dp[i][j] << " ";
  //     cout << endl;
  // }
  if (dp[dh][dw] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define pi pair<int, int>
#define MAXN 200005
using namespace std;
using namespace __gnu_pbds;

ll n, m, dp[1005][1005], vis[1005][1005], ch, dh, cw, dw;
char s[1005][1005];
ll r[] = {1,  -1, 0, 0, -2, -2, -2, -2, -2, -1, -1, -1,
          -1, 1,  1, 1, 1,  2,  2,  2,  2,  2,  0,  0};
ll c[] = {0, 0,  1,  -1, -2, -1, 0,  1, 2, -2, -1, 1,
          2, -2, -1, 1,  2,  -2, -1, 0, 1, 2,  -2, 2};

bool check(ll x, ll y) {
  if (x == 0 || y == 0 || x == n + 1 || y == m + 1)
    return 0;
  else
    return 1;
}

void bfs(ll x1, ll y1) {
  queue<pi> q;
  q.push(mp(x1, y1));
  while (!q.empty()) {
    pi p = q.front();
    q.pop();
    int x = p.ff, y = p.ss;
    vis[x][y] = 1;
    int flag = 0;
    for (ll i = 0; i < 4; i++) {
      if (check(x + r[i], y + c[i]) && s[x + r[i]][y + c[i]] == '.' &&
          !vis[x + r[i]][y + c[i]]) {
        flag = 1;
        dp[x + r[i]][y + c[i]] = min(dp[x + r[i]][y + c[i]], dp[x][y]);
        q.push(mp(x + r[i], y + c[i]));
      }
    }
    if (flag == 0) {
      for (ll i = 4; i < 24; i++) {
        if (check(x + r[i], y + c[i]) && s[x + r[i]][y + c[i]] == '.' &&
            !vis[x + r[i]][y + c[i]]) {
          dp[x + r[i]][y + c[i]] = min(dp[x + r[i]][y + c[i]], dp[x][y] + 1);
          q.push(mp(x + r[i], y + c[i]));
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      dp[i][j] = INT_MAX;
      cin >> s[i][j];
    }
  }
  dp[ch][cw] = 0;
  bfs(ch, cw);
  // for (ll i = 1; i <= n; i++)
  // {
  //     for (ll j = 1; j <= m; j++)
  //         cout << dp[i][j] << " ";
  //     cout << endl;
  // }
  if (dp[dh][dw] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}