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

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  char ch[n + 1][m + 1];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[i][j];
    }
  }
  if (ch[x2][y2] == '@') {
    cout << "-1";
    return 0;
  }

  ll dis[n + 1][m + 1];
  ll vis[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dis[i][j] = 100000000;
      vis[i][j] = false;
    }
  }
  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  dis[x1][y1] = 0;
  vis[x1][y1] = true;

  while (!q.empty()) {
    ll ux = q.front().first;
    ll uy = q.front().second;
    q.pop();
    if (ux == x2 and uy == y2)
      break;
    ll temp = 1;
    while (temp <= k) {
      int nx = ux + temp;
      int ny = uy;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@' or
          dis[nx][ny] < 1 + dis[ux][uy])
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux - temp;
      int ny = uy;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@' or
          dis[nx][ny] < 1 + dis[ux][uy])
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux;
      int ny = uy + temp;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@' or
          dis[nx][ny] < 1 + dis[ux][uy])
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux;
      int ny = uy - temp;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@' or
          dis[nx][ny] < 1 + dis[ux][uy])
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
  }
  if (dis[x2][y2] > 100000)
    dis[x2][y2] = -1;
  cout << dis[x2][y2];

  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  char ch[n + 1][m + 1];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[i][j];
    }
  }
  if (ch[x2][y2] == '@') {
    cout << "-1";
    return 0;
  }

  ll dis[n + 1][m + 1];
  ll vis[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dis[i][j] = 100000000;
      vis[i][j] = false;
    }
  }
  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  dis[x1][y1] = 0;
  vis[x1][y1] = true;

  while (!q.empty()) {
    ll ux = q.front().first;
    ll uy = q.front().second;
    q.pop();
    if (ux == x2 and uy == y2)
      break;
    ll temp = 1;
    while (temp <= k) {
      int nx = ux + temp;
      int ny = uy;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@')
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux - temp;
      int ny = uy;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@')
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux;
      int ny = uy + temp;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@')
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
    temp = 1;
    while (temp <= k) {
      int nx = ux;
      int ny = uy - temp;
      temp++;
      if (nx <= 0 or nx > n or ny <= 0 or ny > m or ch[nx][ny] == '@')
        break;
      if (vis[nx][ny] == false) {
        vis[nx][ny] = true;
        dis[nx][ny] = dis[ux][uy] + 1;
        q.push({nx, ny});
      }
    }
  }
  if (dis[x2][y2] > 100000)
    dis[x2][y2] = -1;
  cout << dis[x2][y2];

  return 0;
}