Problem p00121 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
  int i;
  short a[8][8][8][8][8][8][8], b[8], p;
  short *c;
  string s = "";
  queue<pair<string, short>> q;
  memset(a, -1, sizeof(a));
  for (i = 0; i < 8; i++)
    s += i;
  q.push(make_pair(s, 0));
  while (q.empty() == 0) {
    s = q.front().first;
    p = q.front().second;
    q.pop();
    c = &a[(int)s[0]][(int)s[1]][(int)s[2]][(int)s[3]][(int)s[4]][(int)s[5]]
          [(int)s[6]];
    if (*c == -1) {
      *c = p;
      if (0) {
      } else if (s[0] == 0) {
        s[0] = s[1];
        s[1] = 0;
        q.push(make_pair(s, p + 1));
        s[1] = s[0];
        s[0] = s[4];
        s[4] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[1] == 0) {
        s[1] = s[0];
        s[0] = 0;
        q.push(make_pair(s, p + 1));
        s[0] = s[1];
        s[1] = s[2];
        s[2] = 0;
        q.push(make_pair(s, p + 1));
        s[2] = s[1];
        s[1] = s[5];
        s[5] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[2] == 0) {
        s[2] = s[1];
        s[1] = 0;
        q.push(make_pair(s, p + 1));
        s[1] = s[2];
        s[2] = s[3];
        s[3] = 0;
        q.push(make_pair(s, p + 1));
        s[3] = s[2];
        s[2] = s[6];
        s[6] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[3] == 0) {
        s[3] = s[2];
        s[2] = 0;
        q.push(make_pair(s, p + 1));
        s[2] = s[3];
        s[3] = s[7];
        s[7] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[4] == 0) {
        s[4] = s[0];
        s[0] = 0;
        q.push(make_pair(s, p + 1));
        s[0] = s[4];
        s[4] = s[5];
        s[5] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[5] == 0) {
        s[5] = s[4];
        s[4] = 0;
        q.push(make_pair(s, p + 1));
        s[4] = s[5];
        s[5] = s[6];
        s[6] = 0;
        q.push(make_pair(s, p + 1));
        s[6] = s[5];
        s[5] = s[1];
        s[1] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[6] == 0) {
        s[6] = s[5];
        s[5] = 0;
        q.push(make_pair(s, p + 1));
        s[5] = s[6];
        s[6] = s[7];
        s[7] = 0;
        q.push(make_pair(s, p + 1));
        s[7] = s[6];
        s[6] = s[2];
        s[2] = 0;
        q.push(make_pair(s, p + 1));
      } else if (s[7] == 0) {
        s[7] = s[6];
        s[6] = 0;
        q.push(make_pair(s, p + 1));
        s[6] = s[7];
        s[7] = s[3];
        s[3] = 0;
        q.push(make_pair(s, p + 1));
      }
    }
  }
  while (cin >> b[0]) {
    for (i = 1; i < 8; i++)
      cin >> b[i];
    cout << a[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]][b[6]] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
short *a;
void f(int b[], int c, int p, int g) {
  int e[8];
  short *d;
  d = &a[b[0] * 2097152 + b[1] * 262144 + b[2] * 32768 + b[3] * 4096 +
         b[4] * 512 + b[5] * 64 + b[6] * 8 + b[7]];
  if (*d == -1 || *d > c) {
    *d = c;
    memcpy(e, b, sizeof(e));
    if (g == -1) {
      e[p] = b[p + 1];
      e[p + 1] = b[p];
      f(e, c + 1, p + 1, 0);
      e[p + 1] = b[p + 1];
      e[p] = b[p + 4];
      e[p + 4] = b[p];
      f(e, c + 1, p + 4, 1);
    } else {
      switch (p) {
      case 0:
        if (g % 2) {
          e[p] = b[p + 1];
          e[p + 1] = b[p];
          f(e, c + 1, p + 1, 0);
        } else {
          e[p] = b[p + 4];
          e[p + 4] = b[p];
          f(e, c + 1, p + 4, 1);
        }
        break;
      case 1:
      case 2:
        if (g % 2) {
          e[p] = b[p - 1];
          e[p - 1] = b[p];
          f(e, c + 1, p - 1, 2);
          e[p - 1] = b[p - 1];
          e[p] = b[p + 1];
          e[p + 1] = b[p];
          f(e, c + 1, p + 1, 0);
        } else {
          if (g) {
            e[p] = b[p - 1];
            e[p - 1] = b[p];
            f(e, c + 1, p - 1, 2);
            e[p - 1] = b[p - 1];
          } else {
            e[p] = b[p + 1];
            e[p + 1] = b[p];
            f(e, c + 1, p + 1, 0);
            e[p + 1] = b[p + 1];
          }
          e[p] = b[p + 4];
          e[p + 4] = b[p];
          f(e, c + 1, p + 4, 1);
        }
        break;
      case 3:
        if (g % 2) {
          e[p] = b[p - 1];
          e[p - 1] = b[p];
          f(e, c + 1, p - 1, 2);
        } else {
          e[p] = b[p + 4];
          e[p + 4] = b[p];
          f(e, c + 1, p + 4, 1);
        }
        break;
      case 4:
        if (g % 2) {
          e[p] = b[p + 1];
          e[p + 1] = b[p];
          f(e, c + 1, p + 1, 0);
        } else {
          e[p] = b[p - 4];
          e[p - 4] = b[p];
          f(e, c + 1, p - 4, 3);
        }
        break;
      case 5:
      case 6:
        if (g % 2) {
          e[p] = b[p - 1];
          e[p - 1] = b[p];
          f(e, c + 1, p - 1, 2);
          e[p - 1] = b[p - 1];
          e[p] = b[p + 1];
          e[p + 1] = b[p];
          f(e, c + 1, p + 1, 0);
        } else {
          if (g) {
            e[p] = b[p - 1];
            e[p - 1] = b[p];
            f(e, c + 1, p - 1, 2);
            e[p - 1] = b[p - 1];
          } else {
            e[p] = b[p + 1];
            e[p + 1] = b[p];
            f(e, c + 1, p + 1, 0);
            e[p + 1] = b[p + 1];
          }
          e[p] = b[p - 4];
          e[p - 4] = b[p];
          f(e, c + 1, p - 4, 3);
        }
        break;
      case 7:
        if (g % 2) {
          e[p] = b[p - 1];
          e[p - 1] = b[p];
          f(e, c + 1, p - 1, 2);
        } else {
          e[p] = b[p - 4];
          e[p - 4] = b[p];
          f(e, c + 1, p - 4, 3);
        }
        break;
      }
    }
  }
  return;
}
int main() {
  int i;
  int b[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  a = new short[16777216];
  memset(a, -1, sizeof(short) * 16777216);
  f(b, 0, 0, -1);
  for (;;) {
    b[0] = -1;
    cin >> b[0];
    if (b[0] == -1)
      break;
    for (i = 1; i < 8; i++)
      cin >> b[i];
    cout << (int)a[b[0] * 2097152 + b[1] * 262144 + b[2] * 32768 + b[3] * 4096 +
                   b[4] * 512 + b[5] * 64 + b[6] * 8 + b[7]]
         << endl;
  }
  delete[] a;
  return 0;
}