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

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

static const int HMAX = 1001;
static const int WMAX = 1001;

char S[HMAX][WMAX];
int ans[HMAX][WMAX];

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

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> S[i][j];
      ans[i][j] = -1;
    }
  }

  queue<pair<int, int>> que;
  queue<pair<int, int>> que2;

  que.push(make_pair(Ch, Cw));

  ans[Ch][Cw] = 0;

  while ((!que.empty()) || (!que2.empty())) {

    while (!que.empty()) {
      int i = que.front().first;
      int j = que.front().second;

      que2.push(make_pair(i, j));
      que.pop();

      for (int ii = i - 1; ii <= i + 1; ii = ii + 2) {
        if (ii <= H && ii > 0) {
          if (S[ii][j] == '.') {
            if (ans[ii][j] <= ans[i][j] && ans[ii][j] != -1)
              continue;
            que.push(make_pair(ii, j));
            ans[ii][j] = ans[i][j];
          }
        }
      }
      for (int jj = j - 1; jj <= j + 1; jj = jj + 2) {
        if (jj <= W && jj > 0) {
          if (S[i][jj] == '.') {
            if (ans[i][jj] <= ans[i][j] && ans[i][jj] != -1)
              continue;
            que.push(make_pair(i, jj));
            ans[i][jj] = ans[i][j];
          }
        }
      }
      // cout << "que1: i j ans[i][j] " << i << " " << j << " " << ans[i][j] <<
      // endl;
    }

    while (!que2.empty()) {

      int i = que2.front().first;
      int j = que2.front().second;

      que2.pop();

      for (int ii = i - 2; ii <= i + 2; ii++) {
        for (int jj = j - 2; jj <= j + 2; jj++) {
          if (ii <= H && jj <= W && ii > 0 && jj > 0) {
            if (S[ii][jj] == '.') {
              if (ans[ii][jj] > ans[i][j] + 1 || ans[ii][jj] == -1) {
                ans[ii][jj] = ans[i][j] + 1;
                que.push(make_pair(ii, jj));
              }
            }
          }
        }
      }

      // cout << "que2: i j ans[i][j] " << i << " " << j << " " << ans[i][j] <<
      // endl;
    }
  }

  cout << ans[Dh][Dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

static const int HMAX = 1001;
static const int WMAX = 1001;

char S[HMAX][WMAX];
int ans[HMAX][WMAX];

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

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> S[i][j];
      ans[i][j] = -1;
    }
  }

  queue<pair<int, int>> que;

  que.push(make_pair(Ch, Cw));

  ans[Ch][Cw] = 0;

  while (!que.empty()) {
    int i = que.front().first;
    int j = que.front().second;
    que.pop();

    for (int ii = i - 1; ii <= i + 1; ii = ii + 2) {
      if (ii <= H && ii > 0) {
        if (S[ii][j] == '.') {
          if (ans[ii][j] <= ans[i][j] && ans[ii][j] != -1)
            continue;
          que.push(make_pair(ii, j));
          ans[ii][j] = ans[i][j];
        }
      }
    }
    for (int jj = j - 1; jj <= j + 1; jj = jj + 2) {
      if (jj <= W && jj > 0) {
        if (S[i][jj] == '.') {
          if (ans[i][jj] <= ans[i][j] && ans[i][jj] != -1)
            continue;
          que.push(make_pair(i, jj));
          ans[i][jj] = ans[i][j];
        }
      }
    }

    for (int ii = i - 2; ii <= i + 2; ii++) {
      for (int jj = j - 2; jj <= j + 2; jj++) {
        if (ii <= H && jj <= W && ii > 0 && jj > 0) {
          if (S[ii][jj] == '.') {
            if (ans[ii][jj] > ans[i][j] + 1 || ans[ii][jj] == -1) {
              ans[ii][jj] = ans[i][j] + 1;
              que.push(make_pair(ii, jj));
            }
          }
        }
      }
    }

    // cout << "i j ans[i][j] " << i << " " << j << " " << ans[i][j] << endl;
  }

  cout << ans[Dh][Dw] << endl;

  return 0;
}