Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int H, W, Ch, Cw, Dh, Dw;

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

bool movable_a(vector<vector<char>> &S, int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H) {
    return false;
  } else if (S[y][x] == '.') {
    return true;
  } else {
    return false;
  }
}

void bfs(vector<vector<char>> &S, vector<vector<int>> &seen) {
  queue<P> que;
  set<P> warp;
  que.push(make_pair(Ch, Cw));
  int cnt = 0;
  while (1) {
    P now = que.front();
    int now_x, now_y;
    now_x = now.second;
    now_y = now.first;
    que.pop();
    rep(i, 4) {
      int x, y;
      x = now_x + dx[i];
      y = now_y + dy[i];
      if (movable_a(S, x, y)) {
        if (seen[y][x] == -1) {
          que.push(make_pair(y, x));
          seen[y][x] = cnt;
        }
      }
    }
    rep(h, 5) {
      rep(w, 5) {
        int xx, yy;
        xx = now_x + w - 2;
        yy = now_y + h - 2;
        if (movable_a(S, xx, yy)) {
          if (seen[yy][xx] == -1) {
            warp.insert(make_pair(yy, xx));
          }
        }
      }
    }

    if (que.size() == 0) {
      cnt++;
      for (auto p : warp) {
        int xx, yy;
        xx = p.second;
        yy = p.first;
        if (seen[yy][xx] == -1) {
          que.push(make_pair(yy, xx));
          seen[yy][xx] = cnt;
        }
      }
    }
    // cout << que.size() << endl;
    if (que.size() == 0) {
      break;
    }
  }
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;
  vector<vector<char>> S(H, vector<char>(W));
  vector<vector<int>> seen(H, vector<int>(W, -1));

  rep(i, H) {
    string tmp;
    cin >> tmp;
    rep(j, W) { S[i][j] = tmp[j]; }
  }

  bfs(S, seen);

  cout << seen[Dh][Dw] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int H, W, Ch, Cw, Dh, Dw;

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

bool movable_a(vector<vector<char>> &S, int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H) {
    return false;
  } else if (S[y][x] == '.') {
    return true;
  } else {
    return false;
  }
}

void bfs(vector<vector<char>> &S, vector<vector<int>> &seen) {
  queue<P> que;
  set<P> warp;
  que.push(make_pair(Ch, Cw));
  int cnt = 0;
  while (1) {
    P now = que.front();
    int now_x, now_y;
    now_x = now.second;
    now_y = now.first;
    que.pop();
    rep(i, 4) {
      int x, y;
      x = now_x + dx[i];
      y = now_y + dy[i];
      if (movable_a(S, x, y)) {
        if (seen[y][x] == -1) {
          que.push(make_pair(y, x));
          seen[y][x] = cnt;
        }
      }
    }
    rep(h, 5) {
      rep(w, 5) {
        int xx, yy;
        xx = now_x + w - 2;
        yy = now_y + h - 2;
        if (movable_a(S, xx, yy)) {
          if (seen[yy][xx] == -1) {
            warp.insert(make_pair(yy, xx));
          }
        }
      }
    }

    if (que.size() == 0) {
      cnt++;
      for (auto p : warp) {
        int xx, yy;
        xx = p.second;
        yy = p.first;
        if (seen[yy][xx] == -1) {
          que.push(make_pair(yy, xx));
          seen[yy][xx] = cnt;
        }
      }
      warp.clear();
    }
    // cout << que.size() << endl;
    if (que.size() == 0) {
      break;
    }
  }
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;
  vector<vector<char>> S(H, vector<char>(W));
  vector<vector<int>> seen(H, vector<int>(W, -1));

  rep(i, H) {
    string tmp;
    cin >> tmp;
    rep(j, W) { S[i][j] = tmp[j]; }
  }

  bfs(S, seen);

  cout << seen[Dh][Dw] << endl;
}