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

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll H, W;
ll sx, sy, gx, gy;
ll d[1111][1111];
ll clr[1111][1111];
string s[1111];

void solve() {
  ll i, j, k;
  priority_queue<pair<ll, pll>> q;

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      d[i][j] = INF;
  d[sx][sy] = 0;
  q.push(make_pair(0, pll(sx, sy)));

  while (q.size()) {
    ll x = q.top().sc.ft;
    ll y = q.top().sc.sc;
    ll u = -q.top().ft;
    q.pop();

    if (clr[x][y])
      continue;
    clr[x][y] = 1;

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        ll tx = x + i, ty = y + j;
        if (tx < 0 || tx >= H || ty < 0 || ty >= W)
          continue;
        if (s[tx][ty] == '#')
          continue;

        if (abs(i) <= 1 && abs(j) <= 1 && abs(i + j) == 1) {
          q.push(make_pair(-u, pll(tx, ty)));
          chmin(d[tx][ty], u);
        } else {
          q.push(make_pair(-u - 1, pll(tx, ty)));
          chmin(d[tx][ty], u + 1);
        }
      }
    }
  }
}

int main(void) {
  ll i, j, k;

  cin >> H >> W;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (i = 0; i < H; i++)
    cin >> s[i];

  solve();
  if (d[gx][gy] == INF)
    pt(-1);
  else
    pt(d[gx][gy]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll H, W;
ll sx, sy, gx, gy;
ll d[1111][1111];
ll clr[1111][1111];
string s[1111];

void solve() {
  ll i, j, k;
  priority_queue<pair<ll, pll>> q;

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      d[i][j] = INF;
  d[sx][sy] = 0;
  q.push(make_pair(0, pll(sx, sy)));

  while (q.size()) {
    ll x = q.top().sc.ft;
    ll y = q.top().sc.sc;
    ll u = -q.top().ft;
    q.pop();

    if (d[x][y] < u)
      continue;
    clr[x][y] = 1;

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        ll tx = x + i, ty = y + j;
        if (tx < 0 || tx >= H || ty < 0 || ty >= W)
          continue;
        if (s[tx][ty] == '#')
          continue;
        if (clr[tx][ty])
          continue;

        if (abs(i) <= 1 && abs(j) <= 1 && abs(i + j) == 1) {
          q.push(make_pair(-u, pll(tx, ty)));
          chmin(d[tx][ty], u);
        } else {
          q.push(make_pair(-u - 1, pll(tx, ty)));
          chmin(d[tx][ty], u + 1);
        }
      }
    }
  }
}

int main(void) {
  ll i, j, k;

  cin >> H >> W;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (i = 0; i < H; i++)
    cin >> s[i];

  solve();
  if (d[gx][gy] == INF)
    pt(-1);
  else
    pt(d[gx][gy]);
}