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

#include <bits/stdc++.h>
using namespace std;

#define rep(ver, n) rep2(ver, 0, n)
#define rep2(ver, m, n) for (int ver = m; ver < (n); ver++)
#define loop while (true)
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(v) (v).begin(), (v).end()
#define debug(x) std::cerr << #x << ": " << x << "\\n"
#define debug2(x, y)                                                           \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\\n"
#define debug3(x, y, z)                                                        \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z        \\
            << ": " << z << "\\n"

typedef long long ll;
typedef pair<int, int> P;

int h, w;
int ch, cw, dh, dw;
char m[2000][2000] = {};
int go[2000][2000] = {};
const int NOT_REACH = 1 << 30;

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

int main() {
  scanf("%d %d\\n%d %d\\n%d %d", &h, &w, &ch, &cw, &dh, &dw);
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) {
    scanf("%s", m[i]);
    rep(j, w) { go[i][j] = NOT_REACH; }
  }

  queue<tuple<int, int, int>> q[2];
  q[0].push(make_tuple(ch, cw, 0));
  go[ch][cw] = 0;
  while (!q[0].empty()) {
    while (!q[0].empty()) {
      auto now = q[0].front();
      q[0].pop();
      int nh = get<0>(now), nw = get<1>(now), c = get<2>(now);
      rep(i, 5) {
        rep(j, 5) {
          if (sets[i] == 0 && sets[j] == 0)
            continue;
          int nxh = nh + sets[i], nxw = nw + sets[j];
          nxh = max(0, nxh);
          nxw = max(0, nxw);
          nxh = min(h - 1, nxh);
          nxw = min(w - 1, nxw);
          if (m[nxh][nxw] == '#')
            continue;
          if (abs(sets[i]) + abs(sets[j]) == 1) {
            if (go[nxh][nxw] <= c)
              continue;
            go[nxh][nxw] = c;
            q[0].push(make_tuple(nxh, nxw, c));
          } else {
            if (go[nxh][nxw] <= c + 1)
              continue;
            go[nxh][nxw] = c + 1;
            q[1].push(make_tuple(nxh, nxw, c));
          }
        }
      }
    }
    while (!q[1].empty()) {
      auto now = q[1].front();
      q[1].pop();
      int nh = get<0>(now), nw = get<1>(now), c = get<2>(now);
      if (go[nh][nw] <= c) {
      } else {
        q[0].push(make_tuple(nh, nw, c + 1));
      }
    }
  }

  if (go[dh][dw] == NOT_REACH) {
    cout << -1;
  } else {
    cout << go[dh][dw];
  }

  /*
  rep(i,h){
    rep(j,w){
      cerr<<go[i][j]<<", ";
    }
    cerr<<endl;
  }
  */

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(ver, n) rep2(ver, 0, n)
#define rep2(ver, m, n) for (int ver = m; ver < (n); ver++)
#define loop while (true)
#define dup(x, y) (((x) + (y)-1) / (y))
#define all(v) (v).begin(), (v).end()
#define debug(x) std::cerr << #x << ": " << x << "\\n"
#define debug2(x, y)                                                           \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\\n"
#define debug3(x, y, z)                                                        \\
  std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z        \\
            << ": " << z << "\\n"

typedef long long ll;
typedef pair<int, int> P;

int h, w;
int ch, cw, dh, dw;
char m[2000][2000] = {};
int go[2000][2000] = {};
const int NOT_REACH = 1 << 30;

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

int main() {
  scanf("%d %d\\n%d %d\\n%d %d", &h, &w, &ch, &cw, &dh, &dw);
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) {
    scanf("%s", m[i]);
    rep(j, w) { go[i][j] = NOT_REACH; }
  }

  queue<tuple<int, int, int>> q;
  q.push(make_tuple(ch, cw, 0));
  go[ch][cw] = 0;

  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    int nh = get<0>(now), nw = get<1>(now), c = get<2>(now);
    rep(i, 5) {
      rep(j, 5) {
        if (sets[i] == 0 && sets[j] == 0)
          continue;
        int nxh = nh + sets[i], nxw = nw + sets[j];
        nxh = max(0, nxh);
        nxw = max(0, nxw);
        nxh = min(h - 1, nxh);
        nxw = min(w - 1, nxw);
        if (m[nxh][nxw] == '#')
          continue;
        if (abs(sets[i]) + abs(sets[j]) == 1) {
          if (go[nxh][nxw] <= c)
            continue;
          go[nxh][nxw] = c;
          q.push(make_tuple(nxh, nxw, c));
        } else {
          if (go[nxh][nxw] <= c + 1)
            continue;
          go[nxh][nxw] = c + 1;
          q.push(make_tuple(nxh, nxw, c + 1));
        }
      }
    }
  }

  if (go[dh][dw] == NOT_REACH) {
    cout << -1;
  } else {
    cout << go[dh][dw];
  }

  /*
  rep(i,h){
    rep(j,w){
      cerr<<go[i][j]<<", ";
    }
    cerr<<endl;
  }
  */

  return 0;
}