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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define print(r) cout << r << endl
#define prints(r) cout << r << " "
#define printline cout << endl

ll h, w, mr, mc, dr, dc;
ll ans = LONG_LONG_MAX;
vector<string> grid(1e3);
vector<vector<ll>> cost(1e3);
vector<vector<ll>> visited(1e3);

struct sortq {
  bool operator()(const pair<pair<ll, ll>, ll> &a,
                  const pair<pair<ll, ll>, ll> &b) const {
    return a.second < b.second;
  }
};

int costdecacc(ll r, ll c, ll cos) {
  if (r < 0 || r >= h || c < 0 || c >= w || grid[r][c] == '#')
    return 0;
  if (cost[r][c] > cos) {
    cost[r][c] = cos;
    return 1;
  }
  return 0;
}
void bfsmagicvisit(ll rr, ll cc, ll m) {
  // queue<pair<pair<ll, ll>, ll>> Q;
  deque<pair<pair<ll, ll>, ll>> Q;
  // priority_queue<pair<pair<ll, ll>, ll>,
  //                vector<pair<pair<ll, ll>, ll>>,
  //                sortq>
  //     Q;
  if (costdecacc(rr, cc, m))
    Q.push_front({{rr, cc}, m});

  while (!Q.empty()) {
    pair<pair<ll, ll>, ll> top = Q.front();
    ll r = top.first.first, c = top.first.second;
    ll cos = top.second;
    Q.pop_front();

    if (costdecacc(r + 1, c, cos))
      Q.push_front({{r + 1, c}, cos});
    if (costdecacc(r - 1, c, cos))
      Q.push_front({{r - 1, c}, cos});
    if (costdecacc(r, c + 1, cos))
      Q.push_front({{r, c + 1}, cos});
    if (costdecacc(r, c - 1, cos))
      Q.push_front({{r, c - 1}, cos});

    if (costdecacc(r + 1, c + 1, cos + 1))
      Q.push_back({{r + 1, c + 1}, cos + 1});
    if (costdecacc(r + 1, c - 1, cos + 1))
      Q.push_back({{r + 1, c - 1}, cos + 1});
    if (costdecacc(r - 1, c + 1, cos + 1))
      Q.push_back({{r - 1, c + 1}, cos + 1});
    if (costdecacc(r - 1, c - 1, cos + 1))
      Q.push_back({{r - 1, c - 1}, cos + 1});

    for (ll i = -2; i < 3; i++)
      if (costdecacc(r + i, c + 2, cos + 1))
        Q.push_back({{r + i, c + 2}, cos + 1});
    for (ll i = -2; i < 3; i++)
      if (costdecacc(r + i, c - 2, cos + 1))
        Q.push_back({{r + i, c - 2}, cos + 1});
    for (ll i = -1; i < 2; i++)
      if (costdecacc(r - 2, c + i, cos + 1))
        Q.push_back({{r - 2, c + i}, cos + 1});
    for (ll i = -1; i < 2; i++)
      if (costdecacc(r + 2, c + i, cos + 1))
        Q.push_back({{r + 2, c + i}, cos + 1});
    // }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> h >> w;
  cin >> mr >> mc;
  cin >> dr >> dc;
  mr--;
  mc--;
  dr--;
  dc--;
  for (ll i = 0; i < h; i++) {
    cin >> grid[i];
    for (ll j = 0; j < w; j++) {
      cost[i].push_back(LONG_LONG_MAX);
      visited[i].push_back(0);
    }
  }

  bfsmagicvisit(mr, mc, 0);

  // for (ll i = 0; i < h; i++)
  // {
  //     for (ll j = 0; j < w; j++)
  //     {
  //         prints(cost[i][j]);
  //     }
  //     printline;
  // }
  // printline;

  if (cost[dr][dc] == LONG_LONG_MAX)
    print(-1);
  else
    print(cost[dr][dc]);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define print(r) cout << r << endl
#define prints(r) cout << r << " "
#define printline cout << endl

ll h, w, mr, mc, dr, dc;
ll ans = LONG_LONG_MAX;
vector<string> grid(1e3);
vector<vector<ll>> cost(1e3);
vector<vector<ll>> visited(1e3);

struct sortq {
  bool operator()(const pair<pair<ll, ll>, ll> &a,
                  const pair<pair<ll, ll>, ll> &b) const {
    return a.second < b.second;
  }
};

int costdecacc(ll r, ll c, ll cos) {
  if (r < 0)
    return 0;
  if (r >= h)
    return 0;
  if (c < 0)
    return 0;
  if (c >= w)
    return 0;
  if (grid[r][c] == '#')
    return 0;
  if (cost[r][c] > cos) {
    cost[r][c] = cos;
    return 1;
  }
  return 0;
}
void bfsmagicvisit(ll rr, ll cc, ll m) {
  // queue<pair<pair<ll, ll>, ll>> Q;
  priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>, sortq>
      Q;
  if (costdecacc(rr, cc, m))
    Q.push({{rr, cc}, m});

  while (!Q.empty()) {
    pair<pair<ll, ll>, ll> top = Q.top();
    ll r = top.first.first, c = top.first.second;
    ll cos = top.second;
    Q.pop();

    if (costdecacc(r + 1, c, cos))
      Q.push({{r + 1, c}, cos});
    if (costdecacc(r - 1, c, cos))
      Q.push({{r - 1, c}, cos});
    if (costdecacc(r, c + 1, cos))
      Q.push({{r, c + 1}, cos});
    if (costdecacc(r, c - 1, cos))
      Q.push({{r, c - 1}, cos});

    if (costdecacc(r + 1, c + 1, cos + 1))
      Q.push({{r + 1, c + 1}, cos + 1});
    if (costdecacc(r + 1, c - 1, cos + 1))
      Q.push({{r + 1, c - 1}, cos + 1});
    if (costdecacc(r - 1, c + 1, cos + 1))
      Q.push({{r - 1, c + 1}, cos + 1});
    if (costdecacc(r - 1, c - 1, cos + 1))
      Q.push({{r - 1, c - 1}, cos + 1});

    for (ll i = -2; i < 3; i++)
      if (costdecacc(r + i, c + 2, cos + 1))
        Q.push({{r + i, c + 2}, cos + 1});
    for (ll i = -2; i < 3; i++)
      if (costdecacc(r + i, c - 2, cos + 1))
        Q.push({{r + i, c - 2}, cos + 1});
    for (ll i = -1; i < 2; i++)
      if (costdecacc(r - 2, c + i, cos + 1))
        Q.push({{r - 2, c + i}, cos + 1});
    for (ll i = -1; i < 2; i++)
      if (costdecacc(r + 2, c + i, cos + 1))
        Q.push({{r + 2, c + i}, cos + 1});
    // }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> h >> w;
  cin >> mr >> mc;
  cin >> dr >> dc;
  mr--;
  mc--;
  dr--;
  dc--;
  for (ll i = 0; i < h; i++) {
    cin >> grid[i];
    for (ll j = 0; j < w; j++) {
      cost[i].push_back(LONG_LONG_MAX);
      visited[i].push_back(0);
    }
  }

  bfsmagicvisit(mr, mc, 0);

  // for (ll i = 0; i < h; i++)
  // {
  //     for (ll j = 0; j < w; j++)
  //     {
  //         prints(cost[i][j]);
  //     }
  //     printline;
  // }
  // printline;

  if (cost[dr][dc] == LONG_LONG_MAX)
    print(-1);
  else
    print(cost[dr][dc]);

  return 0;
}