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


/*
 *  Author :: Kattu37
 */

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

#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define sortallr(x) sort(allr(x))
#define PI 3.1415926535897932384626
#define mod 1000000007

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void solve() {
  int h, w;
  cin >> h >> w;

  int p, q, r, s;
  cin >> p >> q;
  cin >> r >> s;
  p--;
  q--;
  r--;
  s--;

  queue<pair<int, int>> qu;
  vector<vector<bool>> vis(h, vector<bool>(w, 0));
  set<pair<int, int>> st;
  vector<string> grid;
  string str;

  for (int i = 0; i < h; i++) {
    cin >> str;
    grid.pb(str);
  }

  qu.push({p, q});
  vis[p][q] = 1;
  int magic = 0;
  bool ctr = 0;

  while (qu.empty() == 0) {
    int sz = qu.size();

    while (sz--) {
      pair<int, int> p = qu.front();
      qu.pop();
      st.insert({p.first, p.second});

      if (p.first == r && p.second == s) {
        ctr = 1;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int in = p.first + dx[i];
        int jn = p.second + dy[i];

        if (in >= 0 && in < h && jn >= 0 && jn < w) {
          if (!vis[in][jn] && grid[in][jn] == '.') {
            if (in == r && jn == s) {
              ctr = 1;
              break;
            }
            vis[in][jn] = 1;
            qu.push({in, jn});
            sz++;
          }
        }
      }
      if (ctr)
        break;
    }

    if (ctr)
      break;

    for (auto it : st) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int in = it.first + i;
          int jn = it.second + j;

          if (in >= 0 && in < h && jn >= 0 && jn < w) {
            if (!vis[in][jn] && grid[in][jn] == '.') {
              vis[in][jn] = 1;
              qu.push({in, jn});
            }
          }
        }
      }
    }

    st.clear();
    magic++;
  }

  if (ctr)
    cout << magic << "\\n";
  else
    cout << -1 << "\\n";
}

int main(void) {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:


/*
 *  Author :: Kattu37
 */

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

#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define sortallr(x) sort(allr(x))
#define PI 3.1415926535897932384626
#define mod 1000000007

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

bool isvalid(int i, int j, int n, int m) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return 0;
  return 1;
}

void solve() {
  int h, w;
  cin >> h >> w;

  int p, q, r, s;
  cin >> p >> q;
  cin >> r >> s;
  p--;
  q--;
  r--;
  s--;

  queue<pair<int, int>> qu;
  vector<vector<bool>> vis(h, vector<bool>(w, 0));
  set<pair<int, int>> st;
  vector<string> grid;
  string str;

  for (int i = 0; i < h; i++) {
    cin >> str;
    grid.pb(str);
  }

  qu.push({p, q});
  int magic = 0;
  bool ctr = 0;

  while (qu.empty() == 0) {
    int sz = qu.size();

    while (sz--) {
      pair<int, int> p = qu.front();
      qu.pop();
      vis[p.first][p.second] = 1;
      st.insert({p.first, p.second});

      if (p.first == r && p.second == s) {
        ctr = 1;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int in = p.first + dx[i];
        int jn = p.second + dy[i];

        if (isvalid(in, jn, h, w)) {
          if (!vis[in][jn] && grid[in][jn] == '.') {
            if (in == r && jn == s) {
              ctr = 1;
              break;
            }
            qu.push({in, jn});
            sz++;
          }
        }
      }
      if (ctr)
        break;
    }

    if (ctr)
      break;

    for (auto it : st) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int in = it.first + i;
          int jn = it.second + j;

          if (isvalid(in, jn, h, w)) {
            if (!vis[in][jn] && grid[in][jn] == '.')
              qu.push({in, jn});
          }
        }
      }
    }

    st.clear();
    magic++;
  }

  if (ctr)
    cout << magic << "\\n";
  else
    cout << -1 << "\\n";
}

int main(void) {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}