Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "vector"
using namespace std;
#define MAXN 1000006
// #define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define mem(a) memset(a, 0, sizeof a)
typedef long long ll;
#define P 1000000007
int n, m, k;
int sx, sy, xx, yy;
char ch[MAXN];
int mm[MAXN], vis[MAXN], dis[MAXN];
int getid(int x, int y) { return (x - 1) * m + y; }

int dirx[] = {0, 1, -1, 0};
int diry[] = {1, 0, 0, -1};

queue<pii> Q;
void bfs() {
  memset(dis, 0x3f, sizeof dis);
  Q.push(mp(sx, sy));
  dis[getid(sx, sy)] = 0;
  while (!Q.empty()) {
    auto u = Q.front();
    Q.pop();
    int x = u.fi, y = u.se, xx, yy;
    //        cout << x << ' ' << y << ' ' << dis[getid( x , y )] << endl;
    for (int i = 0; i < 4; ++i) {
      xx = x, yy = y;
      for (int j = 0; j < k; ++j) {
        xx = xx + dirx[i], yy = yy + diry[i];
        int idd = getid(xx, yy);
        if (xx < 1 || yy < 1 || xx > n || yy > m || mm[idd] ||
            (vis[idd] && dis[getid(x, y)] + 1 > dis[idd]))
          break;
        else {
          Q.push(mp(xx, yy)), vis[idd] = 1,
                              dis[idd] = min(dis[idd], dis[getid(x, y)] + 1);
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> xx >> yy;
  rep(i, 1, n) {
    scanf("%s", ch + 1);
    rep(j, 1, m) mm[getid(i, j)] = (ch[j] == '@' ? 1 : 0);
  }
  bfs();
  if (dis[getid(xx, yy)] > 1e9)
    return puts("-1"), void();
  else
    printf("%d\n", dis[getid(xx, yy)]);
}

signed main() {
  //    int T;cin >> T;while( T-- ) solve();
  solve();
}


Fixed submission:
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "vector"
using namespace std;
#define MAXN 1000006
// #define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define mem(a) memset(a, 0, sizeof a)
typedef long long ll;
#define P 1000000007
int n, m, k;
int sx, sy, xx, yy;
char ch[MAXN];
int mm[MAXN], vis[MAXN], dis[MAXN];
int getid(int x, int y) { return (x - 1) * m + y; }

int dirx[] = {0, 1, -1, 0};
int diry[] = {1, 0, 0, -1};

queue<pii> Q;
void bfs() {
  memset(dis, 0x3f, sizeof dis);
  Q.push(mp(sx, sy));
  dis[getid(sx, sy)] = 0;
  while (!Q.empty()) {
    auto u = Q.front();
    Q.pop();
    int x = u.fi, y = u.se, xx, yy;
    //        cout << x << ' ' << y << ' ' << dis[getid( x , y )] << endl;
    for (int i = 0; i < 4; ++i) {
      xx = x, yy = y;
      for (int j = 0; j < k; ++j) {
        xx = xx + dirx[i], yy = yy + diry[i];
        int idd = getid(xx, yy);
        if (xx < 1 || yy < 1 || xx > n || yy > m || mm[idd] ||
            (vis[idd] && dis[getid(x, y)] + 1 > dis[idd]))
          break;
        else {
          if (!vis[idd])
            Q.push(mp(xx, yy));
          vis[idd] = 1, dis[idd] = min(dis[idd], dis[getid(x, y)] + 1);
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> xx >> yy;
  rep(i, 1, n) {
    scanf("%s", ch + 1);
    rep(j, 1, m) mm[getid(i, j)] = (ch[j] == '@' ? 1 : 0);
  }
  bfs();
  if (dis[getid(xx, yy)] > 1e9)
    return puts("-1"), void();
  else
    printf("%d\n", dis[getid(xx, yy)]);
}

signed main() {
  //    int T;cin >> T;while( T-- ) solve();
  solve();
}