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

#include <bits/stdc++.h>
#define bug(x) cout << #x << " = " << x << endl
#define fr(x) freopen(x, "r", stdin)
#define fw(x) freopen(x, "w", stdout)
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define reset(x) memset(x, 0, sizeof(x))
#define up(i, l, r) for (int i = l; i <= r; i++)
#define down(i, r, l) for (int i = r; i >= l; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
int n, m, k, xa, ya, xb, yb, Ti;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;
  cin >> xa >> ya >> xb >> yb;
  xa--, ya--, xb--, yb--;

  vector<vector<char>> table(n, vector<char>(m));
  up(i, 0, n - 1) up(j, 0, m - 1) cin >> table[i][j];

  vector<vector<int>> dis(n, vector<int>(m, 2e9));
  // bfs()
  dis[xa][ya] = 0;
  queue<pii> q;
  q.push({xa, ya});
  while (!q.empty()) {
    int xx = q.front().fi, yy = q.front().se;
    q.pop();
    if (xx == xb && yy == yb)
      break;
    up(i, 1, k) {
      if (xx + i == n || table[xx + i][yy] != '.' ||
          dis[xx + i][yy] <= dis[xx][yy])
        break;
      else if (dis[xx + i][yy] > dis[xx][yy] + 1) {
        dis[xx + i][yy] = dis[xx][yy] + 1;
        q.push({xx + i, yy});
      }
    }
    up(i, 1, k) {
      if (xx - i == -1 || table[xx - i][yy] != '.' ||
          dis[xx - i][yy] <= dis[xx][yy])
        break;
      else if (dis[xx - i][yy] > dis[xx][yy] + 1) {
        dis[xx - i][yy] = dis[xx][yy] + 1;
        q.push({xx - i, yy});
      }
    }
    up(i, 1, k) {
      if (yy + i == m || table[xx][yy + i] != '.' ||
          dis[xx][yy + i] <= dis[xx][yy])
        break;
      else if (dis[xx][yy + i] > dis[xx][yy] + 1) {
        dis[xx][yy + i] = dis[xx][yy] + 1;
        q.push({xx, yy + i});
      }
    }
    up(i, 1, k) {
      if (yy - i == -1 || table[xx][yy - i] != '.' ||
          dis[xx][yy - i] <= dis[xx][yy])
        break;
      else if (dis[xx][yy - i] > dis[xx][yy] + 1) {
        dis[xx][yy - i] = dis[xx][yy] + 1;
        q.push({xx, yy - i});
      }
    }
  }

  Ti = dis[xb][yb];
  if (Ti == 2e9)
    Ti = -1;
  cout << Ti << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 30;
const int MaxR = 1e3;
const int MaxC = 1e3;
char mat[MaxR][MaxC + 2];
int step[MaxR][MaxC];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y, s;
  DATA(int x = 0, int y = 0, int s = 0) : x(x), y(y), s(s) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\\n", &R, &C, &K) != EOF) {
    scanf("%d %d %d %d\\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y, 0));
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (step[now.x][now.y] < INF)
        continue;
      step[now.x][now.y] = now.s;
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i], now.s + 1);
          if (nxt.x < 0 or nxt.x >= R or nxt.y < 0 or nxt.y >= C or
              mat[nxt.x][nxt.y] == '@' or step[nxt.x][nxt.y] <= nxt.s)
            break;
          Q.push(nxt);
        }
    }
    printf("%d\\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}