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

#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
using namespace std;
struct B {
  int z[2];
  char s[14];
  bool operator<(const B &r) const { return strcmp(s, r.s) < 0; }
};
int main() {
  int a[13][5] =
      {
          {2, -1},  {2, 5, -1},       {0, 1, 3, 6, -1},   {2, 7, -1},
          {5, -1},  {1, 4, 6, 9, -1}, {2, 5, 7, 10, -1},  {3, 6, 8, 11, -1},
          {7, -1},  {5, 10, -1},      {6, 9, 11, 12, -1}, {7, 10, -1},
          {10, -1},
      },
      gx[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2},
      gy[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4}, v, d, s, i, j;
  B g = {0, 0, "0123456789:;0"};
  for (;;) {
    map<B, int> m;
    deque<B> q;
    B b = {-1, -1};
    for (i = 0; i < 13; i++) {
      cin >> v;
      if (v == -1)
        return 0;
      b.s[i] = '0' + v;
      if (!v)
        b.z[b.z[0] != -1] = i;
    }
    b.s[13] = 0;
    m[b] = 1;
    for (q.push_back(b); !q.empty(); q.pop_front()) {
      B &b = q.front();
      if (!strcmp(b.s, g.s))
        break;
      d = 0;
      for (i = 13; i--;) {
        v = b.s[i] - '0';
        if (v)
          d += abs(gx[v] - gx[i]) + abs(gy[v] - gy[i]);
      }
      s = m[b];
      if (s + d < 22)
        for (j = 2; j--;)
          for (i = 0; ~a[b.z[j]][i]; i++) {
            B n = b;
            n.z[j] = a[b.z[j]][i];
            if (n.z[j] - n.z[1 - j]) {
              swap(n.s[b.z[j]], n.s[n.z[j]]);
              int &t = m[n];
              if (!t) {
                t = s + 1;
                q.push_back(n);
              }
            }
          }
    }
    m[g] ? cout << m[g] - 1 << endl : cout << "NA\\n";
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
using namespace std;
struct board {
  char s[14];
  int z1, z2;
  bool operator<(const board &rhs) const { return strcmp(s, rhs.s) < 0; }
};
int main() {
  int d[13][5] = {
      {2, -1},            // 0
      {2, 5, -1},         // 1
      {0, 1, 3, 6, -1},   // 2
      {2, 7, -1},         // 3
      {5, -1},            // 4
      {1, 4, 6, 9, -1},   // 5
      {2, 5, 7, 10, -1},  // 6
      {3, 6, 8, 11, -1},  // 7
      {7, -1},            // 8
      {5, 10, -1},        // 9
      {6, 9, 11, 12, -1}, // 10
      {7, 10, -1},        // 11
      {10, -1},           // 12
  };
  int gx[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
  int gy[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};
  board goal;
  strcpy(goal.s, "0123456789:;0");
  for (;;) {
    typedef map<board, int> SM;
    SM stepmap;
    deque<board> que;
    board b0;
    b0.z1 = b0.z2 = -1;
    for (int i = 0; i < 13; i++) {
      int v;
      cin >> v;
      if (v == -1)
        return 0;
      b0.s[i] = '0' + v;
      if (v == 0) {
        if (b0.z1 == -1)
          b0.z1 = i;
        else
          b0.z2 = i;
      }
    }
    b0.s[13] = 0;
    que.push_back(b0);
    stepmap[b0] = 1;
    while (!que.empty()) {
      board b = que.front();
      if (strcmp(b.s, goal.s) == 0) {
        break;
      }
      que.pop_front();
      int dist = 0;
      for (int i = 0; i < 13; i++) {
        int v = b.s[i] - '0';
        if (v) {
          dist += abs(gx[v] - gx[i]) + abs(gy[v] - gy[i]);
        }
      }
      if (dist <= 20) {
        int step = stepmap[b];
        if (step <= 20) {
          for (int i = 0; d[b.z1][i] != -1; i++) {
            board bn = b;
            bn.z1 = d[b.z1][i];
            if (bn.z1 != bn.z2) {
              swap(bn.s[b.z1], bn.s[bn.z1]);
              int &ns = stepmap[bn];
              if (!ns) {
                ns = step + 1;
                que.push_back(bn);
              }
            }
          }
          for (int i = 0; d[b.z2][i] != -1; i++) {
            board bn = b;
            bn.z2 = d[b.z2][i];
            if (bn.z2 != bn.z1) {
              swap(bn.s[b.z2], bn.s[bn.z2]);
              int &ns = stepmap[bn];
              if (!ns) {
                ns = step + 1;
                que.push_back(bn);
              }
            }
          }
        }
      }
    }
    if (stepmap[goal] == 0)
      cout << "NA " << endl;
    else
      cout << stepmap[goal] - 1 << endl;
  }
}