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

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

#define bug printf("bug\\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6 + 15;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int mat[1200][1200];
string s[1200];
int mark[1200][1200];
int r, c;
int row[] = {1, 0, -1, 0, 1, -1, 1, -1};
int colm[] = {0, 1, 0, -1, 1, -1, -1, 1};
int cur;
vector<int> graph[MAX];
void bfs(int input_x, int input_y) {
  queue<pair<int, int>> q;
  mark[input_x][input_y] = 1;
  mat[input_x][input_y] = cur;
  q.push(make_pair(input_x, input_y));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + row[i];
      int v = y + colm[i];
      if (0 <= u && u < r && 0 <= v && v < c) {
        if (mark[u][v] == 0 && s[u][v] == '.') {
          mark[u][v] = 1;
          q.push(make_pair(u, v));
          mat[u][v] = cur;
        }
      }
    }
  }
  return;
}
int V[MAX], D[MAX];
void bfs2(int src) {
  V[src] = 1;
  queue<int> q;
  q.push(src);
  while (!q.empty()) {
    int parent = q.front();
    q.pop();
    for (int i = 0; i < graph[parent].size(); i++) {
      int child = graph[parent][i];
      if (V[child] == 0) {
        V[child] = 1;
        D[child] = D[parent] + 1;
        q.push(child);
      }
    }
  }
}
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      int a,
      b, p, q;
  cin >> r >> c;
  cin >> a >> b >> p >> q;
  for (int i = 0; i < r; i++)
    cin >> s[i];
  a--;
  b--;
  p--;
  q--;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mark[i][j] == 0 && s[i][j] == '.') {
        // cout<<i<<" "<<j<<endl;
        cur++;
        bfs(i, j);
      }
    }
  }
  map<pair<int, int>, int> mp;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (s[i][j] == '.') {
        for (int x = i - 2; x <= i + 2; x++) {
          for (int y = j - 2; y <= j + 2; y++) {
            if (0 <= x && x < r && 0 <= y && y < c) {
              int u = mat[i][j];
              int v = mat[x][y];
              if (u != v && mp[{u, v}] == 0 && u && v) {
                graph[u].push_back(v);
                graph[v].push_back(u);
                mp[{u, v}] = 1;
                mp[{v, u}] = 1;
              }
            }
          }
        }
      }
    }
  }
  int root = mat[a][b];
  bfs2(root);
  if (V[mat[p][q]] == 0)
    cout << "-1" << endl;
  else
    cout << D[mat[p][q]] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int fx[] = {-1, +1, 0, 0};
int fy[] = {0, 0, +1, -1};
/*int fix[] = {-1,-1,+1,+1,-2,-2,+2,+2,-2,+2,0,0,-2,-2,+2,+2,-1,+1,-1,+1  };
int fiy[] = {+1,-1,+1,-1,+2,-2,+2,-2,0,0,-2,+2,-1,+1,-1,+1,+2,+2,-2,-2   };
*/

int fix[] = {-1, -1, +1, +1, +2, -2, +2, +2, -2, -2, +2, +2, -2, -2};
int fiy[] = {+1, -1, +1, -1, 0, 0, +1, -1, +1, -1, +2, -2, +2, -2};

int dist[1100][1100];
int main() {
  ll n, m;
  cin >> n >> m;
  ll srx, sry, dsx, dsy;
  cin >> srx >> sry;
  cin >> dsx >> dsy;
  string ara[n + 10];
  for (int i = 1; i <= n; i++) {
    cin >> ara[i];

    for (int j = 1; j <= m; j++) {
      dist[i][j] = INT_MAX;
    }
  }
  /*for(int i=1; i<=n; i++)
  {
      for(int j=1; j<=m; j++)
        {dist[i][j] = INT_MAX;
           //cout << ara[i][j-1] << " " ;

        }
      //  cout << endl;
  }*/

  queue<pair<int, int>> q;
  q.push({srx, sry});
  dist[srx][sry] = 0;

  while (!q.empty()) {
    pair<int, int> pr = q.front();
    q.pop();
    bool mrk = 0;
    for (int i = 0; i < 4; i++) {
      int cx = pr.first + fx[i];
      int cy = pr.second + fy[i];
      if (cx >= 1 && cx <= n && cy >= 1 && cy <= m && ara[cx][cy - 1] == '.' &&
          dist[cx][cy] > dist[pr.first][pr.second]) {
        q.push({cx, cy});
        dist[cx][cy] = dist[pr.first][pr.second];
        // mrk = 1;
      }
    }
    for (int i = 0; i < 14; i++) {
      int cx = pr.first + fix[i];
      int cy = pr.second + fiy[i];
      int dx = pr.first + fiy[i];
      int dy = pr.second + fix[i];

      if (cx >= 1 && cx <= n && cy >= 1 && cy <= m && ara[cx][cy - 1] == '.' &&
          dist[cx][cy] > dist[pr.first][pr.second] + 1) {
        q.push({cx, cy});
        dist[cx][cy] = dist[pr.first][pr.second] + 1;
        // mrk = 1;
      }

      if (dx >= 1 && dx <= n && dy >= 1 && dy <= m && ara[dx][dy - 1] == '.' &&
          dist[dx][dy] > dist[pr.first][pr.second] + 1) {
        q.push({dx, dy});
        dist[dx][dy] = dist[pr.first][pr.second] + 1;
        // mrk = 1;
      }
    }

    // if(!mrk)break;
  }

  if (dist[dsx][dsy] != INT_MAX)
    cout << dist[dsx][dsy] << endl;
  else
    cout << -1 << endl;
}