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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct S {
  int x, y, mask;
  ll hash() { return (ll)x + 15 * (ll)y + (ll)mask * (20 * 20); }
};

int b[19][15];

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

bool in(int x, int y) { return 0 <= x && x < 15 && 0 <= y && y < 19; }

map<ll, int> dp;

int main() {
  int B = 0;
  int sx = -1, sy = -1;

  memset(b, -1, sizeof b);
  for (int i = 0; i < 19; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < 15; j++) {
      if (s[j] == 'O')
        sx = j, sy = i;
      if (s[j] == 'X') {
        b[i][j] = B++;
      }
    }
  }

  S is = {sx, sy, 0};
  dp[is.hash()] = 0;

  queue<S> q;
  q.push(is);
  while (q.size()) {
    int x = q.front().x, y = q.front().y, mask = q.front().mask;
    int t = dp[q.front().hash()];
    q.pop();
    for (int k = 0; k < 8; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (!in(nx, ny))
        continue;
      int id = b[ny][nx];
      if (id == -1)
        continue;
      if (mask >> id & 1)
        continue;
      int nmask = mask;
      while (1) {
        nmask |= 1 << id;
        nx += dx[k], ny += dy[k];
        if (!in(nx, ny))
          break;
        id = b[ny][nx];
        if (id == -1 || (mask >> id & 1))
          break;
      }
      if (in(nx, ny) && ny == 18 || ny == 19) {
        printf("%d\\n", t + 1);
        return 0;
      }
      if (in(nx, ny)) {
        S ns = {nx, ny, nmask};
        if (dp.count(ns.hash()) == 0) {
          dp[ns.hash()] = t + 1;
          q.push(ns);
        }
      }
    }
  }
  printf("-1\\n");
}

Error: 0
nx : 6, ny : 19


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct S {
  int x, y, mask;
  ll hash() { return (ll)x + 15 * (ll)y + (ll)mask * (20 * 20); }
};

int b[19][15];

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

bool in(int x, int y) { return 0 <= x && x < 15 && 0 <= y && y < 19; }

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

  int B = 0;
  int sx = -1, sy = -1;
  memset(b, -1, sizeof b);
  for (int i = 0; i < 19; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < 15; j++) {
      if (s[j] == 'O')
        sx = j, sy = i;
      if (s[j] == 'X') {
        b[i][j] = B++;
      }
    }
  }

  map<ll, int> dp;
  S is = {sx, sy, 0};
  dp[is.hash()] = 0;

  queue<S> q;
  q.push(is);
  while (q.size()) {
    int x = q.front().x, y = q.front().y, mask = q.front().mask;
    int t = dp[q.front().hash()];
    q.pop();
    for (int k = 0; k < 8; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (!in(nx, ny))
        continue;
      int id = b[ny][nx];
      if (id == -1)
        continue;
      if (mask >> id & 1)
        continue;
      int nmask = mask;
      while (1) {
        nmask |= 1 << id;
        nx += dx[k], ny += dy[k];
        if (!in(nx, ny))
          break;
        id = b[ny][nx];
        if (id == -1 || (mask >> id & 1))
          break;
      }
      if (ny >= 18) {
        cerr << "nx : " << nx << ", ny : " << ny << endl;
        cout << t + 1 << endl;
        return 0;
      }
      if (in(nx, ny)) {
        S ns = {nx, ny, nmask};
        if (dp.count(ns.hash()) == 0) {
          dp[ns.hash()] = t + 1;
          q.push(ns);
        }
      }
    }
  }
  cout << -1 << endl;
}