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

#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int H, W;

void move(int y, int x, int c, vector<vector<int>> &S,
          std::queue<std::pair<std::pair<int, int>, int>> &q) {
  if (x >= 0 && x < W && y >= 0 && y < H) {
    if (S[y][x] == -1 || S[y][x] > c) {
      S[y][x] = c;
      q.push(std::make_pair(std::make_pair(y, x), c));
    }
  }
}

int main() {
  cin >> H >> W;
  int sy, sx;
  cin >> sy >> sx;
  int gy, gx;
  cin >> gy >> gx;

  vector<vector<int>> S(H);
  rep(i, H) {
    S[i].resize(W);
    rep(j, W) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        S[i][j] = -2;
      else
        S[i][j] = -1;
    }
  }

  std::queue<std::pair<std::pair<int, int>, int>> q;
  std::queue<std::pair<std::pair<int, int>, int>> q2;

  q.push(std::make_pair(std::make_pair(sy - 1, sx - 1), 0));
  S[sy - 1][sx - 1] = 0;

  int tbl1_y[] = {0, 0, -1, 1};
  int tbl1_x[] = {-1, 1, 0, 0};

  int tbl2_y[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                  0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int tbl2_x[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                  2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (!q.empty() || !q2.empty()) {
    std::pair<std::pair<int, int>, int> e;
    if (!q.empty()) {
      e = q.front();
      q.pop();
    } else {
      e = q2.front();
      q2.pop();
    }

    auto p = e.first;
    int y = p.first;
    int x = p.second;
    int c = e.second;

    rep(i, 4) { move(y + tbl1_y[i], x + tbl1_x[i], c, S, q); }

    rep(i, 20) { move(y + tbl2_y[i], x + tbl2_x[i], c + 1, S, q2); }
  }

  cout << S[gy - 1][gx - 1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int H, W;

void move(int y, int x, int c, vector<vector<int>> &S,
          std::queue<std::pair<std::pair<int, int>, int>> &q) {
  if (x >= 0 && x < W && y >= 0 && y < H) {
    if (S[y][x] == -1 || S[y][x] > c) {
      S[y][x] = c;
      q.push(std::make_pair(std::make_pair(y, x), c));
    }
  }
}

int main() {
  cin >> H >> W;
  int sy, sx;
  cin >> sy >> sx;
  int gy, gx;
  cin >> gy >> gx;

  vector<vector<int>> S(H);
  rep(i, H) {
    S[i].resize(W);
    rep(j, W) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        S[i][j] = -2;
      else
        S[i][j] = -1;
    }
  }

  std::queue<std::pair<std::pair<int, int>, int>> q;

  q.push(std::make_pair(std::make_pair(sy - 1, sx - 1), 0));
  S[sy - 1][sx - 1] = 0;

  int tbl1_y[] = {0, 0, -1, 1};
  int tbl1_x[] = {-1, 1, 0, 0};

  int tbl2_y[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                  0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int tbl2_x[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                  2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (!q.empty()) {
    auto e = q.front();
    q.pop();

    auto p = e.first;
    int y = p.first;
    int x = p.second;
    int c = e.second;

    rep(i, 4) { move(y + tbl1_y[i], x + tbl1_x[i], c, S, q); }

    rep(i, 20) { move(y + tbl2_y[i], x + tbl2_x[i], c + 1, S, q); }
  }

  cout << S[gy - 1][gx - 1] << endl;

  return 0;
}