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

#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <vector>
#define P pair<int, int>
using namespace std;
const vector<int> di{-1, 0, 1, 0};
const vector<int> dj{0, -1, 0, 1};

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  vector<vector<char>> S(H + 1, vector<char>(W + 1));
  vector<vector<int>> num(H + 1, vector<int>(W + 1, -1));
  ;
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> S.at(i).at(j);
    }
  }
  num.at(Ch).at(Cw) = 0;
  deque<P> q;
  q.push_front(make_pair(Ch, Cw));
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop_front();
    for (int v = 0; v < 4; v++) {
      int ni = i + di.at(v);
      int nj = j + dj.at(v);
      P nij = make_pair(ni, nj);
      if (ni < 1 || ni > H)
        continue;
      if (nj < 1 || nj > W)
        continue;
      if (S.at(ni).at(nj) == '#')
        continue;
      if (num.at(ni).at(nj) < 0 || num.at(ni).at(nj) > num.at(i).at(j)) {
        num.at(ni).at(nj) = num.at(i).at(j);
        q.push_front(nij);
      }
    }
    for (int ii = -2; ii < 3; ii++) {
      for (int jj = -2; jj < 3; jj++) {
        int ni = i + ii;
        int nj = j + jj;
        P nij = make_pair(ni, nj);
        if (ni < 1 || ni > H)
          continue;
        if (nj < 1 || nj > W)
          continue;
        if (S.at(ni).at(nj) == '#')
          continue;
        if (num.at(ni).at(nj) < 0) {
          num.at(ni).at(nj) = num.at(i).at(j) + 1;
          q.push_back(nij);
        }
      }
    }
  }
  cout << num.at(Dh).at(Dw);
  return 0;
}

Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <map>
#include <vector>
#define P pair<int, int>
using namespace std;
const vector<int> di{-1, 0, 1, 0};
const vector<int> dj{0, -1, 0, 1};

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  vector<vector<char>> S(H + 1, vector<char>(W + 1));
  map<P, int> num;
  for (int i = 1; i < H + 1; i++) {
    for (int j = 1; j < W + 1; j++) {
      cin >> S.at(i).at(j);
      num[make_pair(i, j)] = -1;
    }
  }
  num.at(make_pair(Ch, Cw)) = 0;
  deque<P> q;
  q.push_back(make_pair(Ch, Cw));
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    P ij = q.front();
    q.pop_front();
    bool wall = false;
    for (int v = 0; v < 4; v++) {
      int ni = i + di.at(v);
      int nj = j + dj.at(v);
      P nij = make_pair(ni, nj);
      if (ni < 1 || ni > H)
        continue;
      if (nj < 1 || nj > W)
        continue;
      if (S.at(ni).at(nj) == '#') {
        wall = true;
        continue;
      }
      if (num.at(nij) < 0 || num.at(nij) > num.at(ij)) {
        num.at(nij) = num.at(ij);
        q.push_front(nij);
      }
    }
    for (int ii = -2; ii < 3 && wall; ii++) {
      for (int jj = -2; jj < 3; jj++) {
        int ni = i + ii;
        int nj = j + jj;
        P nij = make_pair(ni, nj);
        if (ni < 1 || ni > H)
          continue;
        if (nj < 1 || nj > W)
          continue;
        if (S.at(ni).at(nj) == '#')
          continue;
        if (num.at(nij) < 0) {
          num.at(nij) = num.at(ij) + 1;
          q.push_back(nij);
        }
      }
    }
  }
  cout << num.at(make_pair(Dh, Dw));
  return 0;
}