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

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
lg n, m;
lg maxnow = -1;
void make(vector<vector<bool>> &visited, vector<vector<lg>> &nodes,
          vector<string> &gr, lg di, lg dj, lg now) {
  // cout<<now<<endl;
  vector<vector<lg>> next = nodes;
  while (!nodes.empty()) {
    vector<lg> cord(2, 0);
    vector<vector<lg>> temp;
    lg l = nodes.size();
    for (lg i = 0; i < l; i++) {
      lg jn = nodes[i][1];
      lg in = nodes[i][0];
      if (in > 0 && !visited[in - 1][jn] && gr[in - 1][jn] == 46) {
        if (in - 1 == di && jn == dj) {
          maxnow = now;
          return;
        }
        cord[0] = in - 1;
        cord[1] = jn;
        // cout<<cord[0]<<" "<<cord[1]<<endl;
        temp.pb(cord);
        visited[in - 1][jn] = true;
      }
      if (in < n - 1 && !visited[in + 1][jn] && gr[in + 1][jn] == 46) {
        if (in + 1 == di && jn == dj) {
          maxnow = now;
          return;
        }
        cord[0] = in + 1;
        cord[1] = jn;
        // cout<<cord[0]<<" "<<cord[1]<<endl;
        temp.pb(cord);
        visited[in + 1][jn] = true;
      }
      if (jn > 0 && !visited[in][jn - 1] && gr[in][jn - 1] == 46) {
        if (in == di && jn - 1 == dj) {
          maxnow = now;
          return;
        }
        cord[0] = in;
        cord[1] = jn - 1;
        // cout<<cord[0]<<" "<<cord[1]<<endl;
        temp.pb(cord);
        visited[in][jn - 1] = true;
      }
      if (jn < m - 1 && !visited[in][jn + 1] && gr[in][jn + 1] == 46) {
        if (in == di && jn + 1 == dj) {
          maxnow = now;
          return;
        }
        cord[0] = in;
        cord[1] = jn + 1;
        // cout<<cord[0]<<" "<<cord[1]<<endl;
        temp.pb(cord);
        visited[in][jn + 1] = true;
      }
    }
    nodes = temp;
    next.insert(next.end(), temp.begin(), temp.end());
  }

  lg l = next.size();
  // cout<<l<<endl;
  nodes.clear();
  for (lg k = 0; k < l; k++) {
    vector<lg> cord(2, 0);
    lg jn = next[k][1];
    lg in = next[k][0];
    for (lg i = max(in - 2, 0); i <= min(in + 2, n - 1); i++) {
      for (lg j = max(jn - 2, 0); j <= min(jn + 2, m - 1); j++) {
        if (!visited[i][j] && gr[i][j] == 46) {
          if (i == di && j == dj) {
            maxnow = now + 1;
            return;
          }
          visited[i][j] = true;
          cord[0] = i;
          cord[1] = j;
          nodes.pb(cord);
        }
      }
    }
  }
  if (!nodes.empty())
    make(visited, nodes, gr, di, dj, now + 1);
}
/*
void sol(vector<vector<bool>>& visited,vector<vector<bool>>& track,
vector<string>& gr, lg in,lg jn,lg di,lg dj,lg now)
{
    if(track[in][jn])
    {
        maxnow=min(maxnow,now);
    }
    else if(now<maxnow)
    {
        bool p=true;

        if(in>0 && !visited[in-1][jn] && gr[in-1][jn]==46)
        {
            visited[in-1][jn]=true;
            sol(visited,track,gr,in-1,jn,di,dj,now);
            visited[in-1][jn]=false;
            p=false;
        }
        if(in<n-1 && !visited[in+1][jn] && gr[in+1][jn]==46)
        {
            visited[in+1][jn]=true;
            sol(visited,track,gr,in+1,jn,di,dj,now);
            visited[in+1][jn]=false;
            p=false;
        }
        if(jn>0 && !visited[in][jn-1] && gr[in][jn-1]==46)
        {
            visited[in][jn-1]=true;
            sol(visited,track,gr,in,jn-1,di,dj,now);
            visited[in][jn-1]=false;
            p=false;
        }
        if(jn<m-1 && !visited[in][jn+1] && gr[in][jn+1]==46)
        {
            visited[in][jn+1]=true;
            sol(visited,track,gr,in,jn+1,di,dj,now);
            visited[in][jn+1]=false;
            p=false;
        }

        if(p)
        {
            for(lg i=max(in-2,0);i<=min(in+2,n-1);i++)
            {
               for(lg j=max(jn-2,0);j<=min(jn+2,m-1);j++)
                {
                    if(!visited[i][j] && gr[i][j]==46)
                    {
                        visited[i][j]=true;
                        sol(visited,track,gr,i,j,di,dj,now+1);
                        visited[i][j]=false;
                    }
                }
            }
        }
    }
}
*/
int main() {

  cin >> n >> m;
  lg cx, cy, dx, dy;
  cin >> cy >> cx >> dy >> dx;
  vector<string> a(n, ".");
  for (lg i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<bool>> visited(n, vector<bool>(m, false));
  visited[cy - 1][cx - 1] = true;

  vector<vector<lg>> nodes(1, vector<lg>(2, cx - 1));
  nodes[0][0] = cy - 1;

  make(visited, nodes, a, dy - 1, dx - 1, 0);

  cout << maxnow << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
lg n, m;
lg maxnow = INT_MAX;
void make(vector<vector<bool>> &visited, vector<string> &gr, lg ii, lg jj) {
  vector<vector<lg>> nodes(1, vector<lg>(2, ii));
  nodes[0][1] = jj;
  visited[ii][jj] = true;
  while (!nodes.empty()) {
    vector<lg> cord(2, 0);
    vector<vector<lg>> temp;
    lg l = nodes.size();
    for (lg i = 0; i < l; i++) {
      lg jn = nodes[i][1];
      lg in = nodes[i][0];
      if (in > 0 && !visited[in - 1][jn] && gr[in - 1][jn] == 46) {
        cord[0] = in - 1;
        cord[1] = jn;
        temp.pb(cord);
        visited[in - 1][jn] = true;
      }
      if (in < n - 1 && !visited[in + 1][jn] && gr[in + 1][jn] == 46) {
        cord[0] = in + 1;
        cord[1] = jn;
        temp.pb(cord);
        visited[in + 1][jn] = true;
      }
      if (jn > 0 && !visited[in][jn - 1] && gr[in][jn - 1] == 46) {
        cord[0] = in;
        cord[1] = jn - 1;
        temp.pb(cord);
        visited[in][jn - 1] = true;
      }
      if (jn < m - 1 && !visited[in][jn + 1] && gr[in][jn + 1] == 46) {
        cord[0] = in;
        cord[1] = jn + 1;
        temp.pb(cord);
        visited[in][jn + 1] = true;
      }
    }
    nodes = temp;
  }
}
void sol(vector<vector<bool>> &visited, vector<vector<bool>> &track,
         vector<string> &gr, lg in, lg jn, lg di, lg dj, lg now) {
  if (track[in][jn]) {
    maxnow = min(maxnow, now);
  } else if (now < maxnow) {
    bool p = true;

    if (in > 0 && !visited[in - 1][jn] && gr[in - 1][jn] == 46) {
      visited[in - 1][jn] = true;
      sol(visited, track, gr, in - 1, jn, di, dj, now);
      visited[in - 1][jn] = false;
      p = false;
    }
    if (in < n - 1 && !visited[in + 1][jn] && gr[in + 1][jn] == 46) {
      visited[in + 1][jn] = true;
      sol(visited, track, gr, in + 1, jn, di, dj, now);
      visited[in + 1][jn] = false;
      p = false;
    }
    if (jn > 0 && !visited[in][jn - 1] && gr[in][jn - 1] == 46) {
      visited[in][jn - 1] = true;
      sol(visited, track, gr, in, jn - 1, di, dj, now);
      visited[in][jn - 1] = false;
      p = false;
    }
    if (jn < m - 1 && !visited[in][jn + 1] && gr[in][jn + 1] == 46) {
      visited[in][jn + 1] = true;
      sol(visited, track, gr, in, jn + 1, di, dj, now);
      visited[in][jn + 1] = false;
      p = false;
    }

    if (p) {
      for (lg i = max(in - 2, 0); i <= min(in + 2, n - 1); i++) {
        for (lg j = max(jn - 2, 0); j <= min(jn + 2, m - 1); j++) {
          if (!visited[i][j] && gr[i][j] == 46) {
            visited[i][j] = true;
            sol(visited, track, gr, i, j, di, dj, now + 1);
            visited[i][j] = false;
          }
        }
      }
    }
  }
}

int main() {

  cin >> n >> m;
  lg cx, cy, dx, dy;
  cin >> cy >> cx >> dy >> dx;
  vector<string> a(n, ".");
  for (lg i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<bool>> visited(n, vector<bool>(m, false));
  vector<vector<bool>> track(n, vector<bool>(m, false));
  make(track, a, dy - 1, dx - 1);
  visited[cy - 1][cx - 1] = true;
  sol(visited, track, a, cy - 1, cx - 1, dy - 1, dx - 1, 0);
  if (maxnow == INT_MAX)
    cout << -1 << endl;
  else
    cout << maxnow << endl;
}