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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
string s[1010];
int b1[4] = {0, 0, 1, -1};
int b2[4] = {1, -1, 0, 0};
bool b[1010][1010] = {false};
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  b[ch][cw] = true;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  queue<tuple<int, int, int>> que, que1;
  que.push(make_tuple(ch, cw, 0));
  while (!que.empty() || !que1.empty()) {
    if (!que.empty()) {
      tuple<int, int, int> now = que.front();
      int x = get<0>(now);
      int y = get<1>(now);
      int z = get<2>(now);
      que1.push(make_tuple(x, y, z));
      if (x == dh && y == dw) {
        cout << z << endl;
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        if (x + b1[i] < 0 || x + b1[i] >= h || y + b2[i] < 0 ||
            y + b2[i] >= w) {
          continue;
        }
        if (s[x + b1[i]][y + b2[i]] == '.') {
          if (!b[x + b1[i]][y + b2[i]]) {
            b[x + b1[i]][y + b2[i]] = true;
            que.push(make_tuple(x + b1[i], y + b2[i], z));
          }
        }
      }
      que.pop();
    } else {
      while (!que1.empty()) {
        tuple<int, int, int> now = que1.front();
        int x = get<0>(now);
        int y = get<1>(now);
        int z = get<2>(now);
        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            if (x + i < 0 || x + i >= h || y + j < 0 || y + j >= w) {
              continue;
            }
            if (s[x + i][y + j] == '.') {
              if (!b[x + i][y + j]) {
                b[x + i][y + j] = true;
                que.push(make_tuple(x + i, y + j, z + 1));
              }
            }
          }
        }
        que1.pop();
      }
    }
  }
  cout << "-1" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> a(1010);
bool b[1010][1010];
string s[1010];
int b1[4] = {0, 0, 1, -1};
int b2[4] = {1, -1, 0, 0};
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  bool ash = true;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        ash = false;
      }
    }
  }
  if (ash) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      a[i].emplace_back(1000000007);
    }
  }
  queue<pair<int, int>> que;
  que.push(make_pair(ch, cw));
  a[ch][cw] = 0;
  while (!que.empty()) {
    pair<int, int> now = que.front();
    int xn = now.first;
    int yn = now.second;
    bool bo = false;
    for (int i = 0; i < 4; i++) {
      if (xn + b1[i] < 0 || xn + b1[i] >= h || yn + b2[i] < 0 ||
          yn + b2[i] >= w) {
        continue;
      }
      if (s[xn + b1[i]][yn + b2[i]] == '.') {
        if (a[xn + b1[i]][yn + b2[i]] > a[xn][yn]) {
          bo = true;
          que.push(make_pair(xn + b1[i], yn + b2[i]));
          a[xn + b1[i]][yn + b2[i]] = a[xn][yn];
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (xn + i < 0 || xn + i >= h || yn + j < 0 || yn + j >= w) {
          continue;
        }
        if (s[xn + i][yn + j] == '.') {
          if (a[xn + i][yn + j] > a[xn][yn] + 1) {
            que.push(make_pair(xn + i, yn + j));
            a[xn + i][yn + j] = a[xn][yn] + 1;
          }
        }
      }
    }
    que.pop();
  }
  if (a[dh][dw] == 1000000007) {
    cout << "-1" << endl;
  } else {
    cout << a[dh][dw] << endl;
  }
  return 0;
}