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

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define repr(i, r, l) for (ll i = (ll)(r); i > (ll)(l); i--)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define all(p) p.begin(), p.end()
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 998244353
#define endl "\\n"
#define inf 1e18
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 20005;
ll ceel(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
ll modneg(ll a, ll m) {
  a = a % m;
  if (a < 0)
    a = a + m;
  return a;
}
ll powermod(ll x, unsigned ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
ll dp[1005][1005], n, m, d1, d2;
bool arr[1005][1005];
vector<ll> x{-1, 1, 0, 0};
vector<ll> y{0, 0, -1, +1};
// void dfs(ll a,ll b)
// {
//     cout<<a<<" "<<b<<" "<<dp[a][b]<<endl;
//     if(a==d1 and b==d2)
//     {
//         cout<<dp[a][b];
//         exit(0);
//     }
//     rep(i,0,4)
//     {
//         ll x1=min(max(b+x[i],(ll)0),m-1);
//         ll y1=min(max(a+y[i],(ll)0),n-1);
//         // cout<<" "<<y1<<" "<<x1<<" "<<arr[y1][x1]<<" "<<dp[y1][x1]<<endl;
//         if(!arr[y1][x1] and dp[y1][x1]>dp[a][b])
//         {
//             dp[y1][x1]=dp[a][b];
//             dfs(y1,x1);
//         }
//     }
//     rep(i,max(a-2,(ll)0),min(a+3,n))
//     {
//         rep(j,max(b-2,(ll)0),min(b+3,m))
//         {
//             // cout<<" "<<i<<" "<<j<<" "<<dp[i][j]<<" "<<arr[i][j]<<endl;
//             if(!arr[i][j] and dp[i][j]>dp[a][b]+1)
//             {
//                 dp[i][j]=dp[a][b]+1;
//                 dfs(i,j);
//             }
//         }
//     }
// }

int main() {

  // fastIO;
  ll test, s1, s2, c, d, i, j, sum = 0;
  scanf("%lld %lld %lld %lld %lld %lld", &n, &m, &s1, &s2, &d1, &d2);
  // cin>>n>>m>>s1>>s2>>d1>>d2;
  char s[m];
  rep(i, 0, n) {
    scanf("%s", s);
    rep(j, 0, m) {
      char c = s[j];
      if (c == '#')
        arr[i][j] = 1;
      // cout<<arr[i][j];
    }
    // cout<<endl;
  }

  rep(i, 0, n + 3) { rep(j, 0, m + 3) dp[i][j] = inf; }
  s1--, s2--, d1--, d2--;
  dp[s1][s2] = 0;
  deque<pair<ll, ll>> q;
  q.push_front({s1, s2});
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    ll a = p.fi, b = p.se;
    if (a == d1 and b == d2) {
      printf("%lld\\n", dp[d1][d2]);
      exit(0);
    }
    rep(i, 0, 4) {
      ll x1 = min(max(b + x[i], (ll)0), m - 1);
      ll y1 = min(max(a + y[i], (ll)0), n - 1);
      // cout<<" "<<y1<<" "<<x1<<" "<<arr[y1][x1]<<" "<<dp[y1][x1]<<endl;
      if (!arr[y1][x1] and dp[y1][x1] > dp[a][b]) {
        dp[y1][x1] = dp[a][b];
        q.push_front({y1, x1});
      }
    }
    rep(i, max(a - 2, (ll)0), min(a + 3, n)) {
      rep(j, max(b - 2, (ll)0), min(b + 3, m)) {
        // cout<<" "<<i<<" "<<j<<" "<<dp[i][j]<<" "<<arr[i][j]<<endl;
        if (!arr[i][j] and dp[i][j] > dp[a][b] + 1) {
          dp[i][j] = dp[a][b] + 1;
          q.push_back({i, j});
        }
      }
    }
  }

  // dfs(s1,s2);
  if (dp[d1][d2] == inf)
    printf("-1\\n");
  else
    printf("%lld\\n", dp[d1][d2]);
  // cout<<dp[d1][d2]<<endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define repr(i, r, l) for (ll i = (ll)(r); i > (ll)(l); i--)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define all(p) p.begin(), p.end()
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 998244353
#define endl "\\n"
#define inf 1e7
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 20005;
ll ceel(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
ll modneg(ll a, ll m) {
  a = a % m;
  if (a < 0)
    a = a + m;
  return a;
}
ll powermod(ll x, unsigned ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
int dp[1005][1005], n, m, d1, d2;
bool arr[1005][1005];
int x[] = {-1, 1, 0, 0};
int y[] = {0, 0, -1, +1};

int main() {

  int test, s1, s2, c, d, i, j, sum = 0;
  scanf("%d %d %d %d %d %d", &n, &m, &s1, &s2, &d1, &d2);
  char s[m];
  rep(i, 0, n) {
    scanf("%s", s);
    rep(j, 0, m) {
      char c = s[j];
      if (c == '#')
        arr[i][j] = 1;
    }
  }
  memset(dp, 0x3f, sizeof(dp));
  s1--, s2--, d1--, d2--;
  dp[s1][s2] = 0;
  queue<pair<int, int>> q;
  q.push({s1, s2});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int a = p.fi, b = p.se;

    rep(i, max(a - 2, (int)0), min(a + 3, n)) {
      rep(j, max(b - 2, (int)0), min(b + 3, m)) {
        // cout<<" "<<i<<" "<<j<<" "<<dp[i][j]<<" "<<arr[i][j]<<endl;
        int d = abs(i - a) + abs(j - b) <= 1 ? 0 : 1;
        if (!arr[i][j] and dp[i][j] > dp[a][b] + d) {

          dp[i][j] = dp[a][b] + d;
          q.push({i, j});
        }
      }
    }
  }

  // dfs(s1,s2);
  if (dp[d1][d2] == 0x3f3f3f3f)
    printf("-1\\n");
  else
    printf("%d\\n", dp[d1][d2]);
  // cout<<dp[d1][d2]<<endl;
  return 0;
}