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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;

#define MAXNUM 100000000
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch += 1;
  cw += 1;
  dh += 1;
  dw += 1;
  char s[h + 4][w + 4];
  int dist[h + 4][w + 4];
  int xvect[4] = {1, 0, 0, -1}, yvect[4] = {0, 1, -1, 0};

  REP(i, h + 4) REP(j, w + 4) {
    s[i][j] = '#';
    dist[i][j] = MAXNUM;
  }
  REP(i, h) REP(j, w) cin >> s[i + 2][j + 2];
  if (s[ch][cw] == '#') {
    cout << -1 << endl;
    return 0;
  }

  // dist[ch][cw]=0;
  deque<pair<pair<int, int>, int>> que;
  que.push_front(make_pair(make_pair(cw, ch), 0));
  while (!que.empty()) {
    pair<pair<int, int>, int> now = que.front();
    que.pop_front();
    int x = now.first.first, y = now.first.second, warp_count = now.second;
    dist[y][x] = warp_count;
    REP(i, 4) {
      int newx = x + xvect[i], newy = y + yvect[i];
      if (s[newy][newx] == '.' and dist[newy][newx] > warp_count) {
        que.push_front(make_pair(make_pair(newx, newy), warp_count));
      }
    }
    REP(i, 5) {
      REP(j, 5) {
        int diffx = x + j - 2, diffy = y + i - 2;
        if (s[diffy][diffx] == '.' and dist[diffy][diffx] > warp_count + 1) {
          que.push_back(make_pair(make_pair(diffx, diffy), warp_count + 1));
        }
      }
    }
    if (dist[dh][dw] != MAXNUM)
      break;
  }
  /*REP(i,h){
      REP(j,w){
          cout << dist[i+2][j+2] << " ";
      }
      cout << endl;
  }*/
  if (dist[dh][dw] == MAXNUM) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[dh][dw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;

#define MAXNUM 100000000
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch += 1;
  cw += 1;
  dh += 1;
  dw += 1;
  char s[h + 4][w + 4];
  int dist[h + 4][w + 4];
  int xvect[4] = {1, 0, 0, -1}, yvect[4] = {0, 1, -1, 0};

  REP(i, h + 4) REP(j, w + 4) {
    s[i][j] = '#';
    dist[i][j] = MAXNUM;
  }
  REP(i, h) REP(j, w) cin >> s[i + 2][j + 2];
  if (s[ch][cw] == '#') {
    cout << -1 << endl;
    return 0;
  }

  dist[ch][cw] = 0;
  queue<pair<pair<int, int>, int>> que;
  que.push(make_pair(make_pair(cw, ch), 0));
  while (!que.empty()) {
    pair<pair<int, int>, int> now = que.front();
    que.pop();
    int x = now.first.first, y = now.first.second, warp_count = now.second;
    REP(i, 4) {
      int newx = x + xvect[i], newy = y + yvect[i];
      if (s[newy][newx] == '.' and dist[newy][newx] > warp_count) {
        dist[newy][newx] = warp_count;
        que.push(make_pair(make_pair(newx, newy), warp_count));
      }
    }
    REP(i, 5) {
      REP(j, 5) {
        int diffx = x + j - 2, diffy = y + i - 2;
        if (s[diffy][diffx] == '.' and dist[diffy][diffx] > warp_count + 1) {
          // cerr << "warp:" << diffx-1  << " " << diffy-1 << endl;
          dist[diffy][diffx] = warp_count + 1;
          que.push(make_pair(make_pair(diffx, diffy), warp_count + 1));
        }
      }
    }
  }
  /*cout << dh <<" " <<  dw  << endl;*/
  /*REP(i,h){
      REP(j,w){
          cout << dist[i+2][j+2] << " ";
      }
      cout << endl;
  }*/
  if (dist[dh][dw] == MAXNUM) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[dh][dw] << endl;
  return 0;
}