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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

const long double EPS = 1e-10;
const long long INF = 1e18;
const int iINF = 1e9;
const int MOD = (int)1e9 + 7;
const long double PI = acos(-1.0L);

const int max_h = 10;
const int max_w = 10;

int H, W;
int CH, CW;
int DH, DW;
int cnt = 0;
P step[4] = {
    P(1, 0),
    P(0, 1),
    P(-1, 0),
    P(0, -1),
};
vector<vector<ll>> note;

int bfs(vector<vector<char>> map, P start, P goal) {
  deque<P> q;
  // スタート地点登録
  q.push_back(start);
  note[start.first][start.second] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop_front();
    int y = p.first;
    int x = p.second;
    rep(4) {
      int nx, ny;
      ny = y + step[i].first;
      nx = x + step[i].second;
      if (nx <= 0 || ny <= 0 || nx > W || ny > H)
        continue;
      // 壁の場合
      if (map[ny][nx] == '#')
        continue;
      if (chmin(note[ny][nx], note[y][x])) {
        q.push_front(P(ny, nx));
      }
    }
    rep(xx, -2, 3) rep(yy, -2, 3) {
      int nx2, ny2;
      ny2 = y + yy;
      nx2 = x + xx;
      if (nx2 <= 0 || ny2 <= 0 || nx2 > W || ny2 > H)
        continue;
      // 壁の場合
      if (map[ny2][nx2] == '#')
        continue;
      if (chmin(note[ny2][nx2], note[y][x] + 1)) {
        q.push_back(P(ny2, nx2));
      }
    }
  }
  return 0;
}

int main() { // TODO
  cin >> H >> W;
  cin >> CH >> CW;
  cin >> DH >> DW;
  P c = P(CH, CW);
  P d = P(DH, DW);

  vector<vector<char>> map(H + 1, vector<char>(W + 1));
  note.assign(H + 1, vector<ll>(W + 1, INF));
  // vector<vector<ll>> note(H + 1, vector<ll>(W + 1, INF));
  rep2(i, H) rep2(j, W) cin >> map[i + 1][j + 1];

  if (map[c.first][c.second] == '#' || map[d.first][d.second] == '#') {
    cout << -1 << endl;
  }
  int res = bfs(map, c, d);
  ll ans = note[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

const long double EPS = 1e-10;
const long long INF = 1e18;
const int iINF = 1e9;
const int MOD = (int)1e9 + 7;
const long double PI = acos(-1.0L);

const int max_h = 10;
const int max_w = 10;

int H, W;
int CH, CW;
int DH, DW;
int cnt = 0;
P step[4] = {
    P(1, 0),
    P(0, 1),
    P(-1, 0),
    P(0, -1),
};
queue<P> q;
vector<vector<ll>> note;

int bfs(vector<vector<char>> map, P start, P goal) {
  // スタート地点登録
  q.push(start);
  note[start.first][start.second] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first;
    int x = p.second;
    rep(4) {
      int nx, ny;
      ny = y + step[i].first;
      nx = x + step[i].second;
      if (nx <= 0 || ny <= 0 || nx > W || ny > H)
        continue;
      // 壁の場合
      if (map[ny][nx] == '#')
        continue;
      if (chmin(note[ny][nx], note[y][x])) {
        q.push(P(ny, nx));
      }
    }
    rep(xx, -2, 3) rep(yy, -2, 3) {
      int nx2, ny2;
      ny2 = y + yy;
      nx2 = x + xx;
      if (nx2 <= 0 || ny2 <= 0 || nx2 > W || ny2 > H)
        continue;
      // 壁の場合
      if (map[ny2][nx2] == '#')
        continue;
      if (chmin(note[ny2][nx2], note[y][x] + 1)) {
        q.push(P(ny2, nx2));
      }
    }
  }
  return 0;
}

int main() { // TODO
  cin >> H >> W;
  cin >> CH >> CW;
  cin >> DH >> DW;
  P c = P(CH, CW);
  P d = P(DH, DW);

  vector<vector<char>> map(H + 1, vector<char>(W + 1));
  note.assign(H + 1, vector<ll>(W + 1, INF));
  // vector<vector<ll>> note(H + 1, vector<ll>(W + 1, INF));
  rep2(i, H) rep2(j, W) cin >> map[i + 1][j + 1];

  if (map[c.first][c.second] == '#' || map[d.first][d.second] == '#') {
    cout << -1 << endl;
  }
  int res = bfs(map, c, d);
  ll ans = note[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}