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

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

#define int long long
#define ll long long
#define ffor(i, n) for (int i = 0; i < (n); ++i)
#define fro(i, j, n) for (int i = (j); i < (n); ++i)
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define ff first
#define ss second
const int mod = 1e9 + 7;
const int inf = 1e18 + 5;

template <typename T> T binpow(T a, T b, T modi) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % modi;
    }
    b /= 2;
    (a *= a) %= modi;
  }
  return res;
}

template <typename T> T binpow(T a, T b) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a);
    }
    b /= 2;
    a *= a;
  }
  return res;
}

template <typename T> T add(T a, T b) { return (a + b) % mod; }
template <typename T> T sub(T a, T b) { return add(a, mod - b); }
template <typename T> T mul(T a, T b) { return (a * b) % mod; }
template <typename T> T inv(T a) { return binpow(a, mod - 2, mod); }

int make_int(string s) {
  int x = 0;
  reverse(s.begin(), s.end());
  string tmp = s;
  for (int i = 0; i < s.length(); ++i) {
    x *= 10;
    x += (int)(tmp.back() - '0');
    tmp.pop_back();
  }
  return x;
}

string make_string(int s) {
  string x;
  while (s) {
    x.push_back(char(s % 10 + '0'));
    s /= 10;
  }
  reverse(x.begin(), x.end());
  return x;
}

random_device rd;
mt19937 g(rd());

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

const int N = 100;

vi fact;

void factorial() {
  fact.resize(N);
  fact[0] = 1, fact[1] = 1;
  fro(i, 2, N) { fact[i] = (fact[i - 1] * i) % mod; }
}

int nCr(int n, int r) { return (mul(fact[n], inv(mul(fact[r], fact[n - r])))); }

int nPr(int n, int r) { return (mul(nCr(n, r), fact[r])); }

// Linear Seive

int lp[N + 1];
vector<int> pr;
void ls() {
  for (int i = 2; i <= N; ++i) {
    // means prime
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; j++) {
      lp[i * pr[j]] = pr[j];
    }
  }
}

// getting the prime factorisation

vector<int> getfc(int n) {
  vi factors;
  while (n != 1) {
    int x = lp[n];
    factors.push_back(x);
    while (n % x == 0) {
      n /= x;
    }
  }
  return factors;
}

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int h, w, k;

bool ok(int x, int y) {
  bool res = (x >= 0 and x < h and y >= 0 and y < w);
  return res;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // shuffle(all(a),g);
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  vector<vector<char>> a(h, vector<char>(w));
  ffor(i, h) {
    ffor(j, w) { cin >> a[i][j]; }
  }
  vvi dist(h, vi(w, inf));
  queue<pii> q;
  if (a[x1][y1] != '@') {
    dist[x1][y1] = 0;
    q.push({x1, y1});
  }

  while (!q.empty()) {
    auto tp = q.front();
    q.pop();
    int xxx = tp.first;
    int yyy = tp.second;
    for (int i = 0; i < 4; ++i) {
      for (int z = 1; z <= k; ++z) {
        int xx = xxx + dx[i] * z;
        int yy = yyy + dy[i] * z;
        if (!ok(xx, yy) or a[xx][yy] == '@' or dist[xx][yy] <= dist[xxx][yyy])
          break;
        if (dist[xx][yy] > dist[xxx][yyy] + 1) {
          dist[xx][yy] = dist[xxx][yyy] + 1;
          q.push({xx, yy});
        }
      }
    }
  }

  dist[x2][y2] == inf ? cout << -1 : cout << dist[x2][y2];
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long
#define ll long long
#define ffor(i, n) for (int i = 0; i < (n); ++i)
#define fro(i, j, n) for (int i = (j); i < (n); ++i)
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define ff first
#define ss second
const int mod = 1e9 + 7;
const int inf = 1e18 + 5;

template <typename T> T binpow(T a, T b, T modi) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % modi;
    }
    b /= 2;
    (a *= a) %= modi;
  }
  return res;
}

template <typename T> T binpow(T a, T b) {
  T res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a);
    }
    b /= 2;
    a *= a;
  }
  return res;
}

template <typename T> T add(T a, T b) { return (a + b) % mod; }
template <typename T> T sub(T a, T b) { return add(a, mod - b); }
template <typename T> T mul(T a, T b) { return (a * b) % mod; }
template <typename T> T inv(T a) { return binpow(a, mod - 2, mod); }

int make_int(string s) {
  int x = 0;
  reverse(s.begin(), s.end());
  string tmp = s;
  for (int i = 0; i < s.length(); ++i) {
    x *= 10;
    x += (int)(tmp.back() - '0');
    tmp.pop_back();
  }
  return x;
}

string make_string(int s) {
  string x;
  while (s) {
    x.push_back(char(s % 10 + '0'));
    s /= 10;
  }
  reverse(x.begin(), x.end());
  return x;
}

random_device rd;
mt19937 g(rd());

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

const int N = 100;

vi fact;

void factorial() {
  fact.resize(N);
  fact[0] = 1, fact[1] = 1;
  fro(i, 2, N) { fact[i] = (fact[i - 1] * i) % mod; }
}

int nCr(int n, int r) { return (mul(fact[n], inv(mul(fact[r], fact[n - r])))); }

int nPr(int n, int r) { return (mul(nCr(n, r), fact[r])); }

// Linear Seive

int lp[N + 1];
vector<int> pr;
void ls() {
  for (int i = 2; i <= N; ++i) {
    // means prime
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; j++) {
      lp[i * pr[j]] = pr[j];
    }
  }
}

// getting the prime factorisation

vector<int> getfc(int n) {
  vi factors;
  while (n != 1) {
    int x = lp[n];
    factors.push_back(x);
    while (n % x == 0) {
      n /= x;
    }
  }
  return factors;
}

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

int h, w, k;

bool ok(int x1, int y1) {
  if (x1 >= 0 && x1 < h && y1 >= 0 && y1 < w)
    return true;
  return false;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // shuffle(all(a),g);

  cin >> h >> w >> k;
  vector<vector<char>> a(h, vector<char>(w));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  ffor(i, h) {
    ffor(j, w) {
      char x;
      cin >> x;
      a[i][j] = x;
    }
  }
  vvi dist(h, vi(w, inf));
  // do bfs but wait update will be different
  queue<pii> q;
  if (a[x1][y1] != '@') {
    dist[x1][y1] = 0;
    q.push({x1, y1});
  }

  while (!q.empty()) {
    // go in one direction first

    pii tp = q.front();
    int x11 = tp.first;
    int y11 = tp.second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int xx = x11 + j * dx[i];
        int yy = y11 + j * dy[i];
        if (ok(xx, yy) && a[xx][yy] != '@') {
          if (dist[x11][y11] + 1 < dist[xx][yy]) {
            dist[xx][yy] = dist[x11][y11] + 1;
            q.push({xx, yy});
          }
        } else {
          break;
        }
      }
    }
  }

  // ffor(i,h){
  // 	ffor(j,w){
  // 		cout << dist[i][j] << ' ';
  // 	}
  // 	cout << '\\n';
  // }
  dist[x2][y2] == inf ? cout << -1 : cout << dist[x2][y2];
  return 0;
}