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

#include <algorithm>
#include <stdio.h>
#include <string>

const int N = 4;
const int N2 = 16;
const int LIMIT = 100;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const char dir[4] = {'u', 'r', 'd', 'l'};

int mdt[N2][N2];
int path[LIMIT];

struct Puzzle {
  int f[N2];
  int space;
  int md;
};

Puzzle state;
int limit;

bool isTarget() {
  for (int i = 0; i < N2; i++)
    if (state.f[i] != i + 1)
      return false;
  return true;
}

int getAllmd(const Puzzle &in) {
  int sum = 0;
  for (int i = 0; i < N2; i++) {
    if (in.f[i] == N2)
      continue;
    sum += mdt[i][in.f[i] - 1];
  }
  return sum;
}

bool dfs(int depth, int prev) {
  if (state.md == 0)
    return true;
  if (depth + state.md > limit)
    return false;

  int sx = state.space / N;
  int sy = state.space % N;
  Puzzle tmp;

  for (int r = 0; r < 4; r++) {
    int tx = sx + dx[r];
    int ty = sy + dy[r];
    if (tx < 0 || ty < 0 || tx >= N || ty >= N)
      continue;
    if (std::max(prev, r) - std::min(prev, r) == 2)
      continue;

    tmp = state;
    state.md -= mdt[tx * N + ty][state.f[tx * N + ty] - 1];
    state.md += mdt[sx * N + sy][state.f[tx * N + ty] - 1];
    std::swap(state.f[tx * N + ty], state.f[sx * N + sy]);
    state.space = tx * N + ty;
    if (dfs(depth + 1, r)) {
      path[depth] = r;
      return true;
    }
    state = tmp;
  }
  return false;
}

std::string iterative_deepening(Puzzle in) {
  in.md = getAllmd(in);
  for (limit = in.md; limit <= LIMIT; limit++) {
    state = in;
    if (dfs(0, -100)) {
      std::string ans;
      for (int i = 0; i < limit; i++)
        ans += dir[path[i]];
      return ans;
    }
  }

  return "unsolvable";
}

int main() {
  for (int i = 0; i < N2; i++)
    for (int j = 0; j < N2; j++)
      mdt[i][j] = abs(i / N - j / N) + abs(i % N - j % N);

  Puzzle in;

  for (int i = 0; i < N2; i++) {
    scanf("%d", &in.f[i]);
    if (in.f[i] == 0) {
      in.f[i] = N2;
      in.space = i;
    }
  }
  std::string ans = iterative_deepening(in);
  printf("%d\\n", (int)ans.length());

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int a[10][10], flag = 0, ans = 0, limit;
int b[4][2] = {-1, 0, 0, -1, 0, 1, 1, 0};
int goal[][2] = {3, 3, 0, 0, 0, 1, 0, 2, 0, 3, 1, 0, 1, 1, 1, 2,
                 1, 3, 2, 0, 2, 1, 2, 2, 2, 3, 3, 0, 3, 1, 3, 2};

int distance(int b1[10][10]) {
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (b1[i][j] != 0)
        ans += abs(i - goal[b1[i][j]][0]) + abs(j - goal[b1[i][j]][1]);
    }
  }
  return ans;
}
void dfs(int x, int y, int len, int pre) {
  int k = distance(a);
  if (k == 0) {
    flag = 1;
    ans = len;
    return;
  }
  for (int i = 0; i < 4; i++) {
    if (flag == 0 || i + pre != 3) {
      int tx = x + b[i][0];
      int ty = y + b[i][1];
      if (tx < 0 || ty < 0 || tx >= 4 || ty >= 4)
        continue;
      swap(a[tx][ty], a[x][y]);
      k = distance(a);
      if (len + k <= limit && flag == 0) {
        dfs(tx, ty, len + 1, i);
        if (flag)
          return;
      }
      swap(a[tx][ty], a[x][y]);
    }
  }
}
int main() {
  int x1, y1;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      scanf("%d", &a[i][j]);
      if (a[i][j] == 0) {
        x1 = i;
        y1 = j;
      }
    }
  }
  flag = 0;
  limit = distance(a);
  while (flag == 0 && limit <= 45) {
    dfs(x1, y1, 0, 0);
    if (flag == 0)
      limit++;
  }
  printf("%d\\n", ans);
  return 0;
}