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


#define MOD 1000007

#include <cmath>
#include <cstring>
#include <iostream>
#include <set>

using namespace std;

const int AIM = 87654321;
const int inic[] = {2, 0, 1, 2, 0, 1, 2, 0, 1};
const int inir[] = {2, 0, 0, 0, 1, 1, 1, 2, 2};
const int incr[] = {-1, 0, 1, 0};
const int incc[] = {0, -1, 0, 1};

int gragh[3][3];
int zeror, zeroc;
int dis(0);

set<int> hash;

int maxdep(0);

inline int trans() {
  int mpow(1);
  int data(0);
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) {
      data += mpow * gragh[i][j];
      mpow *= 10;
    }
  return data;
}
inline int calu() {
  int ans(0);
  for (int i = 0; i <= 2; ++i)
    for (int j = 0; j <= 2; ++j)
      ans += abs(inir[gragh[i][j]] - i) + abs(inic[gragh[i][j]] - j);
  return ans;
}
inline int swap(int &a, int &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
bool dfs(int dep) {
  if (trans() == AIM) {
    cout << maxdep << endl;
    return 1;
  }
  if (dep >= maxdep)
    return false;
  if (dis > (maxdep - dep) * 2)
    return false;
  int nr, nc;
  for (int i = 0; i < 4; ++i) {
    nr = zeror + incr[i];
    nc = zeroc + incc[i];
    if (nr < 0 || nc < 0 || nr > 2 || nc > 2)
      continue;
    int olddis = dis;
    swap(nr, zeror);
    swap(nc, zeroc);
    swap(gragh[nr][nc], gragh[zeror][zeroc]);
    int data = trans();
    dis = calu();
    if (!hash.count(data)) {
      hash.insert(data);
      if (dfs(dep + 1))
        return 1;
      hash.erase(data);
    }
    dis = olddis;
    swap(nr, zeror);
    swap(nc, zeroc);
    swap(gragh[nr][nc], gragh[zeror][zeroc]);
  }
  return false;
}

int main() {
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) {
      cin >> gragh[i][j];
      if (!gragh[i][j])
        zeror = i, zeroc = j;
    }
  dis = calu();
  while (!dfs(0)) {
    hash.clear();
    hash.insert(trans());
    ++maxdep;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 10 + 2
int map[MAXN][MAXN];
int min0 = 1000000000;
int t;
int map1[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
bool hx[40000009];
bool pd() {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (map[i][j] != map1[i][j])
        return false;
    }
  }
  return true;
}
void syf(int x, int y, int tot) {
  if (pd()) {
    if (tot < min0)
      min0 = tot;
    return;
  } else if (tot > 26)
    return;
  else {
    int g = 1;
    if (x + 1 < 3) {
      t = map[x][y];
      map[x][y] = map[x + 1][y];
      map[x + 1][y] = t;
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          g = g * 10 + map[i][j];
          g %= 40000007;
        }
      }
      if (!hx[g]) {
        hx[g] = 1;
        syf(x + 1, y, tot + 1);
        hx[g] = 0;
      }
      t = map[x + 1][y];
      map[x + 1][y] = map[x][y];
      map[x][y] = t;
    }
    g = 1;
    if (x - 1 >= 0) {
      t = map[x][y];
      map[x][y] = map[x - 1][y];
      map[x - 1][y] = t;
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          g = g * 10 + map[i][j];
          g %= 40000007;
        }
      }
      if (!hx[g]) {
        hx[g] = 1;
        syf(x - 1, y, tot + 1);
        hx[g] = 0;
      }
      t = map[x - 1][y];
      map[x - 1][y] = map[x][y];
      map[x][y] = t;
    }
    g = 1;
    if (y + 1 < 3) {
      t = map[x][y + 1];
      map[x][y + 1] = map[x][y];
      map[x][y] = t;
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          g = g * 10 + map[i][j];
          g %= 40000007;
        }
      }
      if (!hx[g]) {
        hx[g] = 1;
        syf(x, y + 1, tot + 1);
        hx[g] = 0;
      }
      t = map[x][y + 1];
      map[x][y + 1] = map[x][y];
      map[x][y] = t;
    }
    g = 1;
    if (y - 1 >= 0) {
      t = map[x][y - 1];
      map[x][y - 1] = map[x][y];
      map[x][y] = t;
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          g = g * 10 + map[i][j];
          g %= 40000007;
        }
      }
      if (!hx[g]) {
        hx[g] = 1;
        syf(x, y - 1, tot + 1);
        hx[g] = 0;
      }
      t = map[x][y - 1];
      map[x][y - 1] = map[x][y];
      map[x][y] = t;
    }
  }
}
void input() {
  int x1, y1;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> map[i][j];
      if (map[i][j] == 0) {
        x1 = i;
        y1 = j;
      }
    }
  }
  int g = 1;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      g = g * 10 + map[i][j];
      g %= 10000007;
    }
  }
  hx[g] = 1;
  syf(x1, y1, 0);
  cout << min0 << endl;
}
int main() {
  input();
  return 0;
}