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

#include <bits/stdc++.h>

using namespace std;
#ifdef ONLINE_JUDGE
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define int long long
#define ld1 long double
#define faster                                                                 \\
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),             \\
      cout << setprecision(14);
#define vint vector<int>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define mm0(X) memset((X), 0, sizeof((X)))
#define mm1(X) memset((X), -1, sizeof((X)))
#define INF (int)1e9
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define endl '\\n'
#define vcc vector<char>
#define rep(i, a, b) for (int i = a; i < b; i++)

// if you have no idea just guess the appropriate well-known algo instead of
// doing nothing -- Benq inspiration
const int mod = 998244353;
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}
inline int mul(int a, int b) { return (a * 1ll * b) % mod; }
inline int powmod(int a, int b) {
  int rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
inline int inv(int a) { return powmod(a, mod - 2); }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const int N = (int)2e5 + 6;
int h, w, kr;
int x[2];
int y[2];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int check(int i, int j) {
  if (i < h and j < w and i >= 0 and j >= 0)
    return 1;
  return 0;
}
void solve() {
  cin >> h >> w >> kr;
  cin >> x[0] >> y[0];
  cin >> x[1] >> y[1];

  for (int i = 0; i < 2; i++) {
    x[i]--, y[i]--;
  }

  char c[h][w];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> c[i][j];

  set<pair<int, pii>> s;
  s.insert({0, {x[0], y[0]}});

  int vis[h][w];
  mm0(vis);
  int dis[h][w];
  mm1(dis);
  int final = -1;

  while (!s.empty()) {
    auto k = *(s.begin());
    s.erase(s.begin());

    for (int kk = 0; kk < 4; kk++) {
      for (int i = 1; i <= kr; i++) {
        int movx = 0;

        int xx = k.second.first + dx[kk] * i;
        int xy = k.second.second + dy[kk] * i;

        if (xx == x[1] and xy == y[1]) {
          cout << k.first + 1 << endl;
          return;
        }

        if (!check(xx, xy))
          break;
        if (c[xx][xy] == '@')
          break;
        if (vis[xx][xy] and dis[xx][xy] < k.first + 1)
          break;
        //                if (vis[xx][xy]) continue;

        vis[xx][xy] = 1;
        dis[xx][xy] = k.first + 1;
        s.insert({k.first + 1, {xx, xy}});
      }
    }
  }
  cout << final << endl;
}

int32_t main() {
  faster;

#ifndef ONLINE_JUDGE
  READ("input.txt");
  WRITE("output.txt");
  double beg = clock();
#endif

  int t = 1;
  //    cin>>t;

  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  double endd = clock();
  cout << endl;
  cerr << "*** Total time = " << (endd - beg) / CLOCKS_PER_SEC;
#endif
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#ifdef ONLINE_JUDGE
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define int long long
#define ld1 long double
#define faster                                                                 \\
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),             \\
      cout << setprecision(14);
#define vint vector<int>
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pb push_back
#define mm0(X) memset((X), 0, sizeof((X)))
#define mm1(X) memset((X), -1, sizeof((X)))
#define INF (int)1e9
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define endl '\\n'
#define vcc vector<char>
#define rep(i, a, b) for (int i = a; i < b; i++)

// if you have no idea just guess the appropriate well-known algo instead of
// doing nothing -- Benq inspiration
const int mod = 998244353;
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}
inline int mul(int a, int b) { return (a * 1ll * b) % mod; }
inline int powmod(int a, int b) {
  int rt = 1;
  while (b > 0) {
    if (b & 1)
      rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
inline int inv(int a) { return powmod(a, mod - 2); }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const int N = (int)2e5 + 6;
int h, w, kr;
int x[2];
int y[2];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int check(int i, int j) {
  if (i < h and j < w and i >= 0 and j >= 0)
    return 1;
  return 0;
}
void solve() {
  cin >> h >> w >> kr;
  cin >> x[0] >> y[0];
  cin >> x[1] >> y[1];

  for (int i = 0; i < 2; i++) {
    x[i]--, y[i]--;
  }

  char c[h][w];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> c[i][j];

  set<pair<int, pii>> s;
  s.insert({0, {x[0], y[0]}});

  //    cerr<<x[1]<<" "<<y[1]<<endl;
  int vis[h][w];
  mm0(vis);
  //    cerr<<kr<<endl;

  int final = -1;
  while (!s.empty()) {
    auto k = *(s.begin());
    s.erase(s.begin());

    if (k.second.first == x[1] and k.second.second == y[1]) {
      cout << k.first + 1 << endl;
      return;
    }
    vis[k.second.first][k.second.second] = 1;

    for (int kk = 0; kk < 4; kk++) {
      //            cerr<<kr<<endl;

      for (int i = 1; i <= kr; i++) {
        int movx = 0;

        int xx = k.second.first + dx[kk] * i;
        int xy = k.second.second + dy[kk] * i;

        if (xx == x[1] and xy == y[1]) {
          cout << k.first + 1 << endl;
          return;
        }

        if (i == 1 and kk == 0)
          cerr << xx << " " << xy << endl;
        if (!check(xx, xy))
          break;
        if (c[xx][xy] == '@')
          break;
        if (vis[xx][xy])
          continue;

        vis[xx][xy] = 1;
        s.insert({k.first + 1, {xx, xy}});
        //                cerr<<xx<<" "<<xy<<" "<<k.first<<"
        //                "<<k.second.first<<" "<<k.second.second<<endl;
      }
    }
  }
  cout << final << endl;
}

int32_t main() {
  faster;

#ifndef ONLINE_JUDGE
  READ("input.txt");
  WRITE("output.txt");
  double beg = clock();
#endif

  int t = 1;
  //    cin>>t;

  while (t--) {
    solve();
  }

#ifndef ONLINE_JUDGE
  double endd = clock();
  cout << endl;
  cerr << "*** Total time = " << (endd - beg) / CLOCKS_PER_SEC;
#endif
}