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

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
#define to_stirng to_string
#define INF 10000000000
using namespace std;

// cout << fixed << setprecision(12)

int main() {
  lint H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;

  vector<vector<lint>> hasu(H + 2, vector<lint>(W + 2));
  vector<vector<lint>> kaki(H + 2, vector<lint>(W + 2, INF));

  for (lint i = 0; i <= H + 1; i++) {
    hasu[i][0] = true;
    hasu[i][W + 1] = true;
  }
  for (lint j = 0; j <= W + 1; j++) {
    hasu[0][j] = true;
    hasu[H + 1][j] = true;
  }
  for (lint i = 1; i <= H; i++) {
    for (lint j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '@')
        hasu[i][j] = true;
      else
        hasu[i][j] = false;
    }
  }

  kaki[x1][y1] = 0;

  queue<pair<lint, lint>> next;
  next.push(MP(x1, y1));

  while (!(next.empty())) {
    pair<lint, lint> tmp = next.front();
    lint x0 = tmp.first;
    lint y0 = tmp.second;
    lint kaki0 = kaki[x0][y0];
    next.pop();
    for (lint i = 1; i <= K; i++) {
      lint u = x0 + i;
      lint v = y0;
      if (kaki[u][v] <= kaki0)
        break; // chokudai
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0 - i;
      lint v = y0;
      if (kaki[u][v] <= kaki0)
        break; // chokudai
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0;
      lint v = y0 + i;
      if (kaki[u][v] <= kaki0)
        break; // chokudai
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0;
      lint v = y0 - i;
      if (kaki[u][v] <= kaki0)
        break; // chokudai
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
  }

  // cout << hasu[3][1] << endl;

  /*
  for(lint i=1; i<=H; i++){
    for(lint j=1; j<=W; j++){
      cout << hasu[i][j] << ' ';
    }
    cout << endl;
  }
  */

  if (kaki[x2][y2] == INF)
    cout << "-1" << endl;
  else
    cout << kaki[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lint long long
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
#define to_stirng to_string
#define INF 10000000000
using namespace std;

// cout << fixed << setprecision(12)

int main() {
  lint H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;

  vector<vector<lint>> hasu(H + 2, vector<lint>(W + 2));
  vector<vector<lint>> kaki(H + 2, vector<lint>(W + 2, INF));

  for (lint i = 0; i <= H + 1; i++) {
    hasu[i][0] = true;
    hasu[i][W + 1] = true;
  }
  for (lint j = 0; j <= W + 1; j++) {
    hasu[0][j] = true;
    hasu[H + 1][j] = true;
  }
  for (lint i = 1; i <= H; i++) {
    for (lint j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '@')
        hasu[i][j] = true;
      else
        hasu[i][j] = false;
    }
  }

  kaki[x1][y1] = 0;

  queue<pair<lint, lint>> next;
  next.push(MP(x1, y1));

  while (!(next.empty())) {
    pair<lint, lint> tmp = next.front();
    lint x0 = tmp.first;
    lint y0 = tmp.second;
    lint kaki0 = kaki[x0][y0];
    next.pop();
    for (lint i = 1; i <= K; i++) {
      lint u = x0 + i;
      lint v = y0;
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0 - i;
      lint v = y0;
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0;
      lint v = y0 + i;
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
    for (lint i = 1; i <= K; i++) {
      lint u = x0;
      lint v = y0 - i;
      if (hasu[u][v])
        break;
      else if (kaki[u][v] > kaki0 + 1) {
        kaki[u][v] = kaki0 + 1;
        next.push(MP(u, v));
      }
    }
  }

  // cout << hasu[3][1] << endl;

  /*
  for(lint i=1; i<=H; i++){
    for(lint j=1; j<=W; j++){
      cout << hasu[i][j] << ' ';
    }
    cout << endl;
  }
  */

  if (kaki[x2][y2] == INF)
    cout << "-1" << endl;
  else
    cout << kaki[x2][y2] << endl;
}