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

#include <bits/stdc++.h>

using namespace std;

#define int long long

#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define MP make_pair

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define LOCAL
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<int> vi;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef unsigned long long ull;

const int INF = LONG_LONG_MAX;
const int NINF = LONG_LONG_MIN / 2;
const double PI = 3.141592653589793238;

// MODULAR ARITHMATIC
const int mmod = 1000000000 + 7;

int add(int a, int b, int m = mmod) { return (a % m + b % m) % m; }

int sub(int a, int b, int m = mmod) { return (a % m - b % m + m) % m; }

int mul(int a, int b, int m = mmod) { return ((a % m) * (b % m) % m); }

int pwr(int a, int b, int m = mmod) {
  a %= m;
  int r = 1;
  while (b) {
    if (b & 1)
      r = r * a % m;
    b >>= 1;
    a = a * a % m;
  }
  return r;
}

int inv(int a, int m = mmod) { return pwr(a, m - 2, m); }

bool multi = false;
struct Solve {
  string endl = "\\n";

  Solve() {
    int h, w;
    cin >> h >> w;
    int ch, cw;
    cin >> ch >> cw;
    int dh, dw;
    cin >> dh >> dw;

    string s[h];
    for (int i = 0; i < h; i++) {
      cin >> s[i];
    }

    vector<vector<bool>> vis(h + 1, vb(w + 1, false));

    queue<ii> q;
    int cnt = 0;
    q.push({ch, cw});

    bool ok = true;
    vector<ii> list;
    while (true) {
      for (int i = 0; i < (int)list.size(); i++) {
        for (int j = -2; j <= 2; j++) {
          for (int k = -2; k <= 2; k++) {
            if (list[i].first + j >= 1 && list[i].second + k >= 1 &&
                list[i].first + j <= h && list[i].second + k <= w) {
              if (s[list[i].first + j - 1][list[i].second + k - 1] == '.' &&
                  !vis[list[i].first + j][list[i].second + k]) {
                vis[list[i].first + j][list[i].second + k] = true;
                q.push({list[i].first + j, list[i].second + k});
              }
            }
          }
        }
      }

      if ((int)q.size() == 0) {
        ok = false;
        break;
      }

      list.clear();
      while (!q.empty()) {
        ii cur = q.front();
        //~ debug(cur);
        q.pop();
        //~ debug(cur);
        list.PB(cur);
        //~ cout << vis[cur.first][cur.second] << endl;

        vis[cur.first][cur.second] = true;
        if (cur.first + 1 <= h && s[cur.first][cur.second - 1] == '.' &&
            vis[cur.first + 1][cur.second] == false) {
          q.push({cur.first + 1, cur.second});
          vis[cur.first + 1][cur.second] = true;
        }
        if (cur.first - 1 >= 1 && s[cur.first - 2][cur.second - 1] == '.' &&
            vis[cur.first - 1][cur.second] == false) {
          q.push({cur.first - 1, cur.second});
          vis[cur.first - 1][cur.second] = true;
        }
        if (cur.second + 1 <= w && s[cur.first - 1][cur.second] == '.' &&
            vis[cur.first][cur.second + 1] == false) {
          q.push({cur.first, cur.second + 1});
          vis[cur.first][cur.second + 1] = true;
        }
        if (cur.second - 1 >= 1 && s[cur.first - 1][cur.second - 2] == '.' &&
            vis[cur.first][cur.second - 1] == false) {
          q.push({cur.first, cur.second - 1});
          vis[cur.first][cur.second - 1] = true;
        }
        //~ debug(list);
        //~ debug(q.size());
      }

      if ((int)list.size() == 0) {
        ok = false;
        break;
      }
      if (vis[dh][dw]) {
        break;
      }
      cnt++;
    }

    if (ok) {
      cout << cnt << endl;
    } else {
      cout << -1 << endl;
    }
  }
};

int32_t main() {
  FAST_IO;

  int T = 1;
  if (multi)
    cin >> T;
  for (int i = 1; i <= T; i++) {
    Solve me;
  }
}
// g++ -Wall -Wextra -pedantic -std=c++17 -O2 -Wshadow -Wformat=2 -Wfloat-equal
// -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual
// -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2
// -fsanitize=address -fsanitize=undefined -fno-sanitize-recover
// -fstack-protector


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long

#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define MP make_pair

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define LOCAL
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<int> vi;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef unsigned long long ull;

const int INF = LONG_LONG_MAX;
const int NINF = LONG_LONG_MIN / 2;
const double PI = 3.141592653589793238;

// MODULAR ARITHMATIC
const int mmod = 1000000000 + 7;

int add(int a, int b, int m = mmod) { return (a % m + b % m) % m; }

int sub(int a, int b, int m = mmod) { return (a % m - b % m + m) % m; }

int mul(int a, int b, int m = mmod) { return ((a % m) * (b % m) % m); }

int pwr(int a, int b, int m = mmod) {
  a %= m;
  int r = 1;
  while (b) {
    if (b & 1)
      r = r * a % m;
    b >>= 1;
    a = a * a % m;
  }
  return r;
}

int inv(int a, int m = mmod) { return pwr(a, m - 2, m); }

bool multi = false;
struct Solve {
  string endl = "\\n";

  Solve() {
    int h, w;
    cin >> h >> w;
    int ch, cw;
    cin >> ch >> cw;
    int dh, dw;
    cin >> dh >> dw;

    string s[h];
    for (int i = 0; i < h; i++) {
      cin >> s[i];
    }

    vvi vis(h + 1, vi(w + 1));

    queue<ii> q;
    int cnt = 0;
    q.push({ch, cw});

    bool ok = true;
    vector<ii> list;
    while (true) {
      for (int i = 0; i < (int)list.size(); i++) {
        for (int j = -2; j <= 2; j++) {
          for (int k = -2; k <= 2; k++) {
            if (list[i].first + j >= 1 && list[i].second + k >= 1 &&
                list[i].first + j <= h && list[i].second + k <= w) {
              if (s[list[i].first + j - 1][list[i].second + k - 1] == '.' &&
                  !vis[list[i].first + j][list[i].second + k]) {
                vis[list[i].first + j][list[i].second + k] = true;
                q.push({list[i].first + j, list[i].second + k});
              }
            }
          }
        }
      }

      if ((int)q.size() == 0) {
        ok = false;
        break;
      }

      list.clear();
      while (!q.empty()) {
        ii cur = q.front();
        q.pop();
        //~ debug(cur);
        list.PB(cur);
        vis[cur.first][cur.second] = true;
        if (cur.first + 1 <= h && s[cur.first][cur.second - 1] == '.' &&
            !vis[cur.first + 1][cur.second]) {
          q.push({cur.first + 1, cur.second});
        }
        if (cur.first - 1 >= 1 && s[cur.first - 2][cur.second - 1] == '.' &&
            !vis[cur.first - 1][cur.second]) {
          q.push({cur.first - 1, cur.second});
        }
        if (cur.second + 1 <= w && s[cur.first - 1][cur.second] == '.' &&
            !vis[cur.first][cur.second + 1]) {
          q.push({cur.first, cur.second + 1});
        }
        if (cur.second - 1 >= 1 && s[cur.first - 1][cur.second - 2] == '.' &&
            !vis[cur.first][cur.second - 1]) {
          q.push({cur.first, cur.second - 1});
        }
      }
      if ((int)list.size() == 0) {
        ok = false;
        break;
      }
      if (vis[dh][dw]) {
        break;
      }
      cnt++;
    }

    if (ok) {
      cout << cnt << endl;
    } else {
      cout << -1 << endl;
    }
  }
};

int32_t main() {
  FAST_IO;

  int T = 1;
  if (multi)
    cin >> T;
  for (int i = 1; i <= T; i++) {
    Solve me;
  }
}
// g++ -Wall -Wextra -pedantic -std=c++17 -O2 -Wshadow -Wformat=2 -Wfloat-equal
// -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual
// -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2
// -fsanitize=address -fsanitize=undefined -fno-sanitize-recover
// -fstack-protector