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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
int main() {
  int H, W;
  cin >> H >> W;
  pint C, D;
  cin >> C.first >> C.second;
  cin >> D.first >> D.second;
  C.first--;
  C.second--;
  D.first--;
  D.second--;
  vector<string> Map(H);
  REP(i, 0, H) cin >> Map[i];
  int ans;
  vvi visited(H, vi(W, __INT_MAX__));
  queue<pint> que, que_2;

  que.push({C.first, C.second});
  visited[C.first][C.second] = 0;

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    int y = cur.first;
    int x = cur.second;
    int warp = visited[y][x];
    REP(i, 0, 4) {
      int next_y = y, next_x = x;
      if (i == 0)
        next_y++;
      else if (i == 1)
        next_y--;
      else if (i == 2)
        next_x++;
      else if (i == 3)
        next_x--;
      if (next_y < 0 || next_y >= H || next_x < 0 || next_x >= W)
        continue;
      if (Map[next_y][next_x] == '.' && visited[next_y][next_x] > warp) {
        que.push({next_y, next_x});
        visited[next_y][next_x] = warp;
      } else if (Map[next_y][next_x] == '#') {
        que_2.push({y, x});
      }
    }

    if (que.empty()) {
      while (!que_2.empty()) {
        auto cur = que_2.front();
        que_2.pop();
        int y = cur.first;
        int x = cur.second;
        REP(i, 0, 25) {
          int next_y = y, next_x = x;
          int d_y = i / 5 - 2;
          int d_x = i % 5 - 2;
          next_y += d_y;
          next_x += d_x;
          if (next_y < 0 || next_y >= H || next_x < 0 || next_x >= W)
            continue;
          if (Map[next_y][next_x] == '.' &&
              visited[next_y][next_x] > warp + 1) {
            que.push({next_y, next_x});
            visited[next_y][next_x] = warp + 1;
          }
        }
      }
    }
  }
  /*
  REP(i, 0, H){
      REP(j, 0, W) cout << visited[i][j] << " ";
      cout << endl;
  }*/
  if (visited[D.first][D.second] == __INT_MAX__)
    cout << -1 << endl;
  else
    cout << visited[D.first][D.second] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
int main() {
  int H, W;
  cin >> H >> W;
  pint C, D;
  cin >> C.first >> C.second;
  cin >> D.first >> D.second;
  C.first--;
  C.second--;
  D.first--;
  D.second--;
  vector<string> Map(H);
  REP(i, 0, H) cin >> Map[i];
  int ans;
  vvi visited(H, vi(W, __INT_MAX__));
  queue<pint> que;
  que.push({C.first, C.second});
  visited[C.first][C.second] = 0;
  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    int y = cur.first;
    int x = cur.second;
    int warp = visited[y][x];
    REP(i, 0, 4) {
      int next_y = y, next_x = x;
      if (i == 0)
        next_y++;
      else if (i == 1)
        next_y--;
      else if (i == 2)
        next_x++;
      else if (i == 3)
        next_x--;
      if (next_y < 0 || next_y >= H || next_x < 0 || next_x >= W)
        continue;
      if (Map[next_y][next_x] == '.' && visited[next_y][next_x] > warp) {
        que.push({next_y, next_x});
        visited[next_y][next_x] = warp;
      }
    }
    REP(i, 0, 25) {
      int next_y = y, next_x = x;
      int d_y = i / 5 - 2;
      int d_x = i % 5 - 2;
      next_y += d_y;
      next_x += d_x;
      if (next_y < 0 || next_y >= H || next_x < 0 || next_x >= W)
        continue;
      if (Map[next_y][next_x] == '.' && visited[next_y][next_x] > warp) {
        que.push({next_y, next_x});
        visited[next_y][next_x] = warp + 1;
      }
    }
  }
  /*
  REP(i, 0, H){
      REP(j, 0, W) cout << visited[i][j] << " ";
      cout << endl;
  }*/
  if (visited[D.first][D.second] == __INT_MAX__)
    cout << -1 << endl;
  else
    cout << visited[D.first][D.second] << endl;
}