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

#include <cmath>
#include <iostream>

using namespace std;
#define LIMIT 30

struct Puzzle {
  char cont[9][3];
  int space;
};

int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

int limit;
Puzzle puzzle;
char target[9];

int getHeuristic() {
  int sum = 0;
  for (int i = 0; i < 9; i++) {
    if (puzzle.cont[i][0] == 'E')
      continue;
    sum += (puzzle.cont[i][0] != target[i] ? 1 : 0);
  }
  return sum;
}

bool dfs(int depth, int prev) {

  int heuri = getHeuristic();

  if (heuri == 0) {
    return true;
  }
  if (depth + heuri > limit)
    return false;

  int px, py, nx, ny;
  Puzzle tmp;

  px = puzzle.space % 3;
  py = puzzle.space / 3;

  tmp = puzzle;
  for (int i = 0; i < 4; i++) {
    nx = px + dx[i];
    ny = py + dy[i];
    if (max(prev, i) - min(prev, i) == 2)
      continue;

    if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3)
      continue;

    if (i % 2) {
      // puzzle.cont[py*3+px]=rb(puzzle.cont[ny*3+nx]);
      puzzle.cont[py * 3 + px][0] = puzzle.cont[ny * 3 + nx][2];
      puzzle.cont[py * 3 + px][1] = puzzle.cont[ny * 3 + nx][1];
      puzzle.cont[py * 3 + px][2] = puzzle.cont[ny * 3 + nx][0];
    } else {
      //  puzzle.cont[py*3+px]=ra(puzzle.cont[ny*3+nx]);
      puzzle.cont[py * 3 + px][0] = puzzle.cont[ny * 3 + nx][1];
      puzzle.cont[py * 3 + px][1] = puzzle.cont[ny * 3 + nx][0];
      puzzle.cont[py * 3 + px][2] = puzzle.cont[ny * 3 + nx][2];
    }

    puzzle.space = ny * 3 + nx;
    puzzle.cont[puzzle.space][0] = 'E';

    if (dfs(depth + 1, i))
      return true;
    puzzle = tmp;
  }
  return false;
}

Puzzle in;
void solve() {
  int d;
  d = getHeuristic();
  for (limit = d; limit <= LIMIT; limit++) {

    if (dfs(0, -100)) {
      cout << limit << endl;
      return;
    }
  }
  cout << "-1" << endl;
}

int main() {
  int x, y;
  while (cin >> x >> y) {
    if (x == 0 && y == 0)
      break;
    x--;
    y--;

    puzzle.space = y * 3 + x;
    for (int i = 0; i < 9; i++) {
      cin >> target[i];
      if (i / 3 == y && i % 3 == x) {
        puzzle.cont[i][0] = 'E';
      } else {
        puzzle.cont[i][0] = 'W';
        puzzle.cont[i][1] = 'R';
        puzzle.cont[i][2] = 'B';
      }
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;
#define LIMIT 30

struct Puzzle {
  string cont[9];
  int space;
};

int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

int limit;
Puzzle puzzle;
char target[9];

int getHeuristic() {
  int sum = 0;
  for (int i = 0; i < 9; i++) {
    sum += (puzzle.cont[i][0] != target[i] ? 1 : 0);
  }
  return sum;
}

bool dfs(int depth, int prev, int heuri) {
  if (heuri == 0) {
    return true;
  }
  if (depth + heuri - 1 > limit)
    return false;

  char temp;
  int px, py, nx, ny, nheuri;
  Puzzle tmp;

  px = puzzle.space % 3;
  py = puzzle.space / 3;

  tmp = puzzle;
  for (int i = 0; i < 4; i++) {
    nheuri = heuri;
    nx = px + dx[i];
    ny = py + dy[i];
    if (max(prev, i) - min(prev, i) == 2)
      continue;

    if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3)
      continue;

    if (target[ny * 3 + nx] == puzzle.cont[ny * 3 + nx][0])
      nheuri++;
    if (target[py * 3 + px] == puzzle.cont[py * 3 + px][0])
      nheuri++;

    if (i % 2) {
      // puzzle.cont[py*3+px]=rb(puzzle.cont[ny*3+nx]);
      puzzle.cont[py * 3 + px] = puzzle.cont[ny * 3 + nx];
      temp = puzzle.cont[py * 3 + px][0];
      puzzle.cont[py * 3 + px][0] = puzzle.cont[py * 3 + px][2];
      puzzle.cont[py * 3 + px][2] = temp;
    } else {
      //  puzzle.cont[py*3+px]=ra(puzzle.cont[ny*3+nx]);
      puzzle.cont[py * 3 + px] = puzzle.cont[ny * 3 + nx];
      temp = puzzle.cont[py * 3 + px][0];
      puzzle.cont[py * 3 + px][0] = puzzle.cont[py * 3 + px][1];
      puzzle.cont[py * 3 + px][1] = temp;
    }

    puzzle.space = ny * 3 + nx;
    puzzle.cont[puzzle.space] = "EEE";

    if (target[ny * 3 + nx] == puzzle.cont[ny * 3 + nx][0])
      nheuri--;
    if (target[py * 3 + px] == puzzle.cont[py * 3 + px][0])
      nheuri--;

    if (dfs(depth + 1, i, nheuri))
      return true;
    puzzle = tmp;
  }
  return false;
}

Puzzle in;
void solve() {
  bool a, b;
  in = puzzle;

  for (limit = getHeuristic(); limit <= LIMIT; limit += 3) {
    puzzle = in;
    if (dfs(0, -100, getHeuristic())) {

      if (limit == 0) {
        cout << 0 << endl;
        return;
      }

      limit -= 2;
      puzzle = in;
      a = dfs(0, -100, getHeuristic());

      if (a == true) {
        cout << limit << endl;
        return;
      }

      limit++;
      puzzle = in;
      b = dfs(0, -100, getHeuristic());
      if (b == true) {
        cout << limit << endl;
        return;
      }

      cout << limit + 1 << endl;

      return;
    }
  }
  cout << "-1" << endl;
}

int main() {
  int x, y;
  while (cin >> x >> y) {
    if (x == 0 && y == 0)
      break;
    x--;
    y--;

    puzzle.space = y * 3 + x;
    for (int i = 0; i < 9; i++) {
      cin >> target[i];
      if (i / 3 == y && i % 3 == x)
        puzzle.cont[i] = "EEE";
      else
        puzzle.cont[i] = "WRB";
    }

    solve();
  }
  return 0;
}