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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;

#define eb emplace_back
#define pb push_back
#define MP make_pair
#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, j, n) for (int i = j; i < n; ++i)
#define repn(i, j, n) for (int i = j; i <= n; ++i)
#define revn(i, j, n) for (int i = j; i >= n; --i)
#define sz(i) i.size()
#define mem(i, v) memset(i, v, sizeof(i))
#define all(v) v.begin(), v.end()
#define endl '\\n'
// #define int long long
#define INF 1e9
#define ss second
#define ff first

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pair<int, int>> piii;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const int mod = 1e9 + 7;
// const int mod=998244353;
const int up = 1e5 + 10;
const int MAXN = 1e6 + 10;

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

void solve() {
  int h, w, k, x1, x2, y1, y2, x, y, nx, ny;
  cin >> h >> w >> k;
  char a[h + 1][w + 1];
  int dis[h + 1][w + 1], vis[h + 1][w + 1];
  string s;
  cin >> x1 >> y1 >> x2 >> y2;
  repn(i, 1, h) {
    cin >> s;
    repn(j, 1, w) {
      a[i][j] = s[j - 1];
      dis[i][j] = INF;
      vis[i][j] = 0;
    }
  }

  dis[x1][y1] = 0;
  queue<pii> q;
  q.push({x1, y1});
  while (!q.empty()) {
    x = q.front().ff;
    y = q.front().ss;
    q.pop();
    vis[x][y] = 1;
    // cout<<x<<" "<<y<<endl;
    //  if(x==x2 && y==y2)
    //  {
    //      cout<<"HAHA"<<endl;
    //  }
    repn(i, 0, 3) {
      repn(l, 1, k) {
        nx = x + l * dx[i];
        ny = y + l * dy[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w) {
          if (a[nx][ny] == '@')
            break;
          if (vis[nx][ny])
            break;
          if (dis[nx][ny] != INF && dis[x][y] + 1 > dis[nx][ny]) {
            break;
          } else if (dis[nx][ny] != INF) {
            continue;
          }
          if (dis[nx][ny] > dis[x][y] + 1) {
            dis[nx][ny] = dis[x][y] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
  if (dis[x2][y2] == INF) {
    cout << -1 << endl;
    return;
  }
  cout << dis[x2][y2] << endl;
}

int32_t main() {
  fast_cin();
  int T;
  // sieve();
  // cin>>T;
  T = 1;
  int t1 = 1;
  while (true) {
    // cout<<"Case "<<t1<<": ";
    solve();
    t1++;
    if (t1 > T)
      break;
  }
  // cout<<ans<<endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;

#define eb emplace_back
#define pb push_back
#define MP make_pair
#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, j, n) for (int i = j; i < n; ++i)
#define repn(i, j, n) for (int i = j; i <= n; ++i)
#define revn(i, j, n) for (int i = j; i >= n; --i)
#define sz(i) i.size()
#define mem(i, v) memset(i, v, sizeof(i))
#define all(v) v.begin(), v.end()
#define endl '\\n'
// #define int long long
#define INF 1e9
#define ss second
#define ff first

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pair<int, int>> piii;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const int mod = 1e9 + 7;
// const int mod=998244353;
const int up = 1e5 + 10;
const int MAXN = 1e6 + 10;

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

void solve() {
  int h, w, k, x1, x2, y1, y2, x, y, nx, ny;
  cin >> h >> w >> k;
  char a[h + 1][w + 1];
  int dis[h + 1][w + 1], vis[h + 1][w + 1];
  string s;
  cin >> x1 >> y1 >> x2 >> y2;
  repn(i, 1, h) {
    cin >> s;
    repn(j, 1, w) {
      a[i][j] = s[j - 1];
      dis[i][j] = INF;
      vis[i][j] = 0;
    }
  }

  dis[x1][y1] = 0;
  queue<pii> q;
  q.push({x1, y1});
  while (!q.empty()) {
    x = q.front().ff;
    y = q.front().ss;
    q.pop();
    vis[x][y] = 1;
    // cout<<x<<" "<<y<<endl;
    //  if(x==x2 && y==y2)
    //  {
    //      cout<<"HAHA"<<endl;
    //  }
    repn(i, 0, 3) {
      repn(l, 1, k) {
        nx = x + l * dx[i];
        ny = y + l * dy[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w) {
          if (a[nx][ny] == '@')
            break;
          if (vis[nx][ny])
            break;
          if (dis[nx][ny] != INF) {
            continue;
          }
          if (dis[nx][ny] > dis[x][y] + 1) {
            dis[nx][ny] = dis[x][y] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
  if (dis[x2][y2] == INF) {
    cout << -1 << endl;
    return;
  }
  cout << dis[x2][y2] << endl;
}

int32_t main() {
  fast_cin();
  int T;
  // sieve();
  // cin>>T;
  T = 1;
  int t1 = 1;
  while (true) {
    // cout<<"Case "<<t1<<": ";
    solve();
    t1++;
    if (t1 > T)
      break;
  }
  // cout<<ans<<endl;
}