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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e9
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> S;

int h, w, k, m;
int sx, sy, tx, ty;
vector<int> vec;
char c[1500005];
int dist[1500005][4][25];
int sum[1500005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int get(int sx, int sy, int tx, int ty) {
  int ret = sum[ty * w + tx];
  if (sy >= 1)
    ret -= sum[(sy - 1) * w + tx];
  if (sx >= 1)
    ret -= sum[ty * w + (sx - 1)];
  if (sx >= 1 && sy >= 1)
    ret += sum[(sy - 1) * w + (sx - 1)];
  return ret;
}

bool check(int x, int y, int d, int k) {
  int nx = x + dx[d] * k, ny = y + dy[d] * k;
  if (x > nx)
    swap(x, nx);
  if (y > ny)
    swap(y, ny);
  return get(x, y, nx, ny) == 0;
}

deque<S> deq;
void add(int x, int y, int d, int s, int nx, int ny, int nd, int ns, int c) {
  if (dist[ny * w + nx][nd][ns] > dist[y * w + x][d][s]) {
    dist[ny * w + nx][nd][ns] = dist[y * w + x][d][s] + c;
    if (c == 0)
      deq.push_front(S(P(nx, ny), P(nd, ns)));
    else
      deq.push_back(S(P(nx, ny), P(nd, ns)));
  }
}

void bfs() {
  deq.push_front(S(P(sx, sy), P(0, 0)));

  for (int i = 0; i < h * w; i++) {
    for (int j = 0; j <= m; j++) {
      for (int d = 0; d < 4; d++) {
        dist[i][d][j] = inf;
      }
    }
  }
  dist[sy * w + sx][0][0] = 0;

  llint x, y, d, s;
  while (deq.size()) {
    x = deq.front().first.first, y = deq.front().first.second;
    d = deq.front().second.first, s = deq.front().second.second;
    deq.pop_front();
    // cout << x << " " << y << " "<< d << " " <<s << endl;
    if (s == 0) {
      for (int i = 0; i < 4; i++) {
        add(x, y, d, s, x, y, i, 1, 0);
      }
    } else {
      int nd = d, ns = s + 1, c = 0;
      if (s == m)
        nd = 0, ns = 0, c = 1;
      add(x, y, d, s, x, y, nd, ns, c);
      if (check(x, y, d, vec[s - 1])) {
        int nx = x + dx[d] * vec[s - 1], ny = y + dy[d] * vec[s - 1];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        add(x, y, d, s, nx, ny, nd, ns, c);
      }
    }
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w >> k;
  cin >> sy >> sx >> ty >> tx;
  sx--, sy--, tx--, ty--;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      cin >> c[y * w + x];
    }
  }

  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (y >= 1)
        sum[y * w + x] += sum[(y - 1) * w + x];
      if (x >= 1)
        sum[y * w + x] += sum[y * w + (x - 1)];
      if (x >= 1 && y >= 1)
        sum[y * w + x] -= sum[(y - 1) * w + (x - 1)];
      if (c[y * w + x] == '@')
        sum[y * w + x]++;
    }
  }

  llint rem = k;
  for (int j = 0;; j++) {
    if (rem >= (1 << j)) {
      vec.push_back(1 << j);
      rem -= (1 << j);
    } else
      break;
  }
  if (rem > 0)
    vec.push_back(rem);
  m = vec.size();

  bfs();

  int ans = inf;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[ty * w + tx][i][0]);
  if (ans > inf / 2)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e9
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> S;

int h, w, k, m;
int sx, sy, tx, ty;
vector<int> vec;
char c[2200005];
int dist[2200005][4][25];
int sum[2200005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int get(int sx, int sy, int tx, int ty) {
  return sum[ty * w + tx] - sum[(sy - 1) * w + tx] - sum[ty * w + (sx - 1)] +
         sum[(sy - 1) * w + (sx - 1)];
}

bool check(int x, int y, int d, int k) {
  int nx = x + dx[d] * k, ny = y + dy[d] * k;
  if (x > nx)
    swap(x, nx);
  if (y > ny)
    swap(y, ny);
  return get(x, y, nx, ny) == 0;
}

deque<S> deq;
void add(int x, int y, int d, int s, int nx, int ny, int nd, int ns, int c) {
  if (dist[ny * w + nx][nd][ns] > dist[y * w + x][d][s]) {
    dist[ny * w + nx][nd][ns] = dist[y * w + x][d][s] + c;
    if (c == 0)
      deq.push_front(S(P(nx, ny), P(nd, ns)));
    else
      deq.push_back(S(P(nx, ny), P(nd, ns)));
  }
}

void bfs() {
  deq.push_front(S(P(sx, sy), P(0, 0)));

  for (int i = 1; i <= (h + 1) * (w + 1); i++) {
    for (int j = 0; j <= m; j++) {
      for (int d = 0; d < 4; d++) {
        dist[i][d][j] = inf;
      }
    }
  }
  dist[sy * w + sx][0][0] = 0;

  llint x, y, d, s;
  while (deq.size()) {
    x = deq.front().first.first, y = deq.front().first.second;
    d = deq.front().second.first, s = deq.front().second.second;
    deq.pop_front();
    // cout << x << " " << y << " "<< d << " " <<s << endl;
    if (s == 0) {
      for (int i = 0; i < 4; i++) {
        add(x, y, d, s, x, y, i, 1, 0);
      }
    } else {
      int nd = d, ns = s + 1, c = 0;
      if (s == m)
        nd = 0, ns = 0, c = 1;
      add(x, y, d, s, x, y, nd, ns, c);
      if (check(x, y, d, vec[s - 1])) {
        int nx = x + dx[d] * vec[s - 1], ny = y + dy[d] * vec[s - 1];
        if (nx <= 0 || nx > w || ny <= 0 || ny > h)
          continue;
        add(x, y, d, s, nx, ny, nd, ns, c);
      }
    }
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w >> k;
  cin >> sy >> sx >> ty >> tx;
  // sx--, sy--, tx--, ty--;
  for (int y = 1; y <= h; y++) {
    for (int x = 1; x <= w; x++) {
      cin >> c[y * w + x];
    }
  }

  for (int y = 1; y <= h; y++) {
    for (int x = 1; x <= w; x++) {
      sum[y * w + x] = sum[(y - 1) * w + x] + sum[y * w + (x - 1)] -
                       sum[(y - 1) * w + (x - 1)];
      if (c[y * w + x] == '@')
        sum[y * w + x]++;
    }
  }

  llint rem = k;
  for (int j = 0;; j++) {
    if (rem >= (1 << j)) {
      vec.push_back(1 << j);
      rem -= (1 << j);
    } else
      break;
  }
  if (rem > 0)
    vec.push_back(rem);
  m = vec.size();

  bfs();

  int ans = inf;
  for (int i = 0; i < 4; i++)
    ans = min(ans, dist[ty * w + tx][i][0]);
  if (ans > inf / 2)
    ans = -1;
  cout << ans << endl;

  return 0;
}