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

#include <iostream>
#include <queue>
#include <vector>
#define f first
#define s second
#define pii pair<int, int>
using namespace std;
typedef queue<pii> qpii;

void test_case() {
  int n, m, k, a, b, c, d;
  cin >> n >> m >> k >> a >> b >> c >> d;
  vector<string> grid(n + 1);
  vector<vector<int>> odl(n + 1, vector<int>(m + 1, 1e9));
  vector<vector<bool>> odw(n + 1, vector<bool>(m + 1));
  //	int odl[n+1][m+1];
  for (int i = 1; i <= n; i++) {
    cin >> grid[i];
  }
  if (grid[a][b - 1] == '@') {
    cout << -1 << '\\n';
    return;
  }
  if (a == c && b == d) {
    cout << 0 << '\\n';
    return;
  }
  qpii Q;
  Q.push({a, b});
  odl[a][b] = 0;
  odw[a][b] = true;
  while (!Q.empty()) {
    pii cur = Q.front();
    Q.pop();
    int X = cur.f, Y = cur.s, licz = k;
    while (X < n && licz) {
      X++;
      licz--;
      if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (X > 1 && licz) {
      X--;
      licz--;
      if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (Y < m && licz) {
      Y++;
      licz--;
      if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (Y > 1 && licz) {
      Y--;
      licz--;
      if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
  }
  // for (int i = 1; i <= n; i ++) {
  // for (int j = 1; j <= m; j ++) {
  // cout << odl[i][j] << ' ';
  //}
  // cout << '\\n';
  //}
  cout << -1 << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  // cin >> q;
  while (q--) {
    test_case();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
#define f first
#define s second
#define pii pair<int, int>
using namespace std;
typedef queue<pii> qpii;

void test_case() {
  int n, m, k, a, b, c, d;
  cin >> n >> m >> k >> a >> b >> c >> d;
  vector<string> grid(n + 1);
  vector<vector<int>> odl(n + 1, vector<int>(m + 1));
  vector<vector<bool>> odw(n + 1, vector<bool>(m + 1));
  //	int odl[n+1][m+1];
  for (int i = 1; i <= n; i++) {
    cin >> grid[i];
  }
  if (grid[a][b - 1] == '@') {
    cout << -1 << '\\n';
    return;
  }
  if (a == c && b == d) {
    cout << 0 << '\\n';
    return;
  }
  qpii Q;
  Q.push({a, b});
  odl[a][b] = 0;
  odw[a][b] = true;
  while (!Q.empty()) {
    pii cur = Q.front();
    Q.pop();
    int X = cur.f, Y = cur.s, licz = k;
    while (X < n && licz) {
      X++;
      licz--;
      if (grid[X][Y - 1] == '@')
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (X > 1 && licz) {
      X--;
      licz--;
      if (grid[X][Y - 1] == '@')
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (Y < m && licz) {
      Y++;
      licz--;
      if (grid[X][Y - 1] == '@')
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
    X = cur.f, Y = cur.s, licz = k;
    while (Y > 1 && licz) {
      Y--;
      licz--;
      if (grid[X][Y - 1] == '@')
        break;
      if (!odw[X][Y]) {
        odw[X][Y] = true;
        odl[X][Y] = odl[cur.f][cur.s] + 1;
        if (X == c && Y == d) {
          cout << odl[X][Y] << '\\n';
          return;
        }
        Q.push({X, Y});
      }
    }
  }
  // for (int i = 1; i <= n; i ++) {
  // for (int j = 1; j <= m; j ++) {
  // cout << odl[i][j] << ' ';
  //}
  // cout << '\\n';
  //}
  cout << -1 << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  // cin >> q;
  while (q--) {
    test_case();
  }
  return 0;
}