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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000

using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vector<int>> c(h, vector<int>());
  rep(i, h) rep(j, w) c[i].push_back(INF);

  priority_queue<pair<int, pi>, vector<pair<int, pi>>, greater<pair<int, pi>>>
      q;
  q.push({0, {ch, cw}});
  while (q.size()) {
    auto cp = q.top();
    q.pop();
    pi now = cp.second;
    int cs = cp.first;

    int y = now.first;
    int x = now.second;
    if (y < 0 || y >= h || x < 0 || x >= w)
      continue; // 盤外は飛ばし

    if (s[y][x] == '#')
      continue; // 壁は飛ばし
    if (c[y][x] <= cs)
      continue; // スコアが既に低ければ飛ばし
    c[y][x] = cs;

    // cout<<cs<<' '<<y<<' '<<x<<' '<<q.size()<<endl;

    // nowから見て4方向とワープ可能位置をpush
    q.push({cs, {y - 1, x}});
    q.push({cs, {y + 1, x}});
    q.push({cs, {y, x + 1}});
    q.push({cs, {y, x - 1}});
    for (int i = -2; i <= 2; i++) {
      if (y - 2 >= 0 && x + i >= 0 && x + i < w && s[y - 2][x + i] == '.')
        q.push({cs + 1, {y - 2, x + i}});
      if (y + 2 < h && x + i >= 0 && x + i < w && s[y + 2][x + i] == '.')
        q.push({cs + 1, {y + 2, x + i}});
      if (x - 2 >= 0 && y + i >= 0 && y + i < h && s[y + i][x - 2] == '.')
        q.push({cs + 1, {y + i, x - 2}});
      if (x + 2 < w && y + i >= 0 && y + i < h && s[y + i][x + 2] == '.')
        q.push({cs + 1, {y + i, x + 2}});
    }
    q.push({cs + 1, {y + 1, x + 1}});
    q.push({cs + 1, {y + 1, x - 1}});
    q.push({cs + 1, {y - 1, x + 1}});
    q.push({cs + 1, {y - 1, x - 1}});
  }

  if (c[dh][dw] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << c[dh][dw] << endl;

  return 0;

  rep(i, h) {
    rep(j, w) cout << c[i][j] << ' ';
    cout << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000

using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vector<int>> c(h, vector<int>());
  rep(i, h) rep(j, w) c[i].push_back(INF);

  priority_queue<pair<int, pi>, vector<pair<int, pi>>, greater<pair<int, pi>>>
      q;
  q.push({0, {ch, cw}});
  while (q.size()) {
    auto cp = q.top();
    q.pop();
    pi now = cp.second;
    int cs = cp.first;

    int y = now.first;
    int x = now.second;
    if (y < 0 || y >= h || x < 0 || x >= w)
      continue; // 盤外は飛ばし

    if (s[y][x] == '#')
      continue; // 壁は飛ばし
    if (c[y][x] <= cs)
      continue; // スコアが既に低ければ飛ばし
    c[y][x] = cs;

    // cout<<cs<<' '<<y<<' '<<x<<' '<<q.size()<<endl;

    // nowから見て4方向とワープ可能位置をpush
    q.push({cs, {y - 1, x}});
    q.push({cs, {y + 1, x}});
    q.push({cs, {y, x + 1}});
    q.push({cs, {y, x - 1}});
    for (int i = -2; i <= 2; i++) {
      if (y - 2 >= 0)
        q.push({cs + 1, {y - 2, x + i}});
      if (y + 2 <= h)
        q.push({cs + 1, {y + 2, x + i}});
      if (x - 2 >= 0)
        q.push({cs + 1, {y + i, x - 2}});
      if (x + 2 <= w)
        q.push({cs + 1, {y + i, x + 2}});
    }
    q.push({cs + 1, {y + 1, x + 1}});
    q.push({cs + 1, {y + 1, x - 1}});
    q.push({cs + 1, {y - 1, x + 1}});
    q.push({cs + 1, {y - 1, x - 1}});
  }

  if (c[dh][dw] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << c[dh][dw] << endl;

  return 0;

  rep(i, h) {
    rep(j, w) cout << c[i][j] << ' ';
    cout << endl;
  }

  return 0;
}