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

#include <queue>
#include <stdio.h>
using namespace std;
int A[1000][1000];
bool Point_isAdded[1000][1000], Tele_isAdded[1000][1000];
int main() {
  short int n, m, x1, y1, x2, y2;
  bool Grid[1000][1000];
  scanf("%hi%hi%hi%hi%hi%hi", &n, &m, &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  {
    char c;
    for (short int i = 0; i < n; i++) {
      scanf("%*c");
      for (short int j = 0; j < m; j++) {
        scanf("%c", &c);
        Grid[i][j] = (c == '#');
      }
    }
  }

  queue<pair<short int, short int>> Walk, Teleport;
  Walk.push(make_pair(x1, y1));
  Point_isAdded[x1][y1] = true;
  int magic_cnt = 1;
  while (!Walk.empty() || !Teleport.empty()) {
    if (!Walk.empty()) {
      /// We could walk
      short int x = Walk.front().first, y = Walk.front().second;
      Walk.pop();
      A[x][y] = magic_cnt;

      if (x - 1 >= 0 && !Grid[x - 1][y] && !A[x - 1][y] &&
          !Point_isAdded[x - 1][y]) {
        Walk.push(make_pair(x - 1, y));
        Point_isAdded[x - 1][y] = true;
      }
      if (x + 1 < n && !Grid[x + 1][y] && !A[x + 1][y] &&
          !Point_isAdded[x + 1][y]) {
        Walk.push(make_pair(x + 1, y));
        Point_isAdded[x + 1][y] = true;
      }
      if (y - 1 >= 0 && !Grid[x][y - 1] && !A[x][y - 1] &&
          !Point_isAdded[x][y - 1]) {
        Walk.push(make_pair(x, y - 1));
        Point_isAdded[x][y - 1] = true;
      }
      if (y + 1 < m && !Grid[x][y + 1] && !A[x][y + 1] &&
          !Point_isAdded[x][y + 1]) {
        Walk.push(make_pair(x, y + 1));
        Point_isAdded[x][y + 1] = true;
      }

      /// Check for 2-step points to add in Teleport
      for (short int del_x = -2; del_x <= 2; del_x++) {
        short int del_y = -2;
        if (y + del_y >= 0) {
          if (0 <= x + del_x && x + del_x < n && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }

        del_y = 2;
        if (y + del_y < m) {
          if (0 <= x + del_x && x + del_x < n && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }
      }

      for (short int del_y = -1; del_y <= 1; del_y++) {
        short int del_x = -2;
        if (x + del_x >= 0) {
          if (0 <= y + del_y && y + del_y < m && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }

        del_x = 2;
        if (x + del_x < n) {
          if (0 <= y + del_y && y + del_y < m && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }
      }

      short int del_x = -1, del_y = -1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = -1, del_y = 1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = 1, del_y = 1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = 1, del_y = -1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
    } else {
      /// Could teleport only

      // Check all the Teleports in the queue (add to the Walk)
      while (!Teleport.empty()) {
        short int x = Teleport.front().first, y = Teleport.front().second;
        if (!A[x][y])
          Walk.push(make_pair(x, y));
        Teleport.pop();
      }

      // Update magic_cnt
      magic_cnt++;
    }
  }
  printf("%i", A[x2][y2] - 1);
}


Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>
using namespace std;
int A[1000][1000];
bool Tele_isAdded[1000][1000];
int main() {
  short int n, m, x1, y1, x2, y2;
  bool Grid[1000][1000];
  scanf("%hi%hi%hi%hi%hi%hi", &n, &m, &x1, &y1, &x2, &y2);
  x1--, y1--, x2--, y2--;
  {
    char c;
    for (short int i = 0; i < n; i++) {
      scanf("%*c");
      for (short int j = 0; j < m; j++) {
        scanf("%c", &c);
        Grid[i][j] = (c == '#');
      }
    }
  }

  queue<pair<short int, short int>> Walk, Teleport;
  Walk.push(make_pair(x1, y1));
  int magic_cnt = 1;
  while (!Walk.empty() || !Teleport.empty()) {
    if (!Walk.empty()) {
      /// We could walk
      short int x = Walk.front().first, y = Walk.front().second;
      Walk.pop();
      A[x][y] = magic_cnt;

      if (x - 1 >= 0 && !Grid[x - 1][y] && !A[x - 1][y]) {
        Walk.push(make_pair(x - 1, y));
      }
      if (x + 1 < n && !Grid[x + 1][y] && !A[x + 1][y]) {
        Walk.push(make_pair(x + 1, y));
      }
      if (y - 1 >= 0 && !Grid[x][y - 1] && !A[x][y - 1]) {
        Walk.push(make_pair(x, y - 1));
      }
      if (y + 1 < m && !Grid[x][y + 1] && !A[x][y + 1]) {
        Walk.push(make_pair(x, y + 1));
      }

      /// Check for 2-step points to add in Teleport
      for (short int del_x = -2; del_x <= 2; del_x++) {
        short int del_y = -2;
        if (y + del_y >= 0) {
          if (0 <= x + del_x && x + del_x < n && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }

        del_y = 2;
        if (y + del_y < m) {
          if (0 <= x + del_x && x + del_x < n && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }
      }

      for (short int del_y = -1; del_y <= 1; del_y++) {
        short int del_x = -2;
        if (x + del_x >= 0) {
          if (0 <= y + del_y && y + del_y < m && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }

        del_x = 2;
        if (x + del_x < n) {
          if (0 <= y + del_y && y + del_y < m && !Grid[x + del_x][y + del_y] &&
              !A[x + del_x][y + del_y] && !Tele_isAdded[x + del_x][y + del_y]) {
            Teleport.push(make_pair(x + del_x, y + del_y));
            Tele_isAdded[x + del_x][y + del_y] = true;
          }
        }
      }

      short int del_x = -1, del_y = -1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = -1, del_y = 1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = 1, del_y = 1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
      del_x = 1, del_y = -1;
      if (0 <= x + del_x && x + del_x < n && 0 <= y + del_y && y + del_y < m &&
          !Grid[x + del_x][y + del_y] && !A[x + del_x][y + del_y] &&
          !Tele_isAdded[x + del_x][y + del_y]) {
        Teleport.push(make_pair(x + del_x, y + del_y));
        Tele_isAdded[x + del_x][y + del_y] = true;
      }
    } else {
      /// Could teleport only

      // Check all the Teleports in the queue (add to the Walk)
      while (!Teleport.empty()) {
        short int x = Teleport.front().first, y = Teleport.front().second;
        if (!A[x][y])
          Walk.push(make_pair(x, y));
        Teleport.pop();
      }

      // Update magic_cnt
      magic_cnt++;
    }
  }
  printf("%i", A[x2][y2] - 1);
}