Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<int, int>;
const ll M = 1e9 + 7, mx = 200000;
void add(ll &x, ll y) {
  x += y;
  x %= M;
}
void chmax(ll &x, ll y) {
  if (x < y)
    x = y;
}

const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1};
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}

struct tup {
  ll x, y, c;
  bool operator<(const tup tt) const {
    if (c != tt.c)
      return c < tt.c;
    else if (x != tt.x)
      return x < tt.x;
    else
      return y < tt.y;
  }
  bool operator>(const tup tt) const {
    if (c != tt.c)
      return c > tt.c;
    else if (x != tt.x)
      return x > tt.x;
    else
      return y > tt.y;
  }
};

#include <ctime>
#include <random>

namespace r {
random_device rd;
mt19937 mt(rd());
// mt19937 mt(time(NULL));
int nd(int max) { return mt() % max + 1; }
int nd2(int min, int max) { return mt() % (max - min + 1) + min; }
} // namespace r

int main() {
  int h, w, k;
  // h=100, w=100, k=100;
  cin >> h >> w >> k;
  int fx, fy, lx, ly;
  cin >> fx >> fy >> lx >> ly;
  fx--;
  fy--;
  lx--;
  ly--;
  // fx=0, fy=0, lx=h-1, ly=w-1;

  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];

  /*for(int i=0;i<h;i++) {
    s[i].assign('a',w);
    for(int j=0;j<w;j++) {
      if (i==fx&&j==fy||i==lx&&j==ly) s[i][j]='.';//cout<<".";
      else if (r::nd(5)==1) s[i][j]='@';
      else s[i][j]='.';
    }
  }*/

  ll an[h][w];
  fill(an[0], an[h], LINF);
  queue<tup> que;
  an[fx][fy] = 0;
  que.push({fx, fy, 0});

  while (!que.empty()) {
    tup tp = que.front();
    que.pop();
    int cs = tp.c + 1;
    for (int i = 0; i < 4; i++) {
      int nx = tp.x, ny = tp.y;
      for (int j = 0; j < k; j++) {
        nx += up[i];
        ny += lf[i];
        if (vaild(nx, ny, h, w) && s[nx][ny] == '.') {
          if (cs < an[nx][ny]) {
            an[nx][ny] = cs;
            que.push({nx, ny, cs});
          } else if (cs == an[nx][ny]) {
            an[nx][ny] = cs;
          }
        } else {
          break;
        }
      }
    }
  }
  /*
  for(int i=0;i<h;i++) {
    for(int j=0;j<w;j++) {
      cout<<an[i][j]<<" ";
    }cout<<endl;
  }*/
  if (an[lx][ly] == LINF) {
    cout << -1 << endl;
  } else {
    cout << an[lx][ly] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<int, int>;
const ll M = 1e9 + 7, mx = 200000;
void add(ll &x, ll y) {
  x += y;
  x %= M;
}
void chmax(ll &x, ll y) {
  if (x < y)
    x = y;
}

const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1};
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}

struct tup {
  ll x, y, c;
  bool operator<(const tup tt) const {
    if (c != tt.c)
      return c < tt.c;
    else if (x != tt.x)
      return x < tt.x;
    else
      return y < tt.y;
  }
  bool operator>(const tup tt) const {
    if (c != tt.c)
      return c > tt.c;
    else if (x != tt.x)
      return x > tt.x;
    else
      return y > tt.y;
  }
};

#include <ctime>
#include <random>

namespace r {
random_device rd;
mt19937 mt(rd());
// mt19937 mt(time(NULL));
int nd(int max) { return mt() % max + 1; }
int nd2(int min, int max) { return mt() % (max - min + 1) + min; }
} // namespace r

int main() {
  int h, w, k;
  // h=100, w=100, k=100;
  cin >> h >> w >> k;
  int fx, fy, lx, ly;
  cin >> fx >> fy >> lx >> ly;
  fx--;
  fy--;
  lx--;
  ly--;
  // fx=0, fy=0, lx=h-1, ly=w-1;

  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];

  /*for(int i=0;i<h;i++) {
    s[i].assign('a',w);
    for(int j=0;j<w;j++) {
      if (i==fx&&j==fy||i==lx&&j==ly) s[i][j]='.';//cout<<".";
      else if (r::nd(5)==1) s[i][j]='@';
      else s[i][j]='.';
    }
  }*/

  ll an[h][w];
  fill(an[0], an[h], LINF);
  queue<tup> que;
  an[fx][fy] = 0;
  que.push({fx, fy, 0});

  while (!que.empty()) {
    tup tp = que.front();
    que.pop();
    int cs = tp.c + 1;
    for (int i = 0; i < 4; i++) {
      int nx = tp.x, ny = tp.y;
      for (int j = 0; j < k; j++) {
        nx += up[i];
        ny += lf[i];
        if (vaild(nx, ny, h, w) && s[nx][ny] == '.') {
          if (cs < an[nx][ny]) {
            an[nx][ny] = cs;
            que.push({nx, ny, cs});
          } else if (cs == an[nx][ny]) {
            an[nx][ny] = cs;
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  /*
  for(int i=0;i<h;i++) {
    for(int j=0;j<w;j++) {
      cout<<an[i][j]<<" ";
    }cout<<endl;
  }*/
  if (an[lx][ly] == LINF) {
    cout << -1 << endl;
  } else {
    cout << an[lx][ly] << endl;
  }
}