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

//?????????????§£??????????????§IDA*?????????
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef vector<int> Board;

const int INF = INT_MAX / 3;
int dist[16][16];
Board goal(16);

int heuristic(Board &b) {
  int S = 0;
  for (int i = 0; i < 16; i++) {
    if (b[i] != 0) {
      S = S + dist[i][b[i] - 1];
    }
  }
  return S;
}

void Adj(Board &u, vector<Board> &adj) {
  int zero;
  for (int i = 0; i < 16; i++) {
    if (u[i] == 0) {
      zero = i;
    }
  }

  if (zero < 12) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero + 4]);
    adj.push_back(v);
  }
  if (zero > 3) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero - 4]);
    adj.push_back(v);
  }
  if (zero % 4 == 0 || zero % 4 == 1 || zero % 4 == 2) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero + 1]);
    adj.push_back(v);
  }
  if (zero % 4 == 1 || zero % 4 == 2 || zero % 4 == 3) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero - 1]);
    adj.push_back(v);
  }
}

bool DLS(Board &b, Board &prev, int limit, int depth) {
  if (b == goal) {
    return true;
  }
  if (depth + heuristic(b) <= limit) {
    vector<Board> adj;
    Adj(b, adj);
    for (int i = 0; i < adj.size(); i++) {
      if (prev == adj[i])
        continue;
      if (DLS(adj[i], b, limit, depth + 1) == true) {
        return true;
      }
    }
  }
  return false;
}

int IDDFS(Board &init) {
  for (int limit = 0;; limit++) {
    bool found = DLS(init, init, limit, 0);
    if (found) {
      return limit;
    }
  }
}

int main() {
  Board init(16);

  for (int i = 0; i < 16; i++) {
    cin >> init[i];
  }

  /*
  int x[16]={1,6,7,2,5,10,12,3,0,14,13,8,9,15,11,4};
  for(int i=0;i<16;i++){
          init[i]=x[i];
  }
  */

  for (int i = 0; i < 15; i++) {
    goal[i] = i + 1;
  }
  goal[15] = 0;

  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      dist[i][j] = abs((i / 4) - (j / 4)) + abs((i % 4) - (j % 4));
    }
  }

  cout << IDDFS(init) << endl;
  return 0;
}

Error: TLE

Fixed submission:

//?????????????§£??????????????§IDA*?????????
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef vector<int> Board;

const int INF = INT_MAX / 3;

int heuristic(Board &b, Board &goal) {
  int S = 0;
  for (int i = 0; i < 16; i++) {
    if (b[i] != 0) {
      for (int j = 0; j < 16; j++) {
        if (b[i] == goal[j]) {
          S = S + abs((i / 4) - (j / 4)) + abs((i % 4) - (j % 4));
          break;
        }
      }
    }
  }
  return S;
}

void Adj(Board &u, vector<Board> &adj) {
  int zero;
  for (int i = 0; i < 16; i++) {
    if (u[i] == 0) {
      zero = i;
    }
  }

  if (zero < 12) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero + 4]);
    adj.push_back(v);
  }
  if (zero > 3) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero - 4]);
    adj.push_back(v);
  }
  if (zero % 4 == 0 || zero % 4 == 1 || zero % 4 == 2) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero + 1]);
    adj.push_back(v);
  }
  if (zero % 4 == 1 || zero % 4 == 2 || zero % 4 == 3) {
    Board v(16);
    v = u;
    swap(v[zero], v[zero - 1]);
    adj.push_back(v);
  }
}

bool DLS(Board &b, Board &goal, int limit, int depth) {
  if (b == goal) {
    return true;
  }
  if (depth + heuristic(b, goal) <= limit) {
    vector<Board> adj;
    Adj(b, adj);
    for (int i = 0; i < adj.size(); i++) {
      if (DLS(adj[i], goal, limit, depth + 1) == true) {
        return true;
      }
    }
  }
  return false;
}

int IDDFS(Board &init, Board &goal) {
  for (int limit = 1;; limit++) {
    bool found = DLS(init, goal, limit, 0);
    if (found) {
      return limit;
    }
  }
}

int main() {
  Board init(16), goal(16);
  for (int i = 0; i < 16; i++) {
    cin >> init[i];
  }
  for (int i = 0; i < 15; i++) {
    goal[i] = i + 1;
  }
  goal[15] = 0;

  cout << IDDFS(init, goal) << endl;
  return 0;
}