Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h, w, k, x1, x2, y1, y2;
char s[1000001];
P d[1000000][4];
priority_queue<PP, vector<PP>, greater<PP>> q;

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < h; i++)
    scanf("%s", &s[i * w]);

  for (int i = 0; i < h * w; i++)
    fill(d[i], d[i] + 4, P((int)1e9, 0));

  for (int i = 0; i < 4; i++) {
    int a = x1 + dx[i], b = y1 + dy[i];
    if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
      d[a * w + b][i] = P(1, 1);
      q.emplace(P(1, 1), 10000000 * i + a * w + b);
    }
  }

  while (!q.empty()) {
    int z = q.top().second;
    int x = z % 10000000;
    z /= 10000000;
    int y = x % w;
    x /= w;
    P p = q.top().first;
    q.pop();
    if (d[x * w + y][z] != p)
      continue;
    for (int i = 0; i < 4; i++) {
      int a = x + dx[i], b = y + dy[i];
      if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
        P tmp = p;
        if (i != z || tmp.second == k) {
          tmp.second = 1;
          tmp.first++;
        } else
          tmp.second++;
        if (tmp >= d[a * w + b][i])
          continue;
        d[a * w + b][i] = tmp;
        q.emplace(tmp, 10000000 * i + a * w + b);
      }
    }
  }

  int ans = 1e9;
  for (int i = 0; i < 4; i++)
    ans = min(ans, d[x2 * w + y2][i].first);
  printf("%d\\n", ans == 1e9 ? -1 : ans);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h, w, k, x1, x2, y1, y2;
char s[100001];
P d[100000][4];
priority_queue<PP, vector<PP>, greater<PP>> q;

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < h; i++)
    scanf("%s", &s[i * w]);

  for (int i = 0; i < h * w; i++)
    fill(d[i], d[i] + 4, P((int)1e9, 0));

  for (int i = 0; i < 4; i++) {
    int a = x1 + dx[i], b = y1 + dy[i];
    if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
      d[a * w + b][i] = P(1, 1);
      q.emplace(P(1, 1), 10000000 * i + a * w + b);
    }
  }

  while (q.empty()) {
    int z = q.top().second;
    int x = z % 10000000;
    z /= 10000000;
    int y = x % w;
    x /= w;
    P p = q.top().first;
    q.pop();
    if (d[x * w + y][z] != p)
      continue;
    for (int i = 0; i < 4; i++) {
      int a = x + dx[i], b = y + dy[i];
      if (0 <= a && a < h && 0 <= b && b < w && s[a * w + b] == '.') {
        P tmp = p;
        if (i != z || tmp.second == k) {
          tmp.second = 1;
          tmp.first++;
        } else
          tmp.second++;
        if (tmp >= d[a * w + b][i])
          continue;
        d[a * w + b][i] = tmp;
        q.emplace(tmp, 10000000 * i + a * w + b);
      }
    }
  }

  int ans = 1e9;
  for (int i = 0; i < 4; i++)
    ans = min(ans, d[x2 * w + y2][i].first);
  printf("%d\\n", ans == 1e9 ? -1 : ans);
  return 0;
}