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

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
// Problem : F - Pond Skater
// Contest : AtCoder - AtCoder Beginner Contest 170
// URL : https://atcoder.jp/contests/abc170/tasks/abc170_f
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define iter(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a) + 1)
#define fi first
#define se second
#define pb push_back

#define ll long long
#define pii pair<int, int>
// #define int ll
const int MOD = 1000000007;

queue<pii> b;
vector<vector<int>> mat;
vector<vector<int>> weig;
int n, m, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void w(int x1, int y1) {
  weig[x1][y1] = 0;
  b.push({x1, y1});
  while (!b.empty()) {
    pii t = b.front();
    b.pop();
    rep(d, 4) {
      int px = t.fi, py = t.se;
      px += dx[d];
      py += dy[d];
      if (!(0 <= px && px < n && 0 <= py && py < m) || mat[px][py] == -1)
        continue;
      if (weig[px][py] == -2 || weig[px][py] > weig[t.fi][t.se] + 1) {
        weig[px][py] = weig[t.fi][t.se] + 1;
        b.push({px, py});
      }
    }
  }
}

struct qel {
  pii v;
  const bool operator<(const qel &o) const {
    return weig[v.fi][v.se] > weig[o.v.fi][o.v.se];
  }
};
priority_queue<qel> b2;

int bfs(int x1, int y1, int x2, int y2) {
  mat[x1][y1] = 0;
  b2.push({{x1, y1}});
  int itt = 0, ans = -1;
  while (!b2.empty()) {
    pii t = b2.top().v;
    b2.pop();
    if (t.fi == x2 && t.se == y2) {
      if (ans == -1 || mat[x2][y2] < ans)
        ans = mat[x2][y2];
    }
    itt++;
    if (itt == 10000000)
      break;
    rep(d, 4) {
      int px = t.fi, py = t.se, ocu = 0;
      rep1(p, K) {
        px += dx[d];
        py += dy[d];
        if (!(0 <= px && px < n && 0 <= py && py < m) || mat[px][py] == -1)
          break;
        if (mat[px][py] == -2 || mat[px][py] > mat[t.fi][t.se] + 1) {
          mat[px][py] = mat[t.fi][t.se] + 1;
          b2.push({{px, py}});
        } else {
          ocu++;
          if (ocu == 100)
            break;
        }
      }
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  rep(i, n) {
    string s;
    cin >> s;
    vector<int> p, k;
    rep(j, m) {
      if (s[j] == '.')
        p.pb(-2);
      else
        p.pb(-1);
      k.pb(-2);
    }
    mat.pb(p);
    weig.pb(k);
  }
  w(x1, y1);
  cout << bfs(x1, y1, x2, y2) << endl;
}


Error: TLE

Fixed submission:


// Problem : F - Pond Skater
// Contest : AtCoder - AtCoder Beginner Contest 170
// URL : https://atcoder.jp/contests/abc170/tasks/abc170_f
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define iter(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a) + 1)
#define fi first
#define se second
#define pb push_back

#define ll long long
#define pii pair<int, int>
// #define int ll
const int MOD = 1000000007;

queue<pii> b;
vector<vector<int>> mat;
vector<vector<int>> weig;
int n, m, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void w(int x1, int y1) {
  weig[x1][y1] = 0;
  b.push({x1, y1});
  while (!b.empty()) {
    pii t = b.front();
    b.pop();
    rep(d, 4) {
      int px = t.fi, py = t.se;
      px += dx[d];
      py += dy[d];
      if (!(0 <= px && px < n && 0 <= py && py < m) || mat[px][py] == -1)
        continue;
      if (weig[px][py] == -2 || weig[px][py] > weig[t.fi][t.se] + 1) {
        weig[px][py] = weig[t.fi][t.se] + 1;
        b.push({px, py});
      }
    }
  }
}

struct qel {
  pii v;
  const bool operator<(const qel &o) const {
    return weig[v.fi][v.se] > weig[o.v.fi][o.v.se];
  }
};
priority_queue<qel> b2;

int bfs(int x1, int y1, int x2, int y2) {
  mat[x1][y1] = 0;
  b2.push({{x1, y1}});
  int itt = 0, ans = -1;
  while (!b2.empty()) {
    pii t = b2.top().v;
    b2.pop();
    if (t.fi == x2 && t.se == y2) {
      if (ans == -1 || mat[x2][y2] < ans)
        ans = mat[x2][y2];
    }
    itt++;
    if (itt == 10000000)
      break;
    rep(d, 4) {
      int px = t.fi, py = t.se, ocu = 0;
      rep1(p, K) {
        px += dx[d];
        py += dy[d];
        if (!(0 <= px && px < n && 0 <= py && py < m) || mat[px][py] == -1)
          break;
        if (mat[px][py] == -2 || mat[px][py] > mat[t.fi][t.se] + 1) {
          mat[px][py] = mat[t.fi][t.se] + 1;
          b2.push({{px, py}});
        } else {
          ocu++;
          if (ocu == 500)
            break;
        }
      }
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  rep(i, n) {
    string s;
    cin >> s;
    vector<int> p, k;
    rep(j, m) {
      if (s[j] == '.')
        p.pb(-2);
      else
        p.pb(-1);
      k.pb(-2);
    }
    mat.pb(p);
    weig.pb(k);
  }
  w(x1, y1);
  cout << bfs(x1, y1, x2, y2) << endl;
}