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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;

ll solver(vector<vector<ll>> &s, ll H, ll W, ll ch, ll cw, ll dh, ll dw) {
  ll cost[H][W];
  rep(i, 0, H) {
    rep(j, 0, W) { cost[i][j] = -1; }
  }
  queue<pair<ll, ll>> p, q;
  q.push(make_pair(ch, cw));
  cost[ch][cw] = 0;
  ll costcnt = 0;
  ll flag = true;
  while (flag) {
    flag = false;
    while (!q.empty()) {
      p.push(q.front());
      ll qh = q.front().first, qw = q.front().second;
      q.pop();
      rep(i, qh - 1, qh + 2) {
        rep(j, qw - 1, qw + 2) {
          if (i >= 0 && j >= 0 && i < H && j < W && s[i][j] != -1 &&
              cost[i][j] == -1 && abs(qh - i) + abs(qw - j) == 1) {
            q.push(make_pair(i, j));
            cost[i][j] = costcnt;
          }
        }
      }
    }
    while (!p.empty()) {
      ll ph = p.front().first, pw = p.front().second;
      p.pop();
      rep(i, ph - 2, ph + 3) {
        rep(j, pw - 2, pw + 3) {
          if (i >= 0 && j >= 0 && i < H && j < W && s[i][j] != -1 &&
              cost[i][j] == -1) {
            flag = true;
            cost[i][j] = costcnt + 1;
            q.push(make_pair(i, j));
          }
        }
      }
    }
    ++costcnt;
  }
  return cost[dh][dw];
}
int main() {
  ll H, W;
  cin >> H >> W;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<vector<ll>> s(H, vector<ll>(W, 0));
  rep(i, 0, H) {
    string tmpstr;
    cin >> tmpstr;
    rep(j, 0, W) { s[i][j] = tmpstr[j] == '#' ? -1 : 1; }
  }
  --ch, --cw, --dh, --dw;
  cout << solver(s, H, W, ch, cw, dh, dw) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;

ll solver(vector<vector<ll>> &s, ll H, ll W, ll ch, ll cw, ll dh, ll dw) {
  ll cost[H][W];
  rep(i, 0, H) {
    rep(j, 0, W) { cost[i][j] = -1; }
  }
  queue<pair<ll, ll>> p, q;
  cost[ch][cw] = 0;
  ll costcnt = 0;
  ll flag = true;
  while (flag) {
    flag = false;
    rep(i, 0, H) {
      rep(j, 0, W) {
        if (cost[i][j] == costcnt) {
          q.push(make_pair(i, j));
          p.push(make_pair(i, j));
        }
      }
    }
    while (!q.empty()) {
      ll qh = q.front().first, qw = q.front().second;
      q.pop();
      rep(i, qh - 1, qh + 2) {
        rep(j, qw - 1, qw + 2) {
          if (i >= 0 && j >= 0 && i < H && j < W && s[i][j] != -1 &&
              cost[i][j] == -1 && abs(qh - i) + abs(qw - j) == 1) {
            q.push(make_pair(i, j));
            p.push(make_pair(i, j));
            cost[i][j] = costcnt;
          }
        }
      }
    }
    while (!p.empty()) {
      ll ph = p.front().first, pw = p.front().second;
      p.pop();
      rep(i, ph - 2, ph + 3) {
        rep(j, pw - 2, pw + 3) {
          if (i >= 0 && j >= 0 && i < H && j < W && s[i][j] != -1 &&
              cost[i][j] == -1) {
            flag = true;
            cost[i][j] = costcnt + 1;
          }
        }
      }
    }
    ++costcnt;
  }
  return cost[dh][dw];
}
int main() {
  ll H, W;
  cin >> H >> W;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<vector<ll>> s(H, vector<ll>(W, 0));
  rep(i, 0, H) {
    string tmpstr;
    cin >> tmpstr;
    rep(j, 0, W) { s[i][j] = tmpstr[j] == '#' ? -1 : 1; }
  }
  --ch, --cw, --dh, --dw;
  cout << solver(s, H, W, ch, cw, dh, dw) << endl;
}