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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>

using namespace std;

int limit, num_cup, ans;
bool Log[14348907], FLG;
char ch;
short a_loc, b_loc, c_loc;

struct Info {
  int value;
  char loc;
};

struct Data {
  Data() { count = 0; }
  Data(int arg_count, char arg_table[15]) {
    count = arg_count;
    for (int i = 0; i < 15; i++)
      table[i] = arg_table[i];
  }
  char table[15];
  int count;
};

void setCode(char table[15], int &calc) {
  int S = 1;
  calc = 0;
  for (int i = 0; i < num_cup; i++) {
    if (table[i] == 'A') {
      calc += 0;
    } else if (table[i] == 'B') {
      calc += S;
    } else {
      calc += 2 * S;
    }
    S *= 3;
  }
}

void bfs(char table[15]) {

  queue<Data> Q;
  Q.push(Data(0, table));
  char new_table[4][15];

  Data tmp;
  while (!Q.empty()) {
    tmp.count = Q.front().count;
    for (int i = 0; i < 15; i++) {
      tmp.table[i] = Q.front().table[i];
    }
    Q.pop();

    //?????????????????¢?????????????????????
    FLG = true;
    ch = tmp.table[0];

    if (ch == 'B')
      FLG = false;

    if (FLG) {
      for (int i = 1; i <= num_cup - 1; i++) {
        if (tmp.table[i] != ch) {
          FLG = false;
          break;
        }
      }
    }

    if (FLG) {
      ans = tmp.count;
      break;
    }

    if (tmp.count == limit) {
      continue; //?¬????limit+1??????????????£?????????????????§??????
    }

    for (int i = 0; i < 4; i++) {
      for (int k = 0; k < num_cup; k++)
        new_table[i][k] = tmp.table[k];
    }

    // A,B,C????????§?????´????????¢???
    a_loc = b_loc = c_loc =
        -1; //?????¨???????????????-1??§????§????????????¨???????????§OK
    for (int i = num_cup - 1; i >= 0; i--) {
      if (tmp.table[i] == 'A') {
        a_loc = (a_loc >= i) ? a_loc : i;
      } else if (tmp.table[i] == 'B') {
        b_loc = (b_loc >= i) ? b_loc : i;
      } else {
        c_loc = (c_loc >= i) ? c_loc : i;
      }
    }

    int calc;

    // A???????????????B???
    // B????????§????????§??????????????????:?§????:?±\\??´?????????????????????:?±\\??´??????????????°?????°
    if (a_loc > b_loc) {
      new_table[0][a_loc] = 'B';

      setCode(new_table[0], calc);
      if (Log[calc] == false) {
        Log[calc] = true;
        Q.push(Data(tmp.count + 1, new_table[0]));
      }
    }

    // B???????????????A???
    if (b_loc > a_loc) {
      new_table[1][b_loc] = 'A';

      setCode(new_table[1], calc);
      if (Log[calc] == false) {
        Log[calc] = true;
        Q.push(Data(tmp.count + 1, new_table[1]));
      }
    }

    // C???????????????B???
    if (c_loc > b_loc) {
      new_table[2][c_loc] = 'B';

      setCode(new_table[2], calc);
      if (Log[calc] == false) {
        Log[calc] = true;
        Q.push(Data(tmp.count + 1, new_table[2]));
      }
    }

    // B???????????????C???
    if (b_loc > c_loc) {
      new_table[3][b_loc] = 'C';

      setCode(new_table[3], calc);
      if (Log[calc] == false) {
        Log[calc] = true;
        Q.push(Data(tmp.count + 1, new_table[3]));
      }
    }
  }
}

int main() {

  int a, b, c, index, calc;
  Info info[15];
  char table[15];

  while (true) {
    scanf("%d %d", &num_cup, &limit);
    if (num_cup == 0 && limit == 0)
      break;

    for (int i = 0; i < 14348907; i++) {
      Log[i] = false;
    }

    index = 0;
    scanf("%d", &a);

    for (int i = 0; i < a; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'A';
      index++;
    }

    scanf("%d", &b);
    for (int i = 0; i < b; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'B';
      index++;
    }

    scanf("%d", &c);
    for (int i = 0; i < c; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'C';
      index++;
    }

    for (int i = 1; i <= num_cup - 1; i++) {
      for (int k = num_cup - 1; k >= i; k--) {
        if (info[k].value < info[k - 1].value)
          swap(info[k], info[k - 1]);
      }
    }

    for (int i = 0; i < num_cup; i++) {
      table[i] = info[i].loc;
    }

    //??¶??????????????????
    setCode(
        table,
        calc); // a_calc??¨c_calc?????°???????????????????????¨?????????????????????????????????

    Log[calc] = true;

    ans = -1;

    bfs(table);

    printf("%d\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <stack>
#include <stdio.h>

using namespace std;

int limit, num_cup, ans;
bool Log[14348907], FLG;
char ch;
short a_loc, b_loc, c_loc;

struct Info {
  int value;
  char loc;
};

void setCode(char table[15], int &calc) {
  int S = 1;
  calc = 0;
  for (int i = 0; i < num_cup; i++) {
    if (table[i] == 'A') {
      calc += 0;
    } else if (table[i] == 'B') {
      calc += S;
    } else {
      calc += 2 * S;
    }
    S *= 3;
  }
}

void recursive(char table[15], int count) {

  if (count > limit)
    return;
  if (ans != -1 && count >= ans)
    return;

  FLG = true;
  ch = table[0];

  if (ch == 'B')
    FLG = false;

  if (FLG) {
    for (int i = 1; i <= num_cup - 1; i++) {
      if (table[i] != ch) {
        FLG = false;
        break;
      }
    }
  }

  if (FLG) {
    if (ans == -1) {
      ans = count;
    } else {
      ans = min(ans, count);
    }
    return;
  }

  char new_table[4][15];

  for (int i = 0; i < 4; i++) {
    for (int k = 0; k < num_cup; k++)
      new_table[i][k] = table[k];
  }

  a_loc = b_loc = c_loc = -1;
  for (int i = num_cup - 1; i >= 0; i--) {
    if (table[i] == 'A') {
      a_loc = (a_loc >= i) ? a_loc : i;
    } else if (table[i] == 'B') {
      b_loc = (b_loc >= i) ? b_loc : i;
    } else {
      c_loc = (c_loc >= i) ? c_loc : i;
    }
  }

  int calc;

  if (a_loc > b_loc) {
    new_table[0][a_loc] = 'B';

    setCode(new_table[0], calc);
    if (Log[calc] == false) {
      Log[calc] = true;
      recursive(new_table[0], count + 1);
    }
  }

  if (b_loc > a_loc) {
    new_table[1][b_loc] = 'A';

    setCode(new_table[1], calc);
    if (Log[calc] == false) {
      Log[calc] = true;
      recursive(new_table[1], count + 1);
    }
  }

  if (c_loc > b_loc) {
    new_table[2][c_loc] = 'B';

    setCode(new_table[2], calc);
    if (Log[calc] == false) {
      Log[calc] = true;
      recursive(new_table[2], count + 1);
    }
  }

  if (b_loc > c_loc) {
    new_table[3][b_loc] = 'C';

    setCode(new_table[3], calc);
    if (Log[calc] == false) {
      Log[calc] = true;
      recursive(new_table[3], count + 1);
    }
  }
}

int main() {

  int a, b, c, index, calc;
  Info info[15];
  char table[15];

  while (true) {
    scanf("%d %d", &num_cup, &limit);
    if (num_cup == 0 && limit == 0)
      break;

    for (int i = 0; i < 14348907; i++) {
      Log[i] = false;
    }

    index = 0;
    scanf("%d", &a);

    for (int i = 0; i < a; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'A';
      index++;
    }

    scanf("%d", &b);
    for (int i = 0; i < b; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'B';
      index++;
    }

    scanf("%d", &c);
    for (int i = 0; i < c; i++) {
      scanf("%d", &info[index].value);
      info[index].loc = 'C';
      index++;
    }

    for (int i = 1; i <= num_cup - 1; i++) {
      for (int k = num_cup - 1; k >= i; k--) {
        if (info[k].value < info[k - 1].value)
          swap(info[k], info[k - 1]);
      }
    }

    for (int i = 0; i < num_cup; i++) {
      table[i] = info[i].loc;
    }

    setCode(table, calc);

    Log[calc] = true;

    ans = -1;

    recursive(table, 0);

    printf("%d\\n", ans);
  }

  return 0;
}