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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll inf = 1LL << 62;

typedef pair<string, int> P;

map<string, int> ans;

void bfs(void) {
  string ini = "0123456789ab0";
  P init;
  init.first = ini;
  init.second = 0;
  queue<P> q;
  q.push(init);
  while (!q.empty()) {
    P cur;
    cur = q.front();
    q.pop();
    //	cout << cur.first << endl;
    if (cur.second >= 11)
      continue;

    if (ans.find(cur.first) == ans.end())
      ans[cur.first] = cur.second;
    else
      continue;
    int zero[2] = {-1, -1};
    rep(i, 13) {
      if (zero[0] == -1 && cur.first[i] == '0')
        zero[0] = i;
      else if (zero[1] == -1 && cur.first[i] == '0')
        zero[1] = i;
    }
    rep(i, 2) {
      string tar;
      P next;
      int check = zero[i];
      if (check % 4) {
        int d[4] = {-4, -1, 4, 1};
        rep(j, 4) {
          if (j == 0 && (check == 1 || check == 3))
            continue;
          if (j == 1 && (check == 1 || check == 9))
            continue;
          if (j == 2 && (check == 9 || check == 11))
            continue;
          if (j == 3 && (check == 3 || check == 11))
            continue;
          tar = cur.first;
          int index = check + d[j];
          index = max(index, 0);
          index = min(index, 12);
          swap(tar[check], tar[index]);
          next.first = tar;
          next.second = cur.second + 1;
          q.push(next);
        }
      } else {
        int d[4] = {2, 1, -1, -2};
        tar = cur.first;
        int index = check / 4;
        swap(tar[check], tar[check + d[index]]);
        next.first = tar;
        next.second = cur.second + 1;
        q.push(next);
      }
    }
  }
  return;
}

int bfs2(string query) {
  map<string, int> ans2;
  P init;
  init.first = query;
  init.second = 0;
  queue<P> q;
  q.push(init);
  while (!q.empty()) {
    P cur;
    cur = q.front();
    q.pop();
    //	cout << cur.first << endl;
    if (cur.second >= 11)
      continue;
    if (ans.find(cur.first) != ans.end())
      return cur.second + ans[cur.first];

    if (ans2.find(cur.first) == ans2.end())
      ans2[cur.first] = cur.second;
    else
      continue;
    int zero[2] = {-1, -1};
    rep(i, 13) {
      if (zero[0] == -1 && cur.first[i] == '0')
        zero[0] = i;
      else if (zero[1] == -1 && cur.first[i] == '0')
        zero[1] = i;
    }
    rep(i, 2) {
      string tar;
      P next;
      int check = zero[i];
      if (check % 4) {
        int d[4] = {-4, -1, 4, 1};
        rep(j, 4) {
          if (j == 0 && (check == 1 || check == 3))
            continue;
          if (j == 1 && (check == 1 || check == 9))
            continue;
          if (j == 2 && (check == 9 || check == 11))
            continue;
          if (j == 3 && (check == 3 || check == 11))
            continue;
          tar = cur.first;
          int index = check + d[j];
          index = max(index, 0);
          index = min(index, 12);
          swap(tar[check], tar[index]);
          next.first = tar;
          next.second = cur.second + 1;
          q.push(next);
        }
      } else {
        int d[4] = {2, 1, -1, -2};
        tar = cur.first;
        int index = check / 4;
        swap(tar[check], tar[check + d[index]]);
        next.first = tar;
        next.second = cur.second + 1;
        q.push(next);
      }
    }
  }
  return -1;
}

int main(void) {
  bfs();
  int input[13];
  while (cin >> input[0], input[0] != -1) {
    for (int i = 1; i < 13; i++)
      cin >> input[i];
    string query = "";
    rep(i, 13) {
      if (input[i] >= 10)
        query += ((input[i] - 10) + 'a');
      else
        query += (input[i] + '0');
    }
    int answer = bfs2(query);
    if (answer == -1)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll inf = 1LL << 62;

typedef pair<string, int> P;

map<string, int> ans;

void bfs(void) {
  string ini = "0123456789ab0";
  P init;
  init.first = ini;
  init.second = 0;
  queue<P> q;
  q.push(init);
  while (!q.empty()) {
    P cur;
    cur = q.front();
    q.pop();
    //	cout << cur.first << endl;
    if (cur.second >= 21)
      continue;

    if (ans.find(cur.first) == ans.end())
      ans[cur.first] = cur.second;
    else
      continue;
    int zero[2] = {-1, -1};
    rep(i, 13) {
      if (zero[0] == -1 && cur.first[i] == '0')
        zero[0] = i;
      else if (zero[1] == -1 && cur.first[i] == '0')
        zero[1] = i;
    }
    rep(i, 2) {
      string tar;
      P next;
      int check = zero[i];
      if (check % 4) {
        int d[4] = {-4, -1, 1, 4};
        rep(j, 4) {
          if (j == 0 && (check == 1 || check == 3))
            continue;
          if (j == 1 && (check == 1 || check == 9))
            continue;
          if (j == 2 && (check == 9 || check == 11))
            continue;
          if (j == 3 && (check == 3 || check == 11))
            continue;
          tar = cur.first;
          int index = check + d[j];
          index = max(index, 0);
          index = min(index, 12);
          swap(tar[check], tar[index]);
          next.first = tar;
          next.second = cur.second + 1;
          q.push(next);
        }
      } else {
        int d[4] = {2, 1, -1, -2};
        tar = cur.first;
        int index = check / 4;
        swap(tar[check], tar[check + d[index]]);
        next.first = tar;
        next.second = cur.second + 1;
        q.push(next);
      }
    }
  }
  return;
}

int main(void) {
  bfs();
  int input[13];
  while (cin >> input[0], input[0] != -1) {
    for (int i = 1; i < 13; i++)
      cin >> input[i];
    string query = "";
    rep(i, 13) {
      if (input[i] >= 10)
        query += ((input[i] - 10) + 'a');
      else
        query += (input[i] + '0');
    }
    if (ans.find(query) == ans.end())
      cout << "NA" << endl;
    else
      cout << ans[query] << endl;
  }
  return 0;
}