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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;
#define N 4
#define N2 16

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

struct Puzzle {
  int f[N2], space, MD;
  int cost;

  bool operator<(const Puzzle &p) const {
    for (int i = 0; i < N2; i++) {
      if (f[i] == p.f[i])
        continue;
      return f[i] < p.f[i];
    }
    return false;
  }
};
struct State {
  Puzzle puzzle;
  int estimated;
  bool operator<(const State &s) const { return estimated > s.estimated; }
};

int getAllMD(Puzzle pz) {
  int sum = 0;
  for (int i = 0; i < N2; i++) {
    if (pz.f[i] == N2)
      continue;
    sum += MDT[i][pz.f[i] - 1];
  }
  return sum;
}

int astar(Puzzle s) {
  priority_queue<State> PQ;
  s.MD = getAllMD(s);
  s.cost = 0;
  map<Puzzle, bool> V;
  Puzzle u, v;
  State initial;
  initial.puzzle = s;
  initial.estimated = getAllMD(s) + s.cost;
  PQ.push(initial);
  while (!PQ.empty()) {
    State st = PQ.top();
    PQ.pop();
    u = st.puzzle;

    if (u.MD == 0)
      return u.cost;
    V[u] = true;

    int sx = u.space / N;
    int sy = u.space % N;

    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;
      v = u;

      v.MD -= MDT[tx * N + ty][v.f[tx * N + ty] - 1];
      v.MD += MDT[sx * N + sy][v.f[tx * N + ty] - 1];

      swap(v.f[sx * N + sy], v.f[tx * N + ty]);
      v.space = tx * N + ty;
      if (!V[v]) {
        v.cost++;
        State news;
        news.puzzle = v;
        news.estimated = v.cost + v.MD;
        PQ.push(news);
      }
    }
  }
  return -1;
}

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++) {
    cin >> in.f[i];
    if (in.f[i] == 0) {
      in.f[i] = N2;
      in.space = i;
    }
  }
  cout << astar(in) << endl;

  return 0;
}


Error: TLE

Fixed submission:

// ver 优先队列广搜
#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
tr1::unordered_map<string, int> mp;
int to[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int target[4][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
struct node {
  int v[4][4];    // 棋盘状态
  int step, x, y; // 步数，0的坐标
  string toString() {
    string ss;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        stringstream stream;
        stream << v[i][j];
        ss += stream.str();
      }
    }
    return ss;
  }
  int getMD() {
    int sum = 0;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        if (v[i][j] == 0) {
          sum += abs(i - 3) + abs(j - 3);
        } else {
          sum += abs(i - ((v[i][j] - 1) / 4)) + abs(j - ((v[i][j] - 1) % 4));
        }
      }
    }
    return sum;
  }
};
// 重载小于运算符
bool operator<(node a, node b) {
  return a.step + a.getMD() > b.step + b.getMD();
}

int s[4][4], x, y;
void bfs() {
  priority_queue<node> q;
  node now, end;
  now.x = x;
  now.y = y;
  now.step = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      now.v[i][j] = s[i][j];
    }
  }
  mp[now.toString()] = 1;
  q.push(now);
  while (!q.empty()) {
    now = q.top();
    q.pop();
    if (now.toString() == "1234567891011121314150") {
      printf("%d\\n", now.step);
      return;
    }
    for (int i = 0; i < 4; i++) {
      end = now;
      end.x += to[i][0];
      end.y += to[i][1];
      end.step += 1;
      if (end.x < 0 || end.y < 0 || end.x >= 4 || end.y >= 4)
        continue;
      swap(end.v[end.x][end.y], end.v[now.x][now.y]);
      if (!mp[end.toString()]) {
        mp[end.toString()] = 1;
        q.push(end);
      }
    }
  }
}
int main() {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      scanf("%d", &s[i][j]);
      if (s[i][j] == 0) {
        x = i;
        y = j;
      }
    }
  }
  bfs();
}