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

#include "deque"
#include "iostream"
#include "string"
#include "vector"
#include <stdio.h>

using namespace std;

int main() {
  int H, W;
  const int INF = 1e9 + 1;
  vector<int> C(2), D(2);
  deque<int> que_y;
  deque<int> que_x;

  cin >> H >> W;
  cin >> C[0] >> C[1];
  cin >> D[0] >> D[1];

  vector<string> S(H);
  vector<vector<int>> maze_visited(H, vector<int>(W, INF));

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  que_y.push_front(C[0] - 1);
  que_x.push_front(C[1] - 1);
  maze_visited[C[0] - 1][C[1] - 1] = 0;

  while (!que_y.empty()) {
    int y = que_y.front();
    int x = que_x.front();
    que_y.pop_front();
    que_x.pop_front();
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i != 0 || j != 0) {
          int n_x = x + i;
          int n_y = y + j;
          int mv_n = maze_visited[n_y][n_x];
          int mv_c = maze_visited[y][x];
          if (n_x >= 0 && n_y >= 0 && n_x < W && n_y < H) {
            if (S[n_y][n_x] == '.') {
              if (abs(i) + abs(j) <= 1) {
                if (mv_n > mv_c) {
                  maze_visited[n_y][n_x] = mv_c;
                  que_y.push_front(n_y);
                  que_x.push_front(n_x);
                }
              } else {
                if (mv_n > mv_c + 1) {
                  maze_visited[n_y][n_x] = mv_c + 1;
                  que_y.push_back(n_y);
                  que_x.push_back(n_x);
                }
              }
            }
          }
        }
      }
    }
  }

  if (maze_visited[D[0] - 1][D[1] - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << maze_visited[D[0] - 1][D[1] - 1] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "iostream"
#include "queue"
#include "string"
#include "vector"
#include <stdio.h>

using namespace std;

int main() {
  int H, W;
  const int INF = 1e9 + 1;
  vector<int> C(2), D(2);
  queue<int> que_y;
  queue<int> que_x;

  cin >> H >> W;
  cin >> C[0] >> C[1];
  cin >> D[0] >> D[1];

  vector<string> S(H);
  vector<vector<int>> maze_visited(H, vector<int>(W, INF));

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  que_y.push(C[0] - 1);
  que_x.push(C[1] - 1);
  maze_visited[C[0] - 1][C[1] - 1] = 0;

  while (!que_y.empty()) {
    int y = que_y.front();
    int x = que_x.front();
    que_y.pop();
    que_x.pop();
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i != 0 || j != 0) {
          int n_x = x + i;
          int n_y = y + j;
          int mv_n = maze_visited[n_y][n_x];
          int mv_c = maze_visited[y][x];
          if (n_x >= 0 && n_y >= 0 && n_x < W && n_y < H) {
            if (S[n_y][n_x] == '.') {
              if (abs(i) + abs(j) <= 1) {
                if (mv_n > mv_c) {
                  maze_visited[n_y][n_x] = mv_c;
                  que_y.push(n_y);
                  que_x.push(n_x);
                }
              } else {
                if (mv_n > mv_c + 1) {
                  maze_visited[n_y][n_x] = mv_c + 1;
                  que_y.push(n_y);
                  que_x.push(n_x);
                }
              }
            }
          }
        }
      }
    }
  }

  if (maze_visited[D[0] - 1][D[1] - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << maze_visited[D[0] - 1][D[1] - 1] << endl;
  }

  return 0;
}