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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[5] = {1, 0, -1, 0, 0};
int dy[5] = {0, 1, 0, -1, 0};
typedef pair<int, int> P;
const int MAX_L = 16;
const int MAX_N = 3;
struct State {
  P p[MAX_N];
  int d, f;
  State(P pp[MAX_N], int dd, int hh) {
    REP(i, MAX_N) p[i] = pp[i];
    d = dd;
    f = dd + hh;
  }
  bool operator<(const State &s) const {
    if (f != s.f)
      return f > s.f;
    return d < s.d;
  }
};
int W, H, N;
string grid[MAX_L];
inline int make(const P p[MAX_N]) {
  int res = 0;
  REP(i, N) { res = res << 8 | (p[i].first << 4) | p[i].second; }
  return res;
}
unsigned short dist[1 << 24];
inline void add(const State &s, priority_queue<State> &que) {
  int k = make(s.p);
  if (dist[k] > s.d) {
    dist[k] = s.d;
    que.push(s);
  }
}
inline bool equal(P p[MAX_N], P q[MAX_N]) {
  REP(i, N) if (p[i] != q[i]) return false;
  return true;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H) && grid[y][x] != '#';
}
inline bool valid(const State &s, const State &prev) {
  REP(i, N) if (!valid(s.p[i].first, s.p[i].second, W, H)) return false;
  FOR(i, 0, N) FOR(j, i + 1, N) if (s.p[i] == s.p[j]) return false;
  FOR(i, 0, N)
  FOR(j, i + 1, N) if (s.p[i] == prev.p[j] && s.p[j] == prev.p[i]) return false;
  return true;
}
int memo[16][16][16][16];
int calc_distance(P start, P goal) {
  int &dist = memo[start.first][start.second][goal.first][goal.second];
  if (dist != -1)
    return dist;
  queue<P> que;
  que.push(goal);
  memo[goal.first][goal.second][goal.first][goal.second] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    REP(r, 4) {
      P q = p;
      q.first += dx[r];
      q.second += dy[r];
      if (!valid(q.first, q.second, W, H))
        continue;
      if (memo[q.first][q.second][goal.first][goal.second] != -1)
        continue;
      memo[q.first][q.second][goal.first][goal.second] =
          memo[p.first][p.second][goal.first][goal.second] + 1;
      que.push(q);
    }
  }
  return dist;
}
void print(State s) {
  printf("d = %d h = %d f = %d ", s.d, s.f - s.d, s.f);
  REP(i, N) printf("(%d, %d) ", s.p[i].first, s.p[i].second);
  cout << endl;
}
int main() {
  while (cin >> W >> H >> N && N) {
    memset(memo, -1, sizeof memo);
    REP(i, 1 << 24) dist[i] = 16 * 16 * 3;
    cin.ignore();
    REP(y, H) getline(cin, grid[y]);
    P goal[MAX_N];
    P start[MAX_N];
    REP(y, H) REP(x, W) {
      if (islower(grid[y][x])) {
        int k = grid[y][x] - 'a';
        grid[y][x] = ' ';
        start[k] = P(x, y);
      }
      if (isupper(grid[y][x])) {
        int k = grid[y][x] - 'A';
        grid[y][x] = ' ';
        goal[k] = P(x, y);
      }
    }
    priority_queue<State> que;
    State init(start, 0, 1);
    add(init, que);
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      // print(s);
      if (s.d == s.f) {
        cout << s.d << endl;
        break;
      }
      REP(SR, 5 * 5 * 5) {
        int R = SR;
        State next = s;
        REP(i, N) {
          int r = R % 5;
          R /= 5;
          next.p[i].first += dx[r];
          next.p[i].second += dy[r];
        }
        if (!valid(next, s))
          continue;
        next.d++;
        int h = 0;
        REP(i, N) { h = max(h, calc_distance(next.p[i], goal[i])); }
        next.f = next.d + h;
        assert(h < INF);
        add(next, que);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[5] = {1, 0, -1, 0, 0};
int dy[5] = {0, 1, 0, -1, 0};
typedef pair<int, int> P;
const int MAX_L = 16;
const int MAX_N = 3;
struct State {
  P p[MAX_N];
  int d, f;
  State(P pp[MAX_N], int dd, int hh) {
    REP(i, MAX_N) p[i] = pp[i];
    d = dd;
    f = dd + hh;
  }
  bool operator<(const State &s) const {
    if (f != s.f)
      return f > s.f;
    return d < s.d;
  }
};
int W, H, N;
string grid[MAX_L];
P goal[MAX_N];
P start[MAX_N];
int make(const P p[MAX_N]) {
  int res = 0;
  REP(i, N) { res = res << 8 | (p[i].first << 4) | p[i].second; }
  return res;
}
void add(const State &s, map<int, int> &dist, priority_queue<State> &que) {
  int k = make(s.p);
  if (!dist.count(k) || dist[k] > s.d) {
    dist[k] = s.d;
    que.push(s);
  }
}
bool equal(P p[MAX_N], P q[MAX_N]) {
  REP(i, N) if (p[i] != q[i]) return false;
  return true;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H) && grid[y][x] != '#';
}
bool valid(const State &s, const State &prev) {
  REP(i, N) if (!valid(s.p[i].first, s.p[i].second, W, H)) return false;
  FOR(i, 0, N) FOR(j, i + 1, N) if (s.p[i] == s.p[j]) return false;
  FOR(i, 0, N)
  FOR(j, i + 1, N) if (s.p[i] == prev.p[j] && s.p[j] == prev.p[i]) return false;
  return true;
}
int memo[16][16][16][16];
int calc_distance(P p, P goal) {
  if (!valid(p.first, p.second, W, H))
    return INF;
  if (p == goal)
    return 0;
  int &dist = memo[p.first][p.second][goal.first][goal.second];
  if (dist != -1)
    return dist;
  dist = INF;
  REP(r, 4) {
    P q = p;
    q.first += dx[r];
    q.second += dy[r];
    dist = min(dist, calc_distance(q, goal) + 1);
  }
  return dist;
}
void print(State s) {
  printf("d = %d h = %d f = %d ", s.d, s.f - s.d, s.f);
  REP(i, N) printf("(%d, %d) ", s.p[i].first, s.p[i].second);
  cout << endl;
}
void move(State &s, const State &prev, int k, map<int, int> &dist,
          priority_queue<State> &que) {
  if (k == 0)
    s.d++;
  if (k == N) {
    if (!valid(s, prev))
      return;
    int h = 0;
    REP(i, N) { h = max(h, calc_distance(s.p[i], goal[i])); }
    s.f = s.d + h;
    add(s, dist, que);
  } else {
    REP(r, 5) {
      if (valid(s.p[k].first + dx[r], s.p[k].second + dy[r], W, H)) {
        s.p[k].first += dx[r];
        s.p[k].second += dy[r];
        move(s, prev, k + 1, dist, que);
        s.p[k].first -= dx[r];
        s.p[k].second -= dy[r];
      }
    }
  }
}
int main() {
  while (cin >> W >> H >> N && N) {
    memset(memo, -1, sizeof memo);
    cin.ignore();
    REP(y, H) getline(cin, grid[y]);
    REP(y, H) REP(x, W) {
      if (islower(grid[y][x])) {
        int k = grid[y][x] - 'a';
        grid[y][x] = ' ';
        start[k] = P(x, y);
      }
      if (isupper(grid[y][x])) {
        int k = grid[y][x] - 'A';
        grid[y][x] = ' ';
        goal[k] = P(x, y);
      }
    }
    map<int, int> dist;
    priority_queue<State> que;
    State init(start, 0, 0);
    add(init, dist, que);
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      //  print(s);
      if (equal(s.p, goal)) {
        cout << s.d << endl;
        break;
      }
      State ss = s;

      move(s, ss, 0, dist, que);
    }
  }
  return 0;
}