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

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <vector>
namespace eight_puzzle {
typedef unsigned int uint;
const int N = 3;
const int ansval = 123456780;
const int ansarray[N * N] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
uint arraytovalue(int *table) {
  uint t = 0;
  for (int i = 0; i < N * N; ++i) {
    t *= 10;
    t = t + table[i];
  }
  return t;
}
void valuetoarray(uint t, int *table) {
  for (int i = N - 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      int panel = t - t / 10 * 10;
      table[i * N + j] = panel;
      t /= 10;
    }
  }
}
uint getscore(uint t) {
  int table[N * N];
  uint score = 0;
  valuetoarray(t, table);
  for (int i = 0; i < N * N; ++i) {
    for (int j = 0; j < N * N; ++j) {
      if (ansarray[i] == table[j]) {
        score += (i / N - j / N) * (i / N - j / N) +
                 (i % N - j % N) * (i % N - j % N);
        break;
      }
    }
  }
  return score;
}
struct State {
  State(const State &s) {
    table = s.table;
    n = s.n;
    score = s.score;
  }
  State(const uint tt, const uint tn) {
    table = tt;
    n = tn;
    score = getscore(table);
  }

  State &operator=(const State &s) {
    table = s.table;
    n = s.n;
    score = s.score;
    return (*this);
  }
  bool operator<(const State &s) const { return score < s.score; }
  bool operator>(const State &s) const { return score > s.score; }
  bool isfinished() const { return table == 123456780; }

  uint table;
  uint n;
  uint score;
};

void getnextstate(const State &s, std::vector<State> &states) {
  int table[N * N];
  int t = s.table;
  int zi, zj;
  valuetoarray(t, table);
  for (int i = 0; i < N * N; ++i) {
    if (table[i] == 0) {
      zi = i / N;
      zj = i % N;
    }
  }
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      if ((i + j) % 2 == 0)
        continue;
      int ti = zi + i, tj = zj + j;
      if (0 <= ti && ti < N && 0 <= tj && tj < N) {
        std::swap(table[ti * N + tj], table[zi * N + zj]);
        states.push_back(State(arraytovalue(table), s.n + 1));
        std::swap(table[ti * N + tj], table[zi * N + zj]);
      }
    }
  }
}

std::map<uint, bool> visited;
}; // namespace eight_puzzle

int main() {
  using namespace eight_puzzle;
  int array[N * N];
  for (int i = 0; i < N * N; ++i) {
    std::cin >> array[i];
  }
  State init(arraytovalue(array), 0);
  if (init.isfinished()) {
    std::cout << 0 << std::endl;
    return 0;
  }
  //	std::priority_queue<State,std::vector<State>,std::greater<State> > Q;
  std::queue<State> Q;
  Q.push(init);
  while (!Q.empty()) {
    State s = Q.front();
    Q.pop();
    std::vector<State> states;
    getnextstate(s, states);
    for (uint i = 0; i < states.size(); ++i) {
      if (states[i].isfinished()) {
        std::cout << states[i].n << std::endl;
        return 0;
      }
      if (visited.find(states[i].table) == visited.end()) {
        Q.push(states[i]);
        visited[states[i].table] = true;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <vector>

namespace eight_puzzle {
typedef unsigned int uint;
const int N = 3;
struct State {
  State(const State &s) {
    table = s.table;
    n = s.n;
  }
  State(const uint tt, const uint tn) {
    table = tt;
    n = tn;
  }

  State &operator=(const State &s) {
    table = s.table;
    n = s.n;
    return (*this);
  }
  bool isfinished() { return table == 123456780; }

  uint table;
  uint n;
};
uint arraytovalue(int *table) {
  uint t = 0;
  for (int i = 0; i < N * N; ++i) {
    t *= 10;
    t = t + table[i];
  }
  return t;
}
void getnextstate(const State &s, std::vector<State> &states) {
  int table[N * N];
  int t = s.table;
  int zi, zj;
  for (int i = N - 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      int panel = t - t / 10 * 10;
      table[i * N + j] = panel;
      t /= 10;
      if (panel == 0) {
        zi = i;
        zj = j;
      }
    }
  }
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      if ((i + j) % 2 == 0)
        continue;
      int ti = zi + i, tj = zj + j;
      if (0 <= ti && ti < N && 0 <= tj && tj < N) {
        std::swap(table[ti * N + tj], table[zi * N + zj]);
        states.push_back(State(arraytovalue(table), s.n + 1));
        std::swap(table[ti * N + tj], table[zi * N + zj]);
      }
    }
  }
}

std::vector<uint> visited;

}; // namespace eight_puzzle

int main() {
  using namespace eight_puzzle;
  int array[N * N];
  for (int i = 0; i < N * N; ++i) {
    std::cin >> array[i];
  }
  State init(arraytovalue(array), 0);

  std::queue<State> Q;
  Q.push(init);
  while (!Q.empty()) {
    State s = Q.front();
    Q.pop();
    std::vector<State> states;
    getnextstate(s, states);
    for (uint i = 0; i < states.size(); ++i) {
      if (states[i].isfinished()) {
        std::cout << states[i].n << std::endl;
        return 0;
      }
      if (std::find(visited.begin(), visited.end(), states[i].table) ==
          visited.end()) {
        Q.push(states[i]);
        visited.push_back(states[i].table);
      }
    }
  }
  return 0;
}