Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

void myadd(vector<vector<int>> &vec, queue<pair<int, int>> &q, int x, int y) {
  if (vec[x][y] != -2) {
    if (vec[x][y] < vec[x + 1][y] || vec[x + 1][y] == -1) {
      vec[x + 1][y] = vec[x][y];
      q.push(make_pair(x + 1, y));
    }
    if (vec[x][y] < vec[x - 1][y] || vec[x - 1][y] == -1) {
      vec[x - 1][y] = vec[x][y];
      q.push(make_pair(x - 1, y));
    }
    if (vec[x][y] < vec[x][y + 1] || vec[x][y + 1] == -1) {
      vec[x][y + 1] = vec[x][y];
      q.push(make_pair(x, y + 1));
    }
    if (vec[x][y] < vec[x][y - 1] || vec[x][y - 1] == -1) {
      vec[x][y - 1] = vec[x][y];
      q.push(make_pair(x, y - 1));
    }
    for (int i = -2; i < 3; ++i) {
      for (int j = -2; j < 3; ++j) {
        if (vec[x][y] < vec[x + i][y + j] || vec[x + i][y + j] == -1) {
          vec[x + i][y + j] = vec[x][y] + 1;
          q.push(make_pair(x + i, y + j));
        }
      }
    }
  }
}

int main() {
  int H, W;
  int Ch, Cw;
  int Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  Ch += 3;
  Cw += 3;
  Dh += 3;
  Dw += 3;

  int offset = 4;

  vector<vector<int>> vec(H + 10, vector<int>(W + 10, -2));
  map<char, int> mp;
  mp['.'] = -1;
  mp['#'] = -2;
  for (int i = offset; i < H + offset; ++i) {
    for (int j = offset; j < W + offset; ++j) {
      char c;
      cin >> c;
      vec[i][j] = mp[c];
    }
  }

  queue<pair<int, int>> que;
  vec[Ch][Cw] = 0;
  myadd(vec, que, Ch, Cw);

  while (que.empty() != 1) {
    auto a = que.front();
    que.pop();
    myadd(vec, que, a.first, a.second);
  }

  /*
  for (int i = offset; i < H + offset; ++i) {
          for (int j = offset; j < W + offset; ++j) {
                  cout << vec[i][j] << "\t";
          }
          cout << endl;
  }
  */
  cout << vec[Dh][Dw] << endl;
  ;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

void myadd(vector<vector<int>> &vec, queue<pair<int, int>> &q, int x, int y) {
  if (vec[x][y] != -2) {
    if (vec[x][y] < vec[x + 1][y] || vec[x + 1][y] == -1) {
      vec[x + 1][y] = vec[x][y];
      q.push(make_pair(x + 1, y));
    }
    if (vec[x][y] < vec[x - 1][y] || vec[x - 1][y] == -1) {
      vec[x - 1][y] = vec[x][y];
      q.push(make_pair(x - 1, y));
    }
    if (vec[x][y] < vec[x][y + 1] || vec[x][y + 1] == -1) {
      vec[x][y + 1] = vec[x][y];
      q.push(make_pair(x, y + 1));
    }
    if (vec[x][y] < vec[x][y - 1] || vec[x][y - 1] == -1) {
      vec[x][y - 1] = vec[x][y];
      q.push(make_pair(x, y - 1));
    }
    for (int i = -2; i < 3; ++i) {
      for (int j = -2; j < 3; ++j) {
        if (vec[x][y] + 1 < vec[x + i][y + j] || vec[x + i][y + j] == -1) {
          vec[x + i][y + j] = vec[x][y] + 1;
          q.push(make_pair(x + i, y + j));
        }
      }
    }
  }
}

int main() {
  int H, W;
  int Ch, Cw;
  int Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  Ch += 3;
  Cw += 3;
  Dh += 3;
  Dw += 3;

  int offset = 4;

  vector<vector<int>> vec(H + 10, vector<int>(W + 10, -2));
  map<char, int> mp;
  mp['.'] = -1;
  mp['#'] = -2;
  for (int i = offset; i < H + offset; ++i) {
    for (int j = offset; j < W + offset; ++j) {
      char c;
      cin >> c;
      vec[i][j] = mp[c];
    }
  }

  queue<pair<int, int>> que;
  vec[Ch][Cw] = 0;
  myadd(vec, que, Ch, Cw);

  while (que.empty() != 1) {
    auto a = que.front();
    que.pop();
    myadd(vec, que, a.first, a.second);
  }

  /*
  for (int i = offset; i < H + offset; ++i) {
          for (int j = offset; j < W + offset; ++j) {
                  cout << vec[i][j] << "\t";
          }
          cout << endl;
  }
  */
  cout << vec[Dh][Dw] << endl;
  ;
  return 0;
}