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

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

// PointBlank's code (⌐■_■)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll power(ll i, ll j);
#define repp(I, A, B) for (int I = A; I <= B; I++)
#define rep(I, A, B) for (int I = A; I < B; I++)
#define rrep(I, B, A) for (int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(this) cerr << "> " << #this << " : " << this << "\\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M)                                                            \\
  repp(I, 1, M) {                                                              \\
    int A, B;                                                                  \\
    cin >> A >> B;                                                             \\
    v[A].pb(B), v[B].pb(A);                                                    \\
  }
#define in_edges2(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    int A, B;                                                                  \\
    cin >> A >> B;                                                             \\
    v[A].pb(B);                                                                \\
  }
#define in_edges3(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    int A, B, C;                                                               \\
    cin >> A >> B >> C;                                                        \\
    v[A].pb(B, C), v[B].pb(A, C);                                              \\
  }
#define endl "\\n"

const ll MOD = 1000000007;
const ll INF = 1e18;
const int N = 1e3 + 3;
const int inf = 1e8;

int n, m;
int sx, sy;
int ex, ey;
int dp[N][N];
char mat[N][N];

void bfs() {
  deque<pair<int, int>> q;
  q.push_back(MP(sx, sy));
  dp[sx][sy] = 0;

  while (q.size()) {
    auto p = q.front();
    q.pop_front();
    auto i = p.ff;
    auto j = p.ss;

    int nx[] = {i + 1, i - 1, i, i};
    int ny[] = {j, j, j + 1, j - 1};

    rep(k, 0, 4) {
      int x = nx[k], y = ny[k];
      if (x < 1 || x > n || y < 1 || y > m)
        continue;
      if (mat[x][y] == '#')
        continue;
      if (dp[x][y] <= dp[i][j])
        continue;
      dp[x][y] = dp[i][j];
      q.push_front(MP(x, y));
    }

    repp(dx, -2, 2) {
      repp(dy, -2, 2) {
        int x = i + dx;
        int y = j + dy;
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j] + 1)
          continue;
        dp[x][y] = dp[i][j] + 1;
        q.push_back(MP(x, y));
      }
    }
  }
}

int main() // PointBlank's code ¯\\_(ツ)_/¯
{
  quickio rep(i, 0, N) rep(j, 0, N) mat[i][j] = '#', dp[i][j] = inf;

  cin >> n >> m;
  cin >> sx >> sy;
  cin >> ex >> ey;
  repp(i, 1, n) repp(j, 1, m) cin >> mat[i][j];

  bfs();
  rep(i, 0, N) rep(j, 0, N) if (dp[i][j] == inf) dp[i][j] = -1;

  cout << dp[ex][ey];
}

ll power(ll i, ll j) {
  ll res = 1;
  i %= MOD;
  while (j > 0) {
    if (j & 1)
      res = (res * i) % MOD;
    j = j >> 1, i = (i * i) % MOD;
  }
  return res;
}


Error: TLE

Fixed submission:

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

// PointBlank's code (⌐■_■)

#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

ll power(ll i, ll j);
#define repp(I, A, B) for (ll I = A; I <= B; I++)
#define rep(I, A, B) for (ll I = A; I < B; I++)
#define rrep(I, B, A) for (ll I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(this) cerr << "> " << #this << " : " << this << "\\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M)                                                            \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B), v[B].pb(A);                                                    \\
  }
#define in_edges2(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    ll A, B;                                                                   \\
    cin >> A >> B;                                                             \\
    v[A].pb(B);                                                                \\
  }
#define in_edges3(M)                                                           \\
  repp(I, 1, M) {                                                              \\
    ll A, B, C;                                                                \\
    cin >> A >> B >> C;                                                        \\
    v[A].pb(B, C), v[B].pb(A, C);                                              \\
  }
#define endl "\\n"

const ll MOD = 1000000007;
const ll INF = 1e18;
const ll N = 1e3 + 3;
const ll inf = 1e15;

ll n, m;
ll sx, sy;
ll ex, ey;
ll dp[N][N];
char mat[N][N];

void bfs() {
  queue<pair<ll, ll>> q;
  q.push(MP(sx, sy));
  dp[sx][sy] = 0;
  int loop = 0;
  // set<pair<int, int> > st;
  // st.emplace(sx, sy);
  while (q.size()) {
    loop++;
    if (loop > 4000000)
      return;
    auto p = q.front();
    q.pop();
    // st.erase(p);
    auto i = p.ff;
    auto j = p.ss;

    ll nx[] = {i + 1, i - 1, i, i};
    ll ny[] = {j, j, j + 1, j - 1};

    rep(k, 0, 4) {
      ll x = nx[k], y = ny[k];
      if (x < 1 || x > n || y < 1 || y > m)
        continue;
      if (mat[x][y] == '#')
        continue;
      if (dp[x][y] <= dp[i][j])
        continue;

      dp[x][y] = dp[i][j];
      // if(st.count(MP(x, y)) == 0){
      q.push(MP(x, y));
      // st.emplace(x, y);
      // }
    }

    repp(dx, -2, 2) {
      repp(dy, -2, 2) {
        ll x = i + dx;
        ll y = j + dy;
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j] + 1)
          continue;

        dp[x][y] = dp[i][j] + 1;
        // if(st.count(MP(x, y)) == 0){
        q.push(MP(x, y));
        // st.emplace(x, y);
        // }
      }
    }
  }
}

int main() // PointBlank's code ¯\\_(ツ)_/¯
{
  quickio rep(i, 0, N) rep(j, 0, N) mat[i][j] = '#', dp[i][j] = inf;

  cin >> n >> m;
  cin >> sx >> sy;
  cin >> ex >> ey;
  repp(i, 1, n) repp(j, 1, m) cin >> mat[i][j];

  bfs();

  repp(i, 1, n) {
    repp(j, 1, m) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  rrep(i, n, 1) {
    rrep(j, m, 1) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  repp(j, 1, m) {
    repp(i, 1, n) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  rrep(j, m, 1) {
    rrep(i, n, 1) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }
  repp(i, 1, n) {
    repp(j, 1, m) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  rrep(i, n, 1) {
    rrep(j, m, 1) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  repp(j, 1, m) {
    repp(i, 1, n) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }

  rrep(j, m, 1) {
    rrep(i, n, 1) {
      ll nx[] = {i + 1, i - 1, i, i};
      ll ny[] = {j, j, j + 1, j - 1};

      rep(k, 0, 4) {
        ll x = nx[k], y = ny[k];
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (mat[x][y] == '#')
          continue;
        if (dp[x][y] <= dp[i][j])
          continue;

        dp[x][y] = dp[i][j];
        // if(st.count(MP(x, y)) == 0){
        // st.emplace(x, y);
        // }
      }

      repp(dx, -2, 2) {
        repp(dy, -2, 2) {
          ll x = i + dx;
          ll y = j + dy;
          if (x < 1 || x > n || y < 1 || y > m)
            continue;
          if (mat[x][y] == '#')
            continue;
          if (dp[x][y] <= dp[i][j] + 1)
            continue;

          dp[x][y] = dp[i][j] + 1;
          // if(st.count(MP(x, y)) == 0){
          // st.emplace(x, y);
          // }
        }
      }
    }
  }
  rep(i, 0, N) rep(j, 0, N) if (dp[i][j] == inf) dp[i][j] = -1;

  cout << dp[ex][ey];
}

ll power(ll i, ll j) {
  ll res = 1;
  i %= MOD;
  while (j > 0) {
    if (j & 1)
      res = (res * i) % MOD;
    j = j >> 1, i = (i * i) % MOD;
  }
  return res;
}