Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>

#define reps(i, f, n) for (int i = (f); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

const int INF = 1111111111;
const double EPS = 1e-9;

using namespace std;

int h, w, k;
int sx, sy, gx, gy;
vector<string> c;

void init() {
  cin >> h >> w >> k;
  cin >> sy >> sx >> gy >> gx;
  rep(i, h) {
    string s;
    cin >> s;
    c.push_back(s);
  }

  sx--;
  sy--;
  gx--;
  gy--;
}

void solve() {
  queue<piii> que;
  que.push(piii(0, pii(sx, sy)));

  int ans = -1;

  while (!que.empty()) {
    piii top = que.front();
    que.pop();

    int cost = top.first;
    int x = top.second.first;
    int y = top.second.second;

    if (c[y][x] == 'o') {
      continue;
    }
    c[y][x] = 'o';

    // cout << "cha " << cost << " " << x << " " << y << endl;
    // rep(i, h) cout << c[i] << endl;

    if (x == gx && y == gy) {
      ans = cost;
      break;
    }

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

    rep(i, 4) {
      reps(dist, 1, k + 1) {
        int nx = x + dist * dx[i];
        int ny = y + dist * dy[i];

        if (nx < 0 || nx >= w | ny < 0 || ny >= h) {
          break;
        }

        if (c[ny][nx] == '@') {
          break;
        }

        if (c[ny][nx] != '.') {
          continue;
        }

        c[ny][nx] = 'x';
        que.push(piii(cost + 1, pii(nx, ny)));
      }
    }
  }

  cout << ans << endl;
}

int main() {
  init();
  solve();
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>

#define reps(i, f, n) for (int i = (f); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

const int INF = 1111111111;
const double EPS = 1e-9;

using namespace std;

int h, w, k;
int sx, sy, gx, gy;
vector<string> c;

void init() {
  cin >> h >> w >> k;
  cin >> sy >> sx >> gy >> gx;
  rep(i, h) {
    string s;
    cin >> s;
    c.push_back(s);
  }

  sx--;
  sy--;
  gx--;
  gy--;
}

void solve() {
  queue<piii> que;
  que.push(piii(0, pii(sx, sy)));

  int ans = -1;

  while (!que.empty()) {
    piii top = que.front();
    que.pop();

    int cost = top.first;
    int x = top.second.first;
    int y = top.second.second;

    if (c[y][x] == 'o') {
      continue;
    }
    c[y][x] = 'o';

    // cout << "cha " << cost << " " << x << " " << y << endl;
    // rep(i, h) cout << c[i] << endl;

    if (x == gx && y == gy) {
      ans = cost;
      break;
    }

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

    rep(i, 4) {
      reps(dist, 1, k + 1) {
        int nx = x + dist * dx[i];
        int ny = y + dist * dy[i];

        if (nx < 0 || nx >= w | ny < 0 || ny >= h) {
          break;
        }

        if (c[ny][nx] == '@' || c[ny][nx] == 'o') {
          break;
        }

        if (c[ny][nx] != '.') {
          continue;
        }

        c[ny][nx] = 'x';
        que.push(piii(cost + 1, pii(nx, ny)));
      }
    }
  }

  cout << ans << endl;
}

int main() {
  init();
  solve();
}