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

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll maze[1005][1005];

void show_table(ll I, ll J) {
  for (ll i = 0; i <= I; i++) {
    for (ll j = 0; j <= J; j++) {
      if (maze[i][j] == INF) {
        cout << "@ ";
      } else {
        cout << maze[i][j] << " ";
      }
    }
    cout << "" << endl;
  }
  cout << "" << endl;
}

int main() {
  ll H, W;
  scanf("%lld %lld", &H, &W);

  ll Ch, Cw;
  scanf("%lld %lld", &Ch, &Cw);
  Ch--;
  Cw--;

  ll Dh, Dw;
  scanf("%lld %lld", &Dh, &Dw);
  Dh--;
  Dw--;

  for (ll y = 0; y < H; y++) {
    string s;
    cin >> s;

    for (ll x = 0; x < s.size(); x++) {
      if (s[x] == '#') {
        maze[y][x] = -1;
      } else {
        maze[y][x] = INF;
      }
    }
  }
  // show_table(H, W);

  // priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, less<pair<ll, pll>>>
  //     que;  // -> 9,8,7,...

  deque<pll> que;

  que.push_front(make_pair(Ch, Cw));
  maze[Ch][Cw] = 0;

  for (;;) {
    if (que.empty()) {
      break;
    }

    auto pos = que.front();
    que.pop_front();
    ll y = pos.first;
    ll x = pos.second;

    // cout << "y:" << (y) << ",x:" << (x) << endl;

    ll c = maze[y][x];

    for (ll oy = -2; oy <= 2; oy++) {
      for (ll ox = -2; ox <= 2; ox++) {
        ll ty = y + oy;
        ll tx = x + ox;

        ll dist = abs(oy) + abs(ox);
        if (dist == 0) {
          continue;
        }
        if (ty >= 0 && ty < H && tx >= 0 && tx < W && maze[ty][tx] > c) {
          if (dist == 1) {
            maze[ty][tx] = c;
            que.push_front(make_pair(ty, tx));
          } else {
            maze[ty][tx] = c + 1;
            que.push_back(make_pair(ty, tx));
          }
        }
      }
    }
  }

  // show_table(H, W);

  ll a = maze[Dh][Dw];

  if (a == INF) {
    cout << (-1) << endl;
  } else {
    cout << (a) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll maze[1005][1005];

void show_table(ll I, ll J) {
  for (ll i = 0; i <= I; i++) {
    for (ll j = 0; j <= J; j++) {
      if (maze[i][j] == INF) {
        cout << "@ ";
      } else {
        cout << maze[i][j] << " ";
      }
    }
    cout << "" << endl;
  }
  cout << "" << endl;
}

int main() {
  ll H, W;
  scanf("%lld %lld", &H, &W);

  ll Ch, Cw;
  scanf("%lld %lld", &Ch, &Cw);
  Ch--;
  Cw--;

  ll Dh, Dw;
  scanf("%lld %lld", &Dh, &Dw);
  Dh--;
  Dw--;

  for (ll y = 0; y < H; y++) {
    string s;
    cin >> s;

    for (ll x = 0; x < s.size(); x++) {
      if (s[x] == '#') {
        maze[y][x] = -1;
      } else {
        maze[y][x] = INF;
      }
    }
  }
  // show_table(H, W);

  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, less<pair<ll, pll>>>
      que; // -> 9,8,7,...

  que.push(make_pair(0, make_pair(Ch, Cw)));
  maze[Ch][Cw] = 0;

  for (;;) {
    if (que.empty()) {
      break;
    }

    auto pos = que.top();
    que.pop();
    ll y = pos.second.first;
    ll x = pos.second.second;

    // cout << "y:" << (y) << ",x:" << (x) << endl;

    ll c = maze[y][x];

    for (ll oy = -2; oy <= 2; oy++) {
      for (ll ox = -2; ox <= 2; ox++) {
        ll ty = y + oy;
        ll tx = x + ox;

        ll dist = abs(oy) + abs(ox);
        if (dist == 0) {
          continue;
        }
        if (ty >= 0 && ty < H && tx >= 0 && tx < W && maze[ty][tx] > c) {
          if (dist == 1) {
            maze[ty][tx] = c;
            que.push(make_pair(c, make_pair(ty, tx)));
          } else {
            maze[ty][tx] = c + 1;
            que.push(make_pair(c + 1, make_pair(ty, tx)));
          }
        }
      }
    }
  }

  // show_table(H, W);

  ll a = maze[Dh][Dw];

  if (a == INF) {
    cout << (-1) << endl;
  } else {
    cout << (a) << endl;
  }
}