Problem p02246 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_T 50
#define WIDTH 21000
pair<int, vector<vector<int>>> E[MAX_T][WIDTH], D[WIDTH];
vector<vector<int>> X;
int hantei(vector<vector<int>> a) {
  int sum = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      int Num = (a[i][j] + 15) % 16;
      if (a[i][j] == 0)
        continue;
      sum += abs(i - (Num / 4)) + abs(j - (Num % 4));
    }
  }
  return sum;
}
int main() {
  for (int i = 0; i < 4; i++) {
    vector<int> FF;
    for (int j = 0; j < 4; j++) {
      int p;
      cin >> p;
      FF.push_back(p);
    }
    X.push_back(FF);
  }
  E[0][0] = make_pair(hantei(X), X);
  for (int i = 0; i < MAX_T; i++) {
    int CNT = 0;
    if (E[i][0].first == 0) {
      cout << i << endl;
      break;
    }
    for (int j = 0; j < WIDTH; j++) {
      if (E[i][j].second.size() == 0)
        break;
      int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
      vector<vector<int>> H = E[i][j].second;
      int I = E[i][j].first;
      int C = 0;
      for (int k = 0; k < 16; k++) {
        if (H[k / 4][k % 4] == 0) {
          C = k;
        }
      }
      for (int k = 0; k < 4; k++) {
        vector<vector<int>> J = H;
        int Ex = (C / 4) + dx[k], Ey = (C % 4) + dy[k];
        if (Ex < 0 || Ex > 3 || Ey < 0 || Ey > 3)
          continue;
        swap(J[C / 4][C % 4], J[Ex][Ey]);
        D[CNT] = make_pair(hantei(J), J);
        CNT++;
      }
    }
    sort(D, D + CNT);
    E[i + 1][0] = D[0];
    int cnt2 = 1;
    for (int k = 1; k < CNT; k++) {
      if (D[k - 1] == D[k])
        continue;
      E[i + 1][cnt2] = D[k];
      cnt2++;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_T 50
#define WIDTH 20000
pair<int, vector<vector<int>>> E[MAX_T][WIDTH], D[WIDTH];
vector<vector<int>> X;
int hantei(vector<vector<int>> a) {
  int sum = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      int Num = (a[i][j] + 15) % 16;
      if (a[i][j] == 0)
        continue;
      sum += abs(i - (Num / 4)) + abs(j - (Num % 4));
    }
  }
  return sum;
}
int main() {
  for (int i = 0; i < 4; i++) {
    vector<int> FF;
    for (int j = 0; j < 4; j++) {
      int p;
      cin >> p;
      FF.push_back(p);
    }
    X.push_back(FF);
  }
  E[0][0] = make_pair(hantei(X), X);
  for (int i = 0; i < MAX_T; i++) {
    int CNT = 0;
    if (E[i][0].first == 0) {
      cout << i << endl;
      break;
    }
    for (int j = 0; j < WIDTH; j++) {
      if (E[i][j].second.size() == 0)
        break;
      int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
      vector<vector<int>> H = E[i][j].second;
      int I = E[i][j].first;
      int C = 0;
      for (int k = 0; k < 16; k++) {
        if (H[k / 4][k % 4] == 0) {
          C = k;
        }
      }
      for (int k = 0; k < 4; k++) {
        vector<vector<int>> J = H;
        int Ex = (C / 4) + dx[k], Ey = (C % 4) + dy[k];
        if (Ex < 0 || Ex > 3 || Ey < 0 || Ey > 3)
          continue;
        swap(J[C / 4][C % 4], J[Ex][Ey]);
        D[CNT] = make_pair(hantei(J), J);
        CNT++;
      }
    }
    sort(D, D + CNT);
    E[i + 1][0] = D[0];
    int cnt2 = 1;
    for (int k = 1; k < CNT; k++) {
      if (D[k - 1] == D[k])
        continue;
      E[i + 1][cnt2] = D[k];
      cnt2++;
    }
  }
  return 0;
}