Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w, k;
int xs, ys, xg, yg;
vector<vector<char>> c;

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

void solve() {
  cin >> h >> w >> k;
  cin >> xs >> ys >> xg >> yg;
  --xs, --ys, --xg, --yg;
  c.assign(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  queue<P> que;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  que.push(P{xs, ys});
  dist[xs][ys] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fr, y = p.sc;
    rep(i, 4) {
      int nx = x, ny = y;
      for (int j = 0; j < k; ++j) {
        nx += dx[i], ny += dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
          if (chmin(dist[nx][ny], dist[x][y] + 1)) {
            que.push(P{nx, ny});
          }
        } else {
          break;
        }
      }
    }
  }
  if (dist[xg][yg] == INF) {
    cout << -1 << endl;
    return;
  }
  cout << dist[xg][yg] << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}

Fixed submission:
/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w, k;
int xs, ys, xg, yg;
vector<vector<char>> c;

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

void solve() {
  cin >> h >> w >> k;
  cin >> xs >> ys >> xg >> yg;
  --xs, --ys, --xg, --yg;
  c.assign(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  queue<P> que;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  que.push(P{xs, ys});
  dist[xs][ys] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fr, y = p.sc;
    rep(i, 4) {
      int nx = x, ny = y;
      for (int j = 0; j < k; ++j) {
        nx += dx[i], ny += dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
          if (chmin(dist[nx][ny], dist[x][y] + 1)) {
            que.push(P{nx, ny});
          } else if (dist[nx][ny] < dist[x][y] + 1) {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dist[xg][yg] == INF) {
    cout << -1 << endl;
    return;
  }
  cout << dist[xg][yg] << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}