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

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
const int inf = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
struct node {
  int x, y;
};
int n, m, k, i, j, p1, q1, p2, q2, ans = inf, st[MAXN][MAXN];
char ch, c[MAXN][MAXN];
bool vis[MAXN][MAXN];
queue<node> q;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
inline void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
signed main(void) {
  read(n), read(m), read(k);
  read(p1), read(q1), read(p2), read(q2);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      ch = getchar();
      while (ch != '.' && ch != '@')
        ch = getchar();
      c[i][j] = ch;
    }
  vis[p1][q1] = 1;
  q.push(node{p1, q1});
  while (!q.empty()) {
    node u = q.front();
    q.pop();
    if (u.x == p2 && u.y == q2)
      ans = min(ans, st[u.x][u.y]);
    for (i = 0; i < 4; i++)
      for (j = 1; j <= k; j++) {
        node v = node{u.x + j * dx[i], u.y + j * dy[i]};
        if (v.x < 1 || v.x > n || v.y < 1 || v.y > m || c[v.x][v.y] == '@' ||
            (st[v.x][v.y] <= st[u.x][u.y] && st[v.x][v.y]))
          break;
        if (!vis[v.x][v.y]) {
          st[v.x][v.y] = st[u.x][u.y] + 1;
          vis[v.x][v.y] = 1;
          q.push(v);
        }
      }
  }
  print(ans == inf ? -1 : ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
const int inf = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
struct node {
  int x, y;
};
int n, m, k, i, j, p1, q1, p2, q2, ans = inf, st[MAXN][MAXN];
char ch, c[MAXN][MAXN];
bool vis[MAXN][MAXN];
queue<node> q;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
inline void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
signed main(void) {
  read(n), read(m), read(k);
  read(p1), read(q1), read(p2), read(q2);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      ch = getchar();
      while (ch != '.' && ch != '@')
        ch = getchar();
      c[i][j] = ch;
    }
  q.push(node{p1, q1});
  while (!q.empty()) {
    node u = q.front();
    q.pop();
    vis[u.x][u.y] = true;
    if (u.x == p2 && u.y == q2) {
      ans = min(ans, st[u.x][u.y]);
      continue;
    }
    for (i = 0; i < 4; i++)
      for (j = 1; j <= k; j++) {
        node v = node{u.x + j * dx[i], u.y + j * dy[i]};
        if (v.x < 1 || v.x > n || v.y < 1 || v.y > m || vis[v.x][v.y] ||
            c[v.x][v.y] == '@' ||
            (st[v.x][v.y] <= st[u.x][u.y] && st[v.x][v.y]))
          break;
        st[v.x][v.y] = st[u.x][u.y] + 1;
        q.push(v);
      }
  }
  print(ans == inf ? -1 : ans);
  return 0;
}