Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(__I, __N) for (int __I = 0; __I < __N; ++__I)

typedef long long int64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;
typedef double db;
typedef long double ldb;

const int INF = 0x3f3f3f3f;
const int64 INF64 = 0x3f3f3f3f3f3f3f3fLL;

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

const int MOD = 1000000007;

const int MAX_H = 1000000;
const int MAX_W = 1000000;

map<int, bool> maze[MAX_W];
map<int, int> dist[MAX_W];

int H, W, K;
pii que[5 * MAX_H];

int solve(int x1, int y1, int x2, int y2) {
  int qs = 0, qt = 0;
  FOR(i, H) FOR(j, W) dist[i][j] = INF;

  que[qt++] = pii(x1, y1);
  dist[x1][y1] = 0;

  while (qs < qt) {
    pii p = que[qs++];
    int x = p.first, y = p.second;
    int d = dist[x][y];
    if (x == x2 && y == y2)
      return d;

    FOR(k, 4) {
      int xx = x, yy = y;
      FOR(step, K) {
        xx += dir[k][0];
        yy += dir[k][1];

        if (xx < 0 || xx > H)
          break;
        if (yy < 0 || yy > W)
          break;
        if (maze[xx][yy])
          break;
        int &dd = dist[xx][yy];
        if (dd > d + 1) {
          dd = d + 1;
          que[qt++] = pii(xx, yy);
        }
        if (dd + 1 <= d) {
          break;
        }
      }
    }
  }

  return -1;
}

char buff[MAX_H + 1];

int main() {
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1;
  --x2, --y2;
  FOR(i, H) {
    scanf(" %s", buff);
    FOR(j, W) maze[i][j] = buff[j] == '@';
  }

  int ans = solve(x1, y1, x2, y2);
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(__I, __N) for (int __I = 0; __I < __N; ++__I)

typedef long long int64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;
typedef double db;
typedef long double ldb;

const int INF = 0x3f3f3f3f;
const int64 INF64 = 0x3f3f3f3f3f3f3f3fLL;

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

const int MOD = 1000000007;

const int MAX_H = 1000000;
const int MAX_W = 1000000;

map<int, bool> maze[MAX_W];
map<int, int> dist[MAX_W];

int H, W, K;
pii que[5 * MAX_H];

int solve(int x1, int y1, int x2, int y2) {
  int qs = 0, qt = 0;
  FOR(i, H) FOR(j, W) dist[i][j] = INF;

  que[qt++] = pii(x1, y1);
  dist[x1][y1] = 0;

  while (qs < qt) {
    pii p = que[qs++];
    int x = p.first, y = p.second;
    int d = dist[x][y];
    if (x == x2 && y == y2)
      return d;

    FOR(k, 4) {
      int xx = x, yy = y;
      FOR(step, K) {
        xx += dir[k][0];
        yy += dir[k][1];

        if (xx < 0 || xx > H)
          break;
        if (yy < 0 || yy > W)
          break;
        if (maze[xx][yy])
          break;
        int &dd = dist[xx][yy];
        if (dd > d + 1) {
          dd = d + 1;
          que[qt++] = pii(xx, yy);
        }

        if (dd <= d) {
          break;
        }

        if (dd + 1 <= d) {
          break;
        }
      }
    }
  }

  return -1;
}

char buff[MAX_H + 1];

int main() {
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1;
  --x2, --y2;
  FOR(i, H) {
    scanf(" %s", buff);
    FOR(j, W) maze[i][j] = buff[j] == '@';
  }

  int ans = solve(x1, y1, x2, y2);
  printf("%d\n", ans);
}