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

#include <cstdio>
#include <cstring>
#include <iostream>

#define maxn 36

using namespace std;

char map[6][3][3], tmp[maxn];
int hole[6][4], hpair[6][4];

void clean() {
  memset(map, 0, sizeof(map));
  memset(hole, 0, sizeof(hole));
  memset(hpair, 0, sizeof(hpair));
}

void read() {
  char tmp2[maxn];

  for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 3; j++) {
      if (i == 0 && j == 0)
        strcpy(tmp2, tmp);
      else
        scanf("%s", tmp2);
      for (int k = 0; k < 3; k++)
        map[i][j][k] = tmp2[k];
    }
  }
}

void signp() {
  hpair[0][0] = hole[0][0] & hole[2][0]; // 1-3-0
  hpair[0][1] = hole[0][1] & hole[2][3]; // 1-3-1
  hpair[0][2] = hole[0][2] & hole[2][2]; // 1-3-2
  hpair[0][3] = hole[0][3] & hole[2][1]; // 1-3-3

  hpair[1][0] = hole[1][0] & hole[3][0];
  hpair[1][1] = hole[1][1] & hole[3][3];
  hpair[1][2] = hole[1][2] & hole[3][2];
  hpair[1][3] = hole[1][3] & hole[3][1];

  hpair[4][0] = hole[4][0] & hole[5][2];
  hpair[4][1] = hole[4][1] & hole[5][1];
  hpair[4][2] = hole[4][2] & hole[5][0];
  hpair[4][3] = hole[4][3] & hole[5][3];
}

void sign() {
  for (int i = 0; i < 6; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        if (map[i][j][k] == '*') {
          if (j == 0)
            hole[i][0] = 1; // up
          if (k == 2)
            hole[i][1] = 1; // right
          if (j == 2)
            hole[i][2] = 1; // down
          if (k == 0)
            hole[i][3] = 1;
        }

  signp();
}

int chk() {
  if (hpair[0][2])
    return 0;
  if (hpair[0][1])
    return 1;
  if (hpair[0][3])
    return 1;
  if (hpair[4][0])
    return 1;
  if (hpair[4][2])
    return 1;
  if (hpair[4][1])
    return 2;
  if (hpair[4][3])
    return 2;
  if (hpair[0][0])
    return 2;
  if (hpair[1][1])
    return 2;
  if (hpair[1][3])
    return 2;
  if (hpair[1][0])
    return 3;
  if (hpair[1][2])
    return 3;

  return -1;
}

void solve() {
  clean();
  read();
  sign();
  cout << chk() << endl;
}

int main(void) {
  while (scanf("%s", tmp), tmp[0] != '#')
    solve();

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
char a[100010][8][5][5], f, e, t = 0, h[100010];
int ck_goal(int f) {
  if ((a[f][1][3][1] == 0 || a[f][1][3][2] == 0 || a[f][1][3][3] == 0) &&
      (a[f][3][3][1] == 0 || a[f][3][3][2] == 0 || a[f][3][3][3] == 0))
    return 1;
  return 0;
}
void ts(int f, int e, int u, int v) {
  int i, j;
  for (i = 1; i <= 3; i++)
    for (j = 1; j <= 3; j++)
      a[e][v][i][j] = a[f][u][4 - j][i];
}
void tn(int f, int e, int u, int v) {
  int i, j;
  for (i = 1; i <= 3; i++)
    for (j = 1; j <= 3; j++)
      a[e][v][i][j] = a[f][u][j][4 - i];
}
void od(int f, int e, int u, int v) {
  int i, j;
  for (i = 1; i <= 3; i++)
    for (j = 1; j <= 3; j++)
      a[e][v][i][j] = a[f][u][i][j];
}
void add(int f, int e, int i) {
  if (i == 1) {
    od(f, e, 1, 5);
    od(f, e, 5, 3);
    od(f, e, 3, 6);
    od(f, e, 6, 1);
    ts(f, e, 4, 4);
    ts(f, e, 2, 2);
  }
  if (i == 2) {
    tn(f, t, 5, 0);
    od(t, e, 0, 2);
    tn(f, t, 2, 0);
    od(t, e, 0, 6);
    ts(f, t, 6, 0);
    od(t, e, 0, 4);
    tn(f, t, 4, 0);
    od(t, e, 0, 5);
    ts(f, e, 1, 1);
    ts(f, e, 3, 3);
  }
  if (i == 3) {
    od(f, e, 1, 6);
    od(f, e, 6, 3);
    od(f, e, 3, 5);
    od(f, e, 5, 1);
    tn(f, e, 4, 4);
    tn(f, e, 2, 2);
  }
  if (i == 4) {
    tn(f, t, 2, 0);
    od(t, e, 0, 5);
    ts(f, t, 6, 0);
    od(t, e, 0, 2);
    ts(f, t, 4, 0);
    od(t, e, 0, 6);
    ts(f, t, 5, 0);
    od(t, e, 0, 4);
    tn(f, e, 1, 1);
    tn(f, e, 3, 3);
  }
}
int ck_re(int f) {
  int i, j, k, l;
  for (l = 1; l <= e; l++) {
    for (i = 1; i <= 6; i++)
      for (j = 1; j <= 3; j++)
        for (k = 1; k <= 3; k++)
          if (a[f][i][j][k] != a[l][i][j][k])
            goto x;
    return 0;
  x:;
  }
  return 1;
}
int main() {
  int i, j, k;
  char c;
  // freopen("test.txt","r",stdin);
  while (scanf("%c", &c) && c != '#') {
    memset(a, 0, sizeof(a));
    for (i = 1; i <= 6; i++)
      for (j = 1; j <= 3; j++) {
        for (k = 1; k <= 3; k++) {
          if (c == '.')
            a[1][i][j][k] = 1;
          c = getchar();
        }
        c = getchar();
      }
    h[1] = 0;
    if (ck_goal(1)) {
      printf("%d", h[1]);
      goto x2;
    }
    f = 0;
    e = 1;
    while (1) {
      f++;
      for (i = 1; i <= 4; i++) {
        // printf("%d\\n",e+1);
        add(f, e + 1, i);
        // for(l=1;l<=6;l++)for(j=1;j<=3;j++){for(k=1;k<=3;k++)printf("%d",a[e+1][l][j][k]);printf("\\n");}printf("\\n");//e++;
        if (ck_re(e + 1)) {
          e++;
          h[e] = h[f] + 1;
          if (ck_goal(e)) {
            printf("%d\\n", h[e]);
            goto x2;
          }
        }
      }
    }
  x2:;
  }
}