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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define endl '\\n'

const ll MOD = 1000000007;

vector<pi> a = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<pi> b = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                {0, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2},
                {2, -2},  {2, -1},  {2, 0},  {2, 1},  {2, 2}};

struct cmp {
  bool operator()(vector<int> a, vector<int> b) { return a[0] > b[0]; }
};

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int ch, cw, dh, dw;
  scanf("%d%d%d%d", &ch, &cw, &dh, &dw);
  vector<vector<int>> s(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    char temp[w];
    scanf("%s", temp);
    for (int j = 0; j < w; j++)
      if (temp[j] == '#')
        s[i][j] = -1;
  }
  deque<vector<int>> pq;
  pq.push_front({1, ch - 1, cw - 1});
  while (!pq.empty()) {
    vector<int> temp = pq.front();
    pq.pop_front();
    if (s[temp[1]][temp[2]] == 0 || s[temp[1]][temp[2]] > temp[0]) {
      s[temp[1]][temp[2]] = temp[0];
      for (pi i : a) {
        if (temp[1] + i.f >= 0 && temp[1] + i.f < h && temp[2] + i.s >= 0 &&
            temp[2] + i.s < w &&
            (s[temp[1] + i.f][temp[2] + i.s] == 0 ||
             s[temp[1] + i.f][temp[2] + i.s] > temp[0])) {
          pq.push_front({temp[0], temp[1] + i.f, temp[2] + i.s});
        }
      }
      for (pi i : b) {
        if (temp[1] + i.f >= 0 && temp[1] + i.f < h && temp[2] + i.s >= 0 &&
            temp[2] + i.s < w &&
            (s[temp[1] + i.f][temp[2] + i.s] == 0 ||
             s[temp[1] + i.f][temp[2] + i.s] > temp[0])) {
          pq.push_back({temp[0] + 1, temp[1] + i.f, temp[2] + i.s});
        }
      }
    }
  }
  if (s[dh - 1][dw - 1] < 1) {
    printf("-1");
  } else {
    printf("%d", s[dh - 1][dw - 1] - 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define endl '\\n'

const ll MOD = 1000000007;

vector<pi> a = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<pi> b = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                {0, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2},
                {2, -2},  {2, -1},  {2, 0},  {2, 1},  {2, 2}};

struct cmp {
  bool operator()(vector<int> a, vector<int> b) { return a[0] > b[0]; }
};

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int ch, cw, dh, dw;
  scanf("%d%d%d%d", &ch, &cw, &dh, &dw);
  vector<vector<int>> s(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    char temp[w];
    scanf("%s", temp);
    for (int j = 0; j < w; j++)
      if (temp[j] == '#')
        s[i][j] = -1;
  }
  queue<vector<int>> pq;
  pq.push({1, ch - 1, cw - 1});
  while (!pq.empty()) {
    vector<int> temp = pq.front();
    pq.pop();
    if (s[temp[1]][temp[2]] == 0 || s[temp[1]][temp[2]] > temp[0]) {
      s[temp[1]][temp[2]] = temp[0];
      for (pi i : a) {
        if (temp[1] + i.f >= 0 && temp[1] + i.f < h && temp[2] + i.s >= 0 &&
            temp[2] + i.s < w &&
            (s[temp[1] + i.f][temp[2] + i.s] == 0 ||
             s[temp[1] + i.f][temp[2] + i.s] > temp[0])) {
          pq.push({temp[0], temp[1] + i.f, temp[2] + i.s});
        }
      }
      for (pi i : b) {
        if (temp[1] + i.f >= 0 && temp[1] + i.f < h && temp[2] + i.s >= 0 &&
            temp[2] + i.s < w &&
            (s[temp[1] + i.f][temp[2] + i.s] == 0 ||
             s[temp[1] + i.f][temp[2] + i.s] > temp[0])) {
          pq.push({temp[0] + 1, temp[1] + i.f, temp[2] + i.s});
        }
      }
    }
  }
  if (s[dh - 1][dw - 1] < 1) {
    printf("-1");
  } else {
    printf("%d", s[dh - 1][dw - 1] - 1);
  }
  return 0;
}