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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> i_i;

int main() {

  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  int nowcost;
  int dh_A[4] = {0, -1, 0, 1};
  int dw_A[4] = {1, 0, -1, 0};
  int dh_B[20] = {2, 2,  2,  2,  2,  1,  1,  1,  1,  0,
                  0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
  int dw_B[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                  2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
  deque<i_i> deq;
  vector<vector<int>> cost(H, vector<int>(W, -1));
  cost[Ch - 1][Cw - 1] = 0;
  deq.push_back(make_pair(Ch - 1, Cw - 1));
  while (!deq.empty()) {
    int h, w;
    i_i now = deq.front();
    deq.pop_front();
    nowcost = cost[now.first][now.second];
    for (int i = 0; i < 4; i++) { // move A
      h = now.first + dh_A[i];
      w = now.second + dw_A[i];
      if (h >= 0 && h < H && w >= 0 && w < W) {
        if (S[h][w] == '.' && (cost[h][w] < 0 || cost[h][w] > nowcost)) {
          deq.push_front(make_pair(h, w));
          cost[h][w] = nowcost;
        }
      }
    }
    for (int i = 0; i < 20; i++) { // move B
      h = now.first + dh_B[i];
      w = now.second + dw_B[i];
      if (h >= 0 && h < H && w >= 0 && w < W) {
        if (S[h][w] == '.' && (cost[h][w] < 0 || cost[h][w] > nowcost + 1)) {
          deq.push_back(make_pair(h, w));
          cost[h][w] = nowcost + 1;
        }
      }
    }
  }

  cout << cost[Dh - 1][Dw - 1];
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];

  vector<vector<int>> cost(H, vector<int>(W, -1));
  int nowcost = 0;
  queue<pair<int, int>> que;
  vector<pair<int, int>> listA;
  listA.push_back(pair<int, int>(Ch - 1, Cw - 1));

  while (!listA.empty()) {

    // listB.clear();
    for (auto a : listA) { // move A
      que.push(a);
      while (!que.empty()) {
        int h = que.front().first;
        int w = que.front().second;
        cost[h][w] = nowcost;
        // listB.push_back(pair<int,int>(h,w));
        que.pop();
        if (h > 0) {
          if (S[h - 1][w] == '.' && cost[h - 1][w] < 0) {
            que.push(pair<int, int>(h - 1, w));
          }
        }
        if (h < H - 1) {
          if (S[h + 1][w] == '.' && cost[h + 1][w] < 0) {
            que.push(pair<int, int>(h + 1, w));
          }
        }
        if (w > 0) {
          if (S[h][w - 1] == '.' && cost[h][w - 1] < 0) {
            que.push(pair<int, int>(h, w - 1));
          }
        }
        if (w < W - 1) {
          if (S[h][w + 1] == '.' && cost[h][w + 1] < 0) {
            que.push(pair<int, int>(h, w + 1));
          }
        }
      }
    }

    listA.clear();
    nowcost++;
    int th[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                  0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
    int tw[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                  2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (cost[i][j] == nowcost - 1) {
          for (int k = 0; k < 20; k++) {
            int h = i + th[k];
            int w = j + tw[k];
            if (h >= 0 && h < H && w >= 0 && w < W) {
              if (S[h][w] == '.' && cost[h][w] < 0) {
                cost[h][w] = nowcost;
                listA.push_back(pair<int, int>(h, w));
              }
            }
          }
        }
      }
    }
    /*for(auto b : listB){ //move B
        for(int i=0;i<20;i++){
            int h = b.first + th[i];
            int w = b.second + tw[i];
            if(h>=0 && h<H && w>=0 && w<W){
                if(S[h][w] == '.' && cost[h][w] < 0){
                    cost[h][w] = nowcost;
                    listA.push_back(pair<int,int>(h,w));
                }
            }
        }
    }*/
    /*for(int i= -2;i<=2;i++){
            for(int j= -2;j<=2;j++){
                int h = b.first + i;
                int w = b.second + j;
                if(h>=0 && h<H && w>=0 && w<W){
                    if(S[h][w] == '.' && cost[h][w] < 0){
                        S[h][w] = nowcost;
                        listA.push_back(pair<int,int>(h,w));
                    }
                }
            }
        }
    }*/
  }

  /*for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
          cout << cost[i][j] << " ";
      }
      cout << endl;
  }*/
  cout << cost[Dh - 1][Dw - 1];
  return 0;
}