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

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {0, 1, 0, -1}, vx[] = {-1, 0, 1, 0};

int W, H, S;
string M[50], MS[10][50];
int bit[50][50], v[2][50][50][1 << 10];
int mov[50][50];

int Solve() {
  memset(v, -1, sizeof(v));
  for (int k = 0; k < S; k++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (MS[k][i][j] == '*')
          bit[j][i] |= 1 << k;
      }
    }
  }
  queue<tuple<int, int, int, int>> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      switch (M[i][j]) {
      case '|':
        mov[j][i] = 3;
        break;
      case '_':
        mov[j][i] = 1;
        break;
      case '^':
        mov[j][i] = 2;
        break;
      case 'a' ... 'j':
        mov[j][i] = 1;
        M[i][j] -= 'a';
        break;
      case 'A' ... 'J':
        mov[j][i] = 2;
        M[i][j] -= 'A';
        break;
      case '%':
        que.emplace(0, j, i, 0);
        v[0][j][i][0] = 0;
        mov[j][i] = 1;
        break;
      case '&':
        mov[j][i] = 1;
        break;
      default:
        mov[j][i] = 0;
        break;
      }
    }
  }

  while (!que.empty()) {
    int f, x, y, button;
    tie(f, x, y, button) = que.front();
    que.pop();
    if (M[y][x] == '&') {
      return (v[f][x][y][button]);
    } else if (M[y][x] == '|') {
      int nf = f ^ 1;
      if (v[nf][x][y][button] == -1) {
        v[nf][x][y][button] = v[f][x][y][button] + 1;
        que.emplace(nf, x, y, button);
      }
    } else if (M[y][x] < 10) {
      int nbutton = button ^ (1 << M[y][x]);
      int nf = (f + ((bit[x][y] >> M[y][x]) & 1)) & 1;
      if (v[nf][x][y][nbutton] == -1) {
        v[nf][x][y][nbutton] = v[f][x][y][button] + 1;
        que.emplace(nf, x, y, nbutton);
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      int isgo = (f + __builtin_popcount(bit[nx][ny] & button)) & 1;
      if (v[f][nx][ny][button] == -1 && (mov[nx][ny] & ++isgo)) {
        v[f][nx][ny][button] = v[f][x][y][button] + 1;
        que.emplace(f, nx, ny, button);
      }
    }
  }
  return (-1);
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    cin >> M[i];
  }
  cin >> S;
  for (int i = 0; i < S; i++) {
    for (int j = 0; j < H; j++) {
      cin >> MS[i][j];
    }
  }
  cout << Solve() << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {0, 1, 0, -1}, vx[] = {-1, 0, 1, 0};

int W, H, S;
string M[50], MS[10][50];

int Solve() {
  int bit[50][50] = {{}}, v[2][50][50][1 << 10];
  int move[50][50];
  memset(v, -1, sizeof(v));

  for (int k = 0; k < S; k++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (MS[k][i][j] == '*')
          bit[j][i] |= 1 << k;
      }
    }
  }
  queue<tuple<int, int, int, int>> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      switch (M[i][j]) {
      case '|':
        move[j][i] = 3;
        break;
      case '_':
        move[j][i] = 1;
        break;
      case '^':
        move[j][i] = 2;
        break;
      case 'a' ... 'j':
        move[j][i] = 1;
        M[i][j] -= 'a';
        break;
      case 'A' ... 'J':
        move[j][i] = 2;
        M[i][j] -= 'A';
        break;
      case '%':
        que.emplace(0, j, i, 0);
        v[0][j][i][0] = 0;
        move[j][i] = 1;
        break;
      case '&':
        move[j][i] = 1;
        break;
      default:
        move[j][i] = 0;
        break;
      }
    }
  }

  while (!que.empty()) {
    int f, x, y, button;
    tie(f, x, y, button) = que.front();
    que.pop();
    if (M[y][x] == '&') {
      return (v[f][x][y][button]);
    } else if (M[y][x] == '|') {
      int nf = f ^ 1;
      if (v[nf][x][y][button] == -1) {
        v[nf][x][y][button] = v[f][x][y][button] + 1;
        que.emplace(nf, x, y, button);
      }
    } else if (M[y][x] < 10) {
      int nbutton = button ^ (1 << M[y][x]);
      int nf = (f + ((bit[x][y] >> M[y][x]) & 1)) & 1;
      if (v[f][x][y][nbutton] == -1) {
        v[nf][x][y][nbutton] = v[f][x][y][button] + 1;
        que.emplace(nf, x, y, nbutton);
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      int isgo = (f + __builtin_popcount(bit[nx][ny] & button)) & 1;
      if (v[f][nx][ny][button] == -1 && (move[nx][ny] & ++isgo)) {
        v[f][nx][ny][button] = v[f][x][y][button] + 1;
        que.emplace(f, nx, ny, button);
      }
    }
  }
  return (-1);
}

int main() {
  cin >> W >> H;
  for (int i = 0; i < H; ++i) {
    cin >> M[i];
  }
  cin >> S;
  for (int i = 0; i < S; i++) {
    for (int j = 0; j < H; j++) {
      cin >> MS[i][j];
    }
  }
  cout << Solve() << endl;
}