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

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

short visited[0xffffff];

inline int is_visited(unsigned s) {

  if (visited[s & 0xffffff] == -1)
    return 0;
  else
    return 1;
}

inline void set_visited(unsigned s, int n) { visited[s & 0xffffff] = n; }

inline unsigned exchange(unsigned s, unsigned p1, unsigned p2, unsigned n) {

  unsigned x = ((s >> p1) ^ (s >> p2)) & ((1U << n) - 1);
  unsigned ns = s ^ ((x << p1) | (x << p2));

  return ns;
}

int main() {

  cin.sync_with_stdio(false);

  unsigned istate = 0;

  for (int i = 0; i < 8; ++i)
    istate |= (i << (3 * i));

  queue<unsigned> q;
  q.push(istate);

  memset(visited, -1, sizeof(visited));
  set_visited(istate, 0);

  for (int n = 0;; ++n) {

    queue<unsigned> nq;

    while (!q.empty()) {

      unsigned p = q.front();
      q.pop();
      unsigned state = (p & 0xffffff);
      unsigned pos = (p >> 24);

      int bit_pos = 3 * pos;
      unsigned next_state;

      if (pos != 0 && pos != 4) {

        next_state = exchange(state, bit_pos, bit_pos - 3, 3);
        next_state |= ((pos - 1) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos != 3 && pos != 7) {

        next_state = exchange(state, bit_pos, bit_pos + 3, 3);
        next_state |= ((pos + 1) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos <= 3) {

        next_state = exchange(state, bit_pos, bit_pos + 12, 3);
        next_state |= ((pos + 4) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos >= 4) {
        next_state = exchange(state, bit_pos, bit_pos - 12, 3);
        next_state |= ((pos - 4) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }
    }

    if (nq.empty())
      break;

    q = nq;
  }

  unsigned int a[8];

  while (cin >> a[0]) {

    for (int i = 1; i < 8; ++i)
      cin >> a[i];
    unsigned state = 0;
    for (int i = 0; i < 8; ++i)
      state |= (a[i] << (3 * i));

    cout << visited[state] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int visited[0xffffff];

inline int is_visited(unsigned s) {

  if (visited[s & 0xffffff] == -1)
    return 0;
  else
    return 1;
}

inline void set_visited(unsigned s, int n) { visited[s & 0xffffff] = n; }

inline unsigned exchange(unsigned s, unsigned p1, unsigned p2, unsigned n) {

  unsigned x = ((s >> p1) ^ (s >> p2)) & ((1U << n) - 1);
  unsigned ns = s ^ ((x << p1) | (x << p2));

  return ns;
}

int main() {

  cin.sync_with_stdio(false);

  unsigned istate = 0;

  for (int i = 0; i < 8; ++i)
    istate |= (i << (3 * i));

  queue<unsigned> q;
  q.push(istate);

  memset(visited, -1, sizeof(visited));
  set_visited(istate, 0);

  for (int n = 0;; ++n) {

    queue<unsigned> nq;

    while (!q.empty()) {

      unsigned p = q.front();
      q.pop();
      unsigned state = (p & 0xffffff);
      unsigned pos = (p >> 24);

      int bit_pos = 3 * pos;
      unsigned next_state;

      if (pos != 0 && pos != 4) {

        next_state = exchange(state, bit_pos, bit_pos - 3, 3);
        next_state |= ((pos - 1) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos != 3 && pos != 7) {

        next_state = exchange(state, bit_pos, bit_pos + 3, 3);
        next_state |= ((pos + 1) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos <= 3) {

        next_state = exchange(state, bit_pos, bit_pos + 12, 3);
        next_state |= ((pos + 4) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }

      if (pos >= 4) {
        next_state = exchange(state, bit_pos, bit_pos - 12, 3);
        next_state |= ((pos - 4) << 24);

        if (!is_visited(next_state)) {
          set_visited(next_state, n + 1);
          nq.push(next_state);
        }
      }
    }

    if (nq.empty())
      break;

    q = nq;
  }

  unsigned int a[8];

  while (cin >> a[0]) {

    for (int i = 1; i < 8; ++i)
      cin >> a[i];
    unsigned state = 0;
    for (int i = 0; i < 8; ++i)
      state |= (a[i] << (3 * i));

    cout << visited[state] << endl;
  }

  return 0;
}