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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll h, w;
  cin >> h >> w;
  ll cx, cy, dx, dy;
  cin >> cx >> cy;
  cin >> dx >> dy;
  --cx;
  --cy;
  --dx;
  --dy;

  ll nn = 10010001001;

  vector<vector<ll>> m(h, vector<ll>(w, -1));
  string s, s2;
  rep(i, h) {
    cin >> s;
    rep(j, w) {
      s2 = s[j];
      if (s2 == "#") {
        m[i][j] = nn + 1;
      }
    }
  }

  // priority_queue <vector <ll>> q;
  queue<vector<ll>> q, q2, q3, q4;

  ll l;
  vector<ll> qq;

  q2.push({nn, cx, cy});

  m[cx][cy] = nn;
  ll xx, yy, zz;
  int ka;

  while (q2.size() != 0) {
    qq = q2.front();
    q2.pop();
    xx = qq[1];
    yy = qq[2];
    zz = qq[0];
    ka = 0;
    // cout << xx << " " << yy << endl;

    if (m[xx][yy] > zz) {
      continue;
    } else {

      if (xx >= 1) {
        if (m[xx - 1][yy] < m[xx][yy]) {
          m[xx - 1][yy] = m[xx][yy];
          q2.push({zz, xx - 1, yy});
          ++ka;
        }
      }
      if (xx < h - 1) {
        if (m[xx + 1][yy] < m[xx][yy]) {
          m[xx + 1][yy] = m[xx][yy];
          q2.push({zz, xx + 1, yy});
          ++ka;
        }
      }

      if (yy >= 1) {
        if (m[xx][yy - 1] < m[xx][yy]) {
          m[xx][yy - 1] = m[xx][yy];
          q2.push({zz, xx, yy - 1});
          ++ka;
        }
      }
      if (yy < w - 1) {
        if (m[xx][yy + 1] < m[xx][yy]) {
          m[xx][yy + 1] = m[xx][yy];
          q2.push({zz, xx, yy + 1});
          ++ka;
        }
      }

      if (ka != 4) {
        if (xx >= 2 and yy >= 2) {
          if (m[xx - 2][yy - 2] + 1 < m[xx][yy]) {
            m[xx - 2][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy - 2});
          }
        }
        if (xx >= 1 and yy >= 2) {
          if (m[xx - 1][yy - 2] + 1 < m[xx][yy]) {
            m[xx - 1][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy - 2});
          }
        }
        if (yy >= 2) {
          if (m[xx][yy - 2] + 1 < m[xx][yy]) {
            m[xx][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx, yy - 2});
          }
        }

        if (xx < h - 1 and yy >= 2) {
          if (m[xx + 1][yy - 2] + 1 < m[xx][yy]) {
            m[xx + 1][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy - 2});
          }
        }

        if (xx < h - 2 and yy >= 2) {
          if (m[xx + 2][yy - 2] + 1 < m[xx][yy]) {
            m[xx + 2][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy - 2});
          }
        }

        // y1

        if (xx >= 2 and yy >= 1) {
          if (m[xx - 2][yy - 1] + 1 < m[xx][yy]) {
            m[xx - 2][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy - 1});
          }
        }
        if (xx >= 1 and yy >= 1) {
          if (m[xx - 1][yy - 1] + 1 < m[xx][yy]) {
            m[xx - 1][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy - 1});
          }
        }
        /*if(yy>=1){
            if(m[xx][yy-2]+1<m[xx][yy]){
                m[xx][yy-2]=m[xx][yy]+1;
                q.push({zz+1,xx,yy-2});
            }
        }*/

        if (xx < h - 1 and yy >= 1) {
          if (m[xx + 1][yy - 1] + 1 < m[xx][yy]) {
            m[xx + 1][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy - 1});
          }
        }

        if (xx < h - 2 and yy >= 1) {
          if (m[xx + 2][yy - 1] + 1 < m[xx][yy]) {
            m[xx + 2][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy - 1});
          }
        }

        // y2
        if (xx >= 2) {
          if (m[xx - 2][yy] + 1 < m[xx][yy]) {
            m[xx - 2][yy] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy});
          }
        }

        if (xx < h - 2) {
          if (m[xx + 2][yy] + 1 < m[xx][yy]) {
            m[xx + 2][yy] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy});
          }
        }

        // y3
        if (xx >= 2 and yy < w - 1) {
          if (m[xx - 2][yy + 1] + 1 < m[xx][yy]) {
            m[xx - 2][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy + 1});
          }
        }
        if (xx >= 1 and yy < w - 1) {
          if (m[xx - 1][yy + 1] + 1 < m[xx][yy]) {
            m[xx - 1][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy + 1});
          }
        }
        /*if(yy>=1){
            if(m[xx][yy-2]+1<m[xx][yy]){
                m[xx][yy-2]=m[xx][yy]+1;
                q.push({zz+1,xx,yy-2});
            }
        }*/

        if (xx < h - 1 and yy < w - 1) {
          if (m[xx + 1][yy + 1] + 1 < m[xx][yy]) {
            m[xx + 1][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy + 1});
          }
        }

        if (xx < h - 2 and yy < w - 1) {
          if (m[xx + 2][yy + 1] + 1 < m[xx][yy]) {
            m[xx + 2][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy + 1});
          }
        }

        // y4
        if (xx >= 2 and yy < w - 2) {
          if (m[xx - 2][yy + 2] + 1 < m[xx][yy]) {
            m[xx - 2][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy + 2});
          }
        }
        if (xx >= 1 and yy < w - 2) {
          if (m[xx - 1][yy + 2] + 1 < m[xx][yy]) {
            m[xx - 1][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy + 2});
          }
        }
        if (yy < w - 2) {
          if (m[xx][yy + 2] + 1 < m[xx][yy]) {
            m[xx][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx, yy + 2});
          }
        }

        if (xx < h - 1 and yy < w - 2) {
          if (m[xx + 1][yy + 2] + 1 < m[xx][yy]) {
            m[xx + 1][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy + 2});
          }
        }

        if (xx < h - 2 and yy < w - 2) {
          if (m[xx + 2][yy + 2] + 1 < m[xx][yy]) {
            m[xx + 2][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy + 2});
          }
        }
      }

      if (q2.size() == 0) {
        q2 = q;
        q = q3;
      }
    }
  }

  if (m[dx][dy] == -1) {
    cout << -1 << endl;
  } else {
    cout << nn - m[dx][dy] << endl;
  }

  /*rep(i,h){
      rep(j,w){
          cout << m[i][j]<< " ";
      }
      cout << endl;
  }*/

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll h, w;
  cin >> h >> w;
  ll cx, cy, dx, dy;
  cin >> cx >> cy;
  cin >> dx >> dy;
  --cx;
  --cy;
  --dx;
  --dy;

  ll nn = 10010001001;

  vector<vector<ll>> m(h, vector<ll>(w, -1));
  string s, s2;
  rep(i, h) {
    cin >> s;
    rep(j, w) {
      s2 = s[j];
      if (s2 == "#") {
        m[i][j] = nn + 1;
      }
    }
  }

  priority_queue<vector<ll>> q;

  ll l;
  vector<ll> qq;

  q.push({nn, cx, cy});
  m[cx][cy] = nn;
  ll xx, yy, zz;
  int ka;

  while (q.size() != 0) {
    qq = q.top();
    q.pop();
    xx = qq[1];
    yy = qq[2];
    zz = qq[0];
    ka = 0;
    // cout << xx << " " << yy << endl;

    if (m[xx][yy] > zz) {
      continue;
    } else {

      if (xx >= 1) {
        if (m[xx - 1][yy] < m[xx][yy]) {
          m[xx - 1][yy] = m[xx][yy];
          q.push({zz, xx - 1, yy});
          ++ka;
        }
      }
      if (xx < h - 1) {
        if (m[xx + 1][yy] < m[xx][yy]) {
          m[xx + 1][yy] = m[xx][yy];
          q.push({zz, xx + 1, yy});
          ++ka;
        }
      }

      if (yy >= 1) {
        if (m[xx][yy - 1] < m[xx][yy]) {
          m[xx][yy - 1] = m[xx][yy];
          q.push({zz, xx, yy - 1});
          ++ka;
        }
      }
      if (yy < w - 1) {
        if (m[xx][yy + 1] < m[xx][yy]) {
          m[xx][yy + 1] = m[xx][yy];
          q.push({zz, xx, yy + 1});
          ++ka;
        }
      }

      if (ka != 4) {
        if (xx >= 2 and yy >= 2) {
          if (m[xx - 2][yy - 2] + 1 < m[xx][yy]) {
            m[xx - 2][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy - 2});
          }
        }
        if (xx >= 1 and yy >= 2) {
          if (m[xx - 1][yy - 2] + 1 < m[xx][yy]) {
            m[xx - 1][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy - 2});
          }
        }
        if (yy >= 2) {
          if (m[xx][yy - 2] + 1 < m[xx][yy]) {
            m[xx][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx, yy - 2});
          }
        }

        if (xx < h - 1 and yy >= 2) {
          if (m[xx + 1][yy - 2] + 1 < m[xx][yy]) {
            m[xx + 1][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy - 2});
          }
        }

        if (xx < h - 2 and yy >= 2) {
          if (m[xx + 2][yy - 2] + 1 < m[xx][yy]) {
            m[xx + 2][yy - 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy - 2});
          }
        }

        // y1

        if (xx >= 2 and yy >= 1) {
          if (m[xx - 2][yy - 1] + 1 < m[xx][yy]) {
            m[xx - 2][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy - 1});
          }
        }
        if (xx >= 1 and yy >= 1) {
          if (m[xx - 1][yy - 1] + 1 < m[xx][yy]) {
            m[xx - 1][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy - 1});
          }
        }
        /*if(yy>=1){
            if(m[xx][yy-2]+1<m[xx][yy]){
                m[xx][yy-2]=m[xx][yy]+1;
                q.push({zz+1,xx,yy-2});
            }
        }*/

        if (xx < h - 1 and yy >= 1) {
          if (m[xx + 1][yy - 1] + 1 < m[xx][yy]) {
            m[xx + 1][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy - 1});
          }
        }

        if (xx < h - 2 and yy >= 1) {
          if (m[xx + 2][yy - 1] + 1 < m[xx][yy]) {
            m[xx + 2][yy - 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy - 1});
          }
        }

        // y2
        if (xx >= 2) {
          if (m[xx - 2][yy] + 1 < m[xx][yy]) {
            m[xx - 2][yy] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy});
          }
        }

        if (xx < h - 2) {
          if (m[xx + 2][yy] + 1 < m[xx][yy]) {
            m[xx + 2][yy] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy});
          }
        }

        // y3
        if (xx >= 2 and yy < w - 1) {
          if (m[xx - 2][yy + 1] + 1 < m[xx][yy]) {
            m[xx - 2][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy + 1});
          }
        }
        if (xx >= 1 and yy < w - 1) {
          if (m[xx - 1][yy + 1] + 1 < m[xx][yy]) {
            m[xx - 1][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy + 1});
          }
        }
        /*if(yy>=1){
            if(m[xx][yy-2]+1<m[xx][yy]){
                m[xx][yy-2]=m[xx][yy]+1;
                q.push({zz+1,xx,yy-2});
            }
        }*/

        if (xx < h - 1 and yy < w - 1) {
          if (m[xx + 1][yy + 1] + 1 < m[xx][yy]) {
            m[xx + 1][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy + 1});
          }
        }

        if (xx < h - 2 and yy < w - 1) {
          if (m[xx + 2][yy + 1] + 1 < m[xx][yy]) {
            m[xx + 2][yy + 1] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy + 1});
          }
        }

        // y4
        if (xx >= 2 and yy < w - 2) {
          if (m[xx - 2][yy + 2] + 1 < m[xx][yy]) {
            m[xx - 2][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 2, yy + 2});
          }
        }
        if (xx >= 1 and yy < w - 2) {
          if (m[xx - 1][yy + 2] + 1 < m[xx][yy]) {
            m[xx - 1][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx - 1, yy + 2});
          }
        }
        if (yy < w - 2) {
          if (m[xx][yy + 2] + 1 < m[xx][yy]) {
            m[xx][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx, yy + 2});
          }
        }

        if (xx < h - 1 and yy < w - 2) {
          if (m[xx + 1][yy + 2] + 1 < m[xx][yy]) {
            m[xx + 1][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 1, yy + 2});
          }
        }

        if (xx < h - 2 and yy < w - 2) {
          if (m[xx + 2][yy + 2] + 1 < m[xx][yy]) {
            m[xx + 2][yy + 2] = m[xx][yy] - 1;
            q.push({zz + 1, xx + 2, yy + 2});
          }
        }
      }
    }
  }

  if (m[dx][dy] == -1) {
    cout << -1 << endl;
  } else {
    cout << nn - m[dx][dy] << endl;
  }

  /*rep(i,h){
      rep(j,w){
          cout << m[i][j]<< " ";
      }
      cout << endl;
  }*/

  return 0;
}