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

#include <bits/stdc++.h>
using namespace std;
typedef pair<vector<int>, vector<int>> P;
typedef pair<int, int> S;

int w, h, n;
string s[20];
int ax[] = {1, -1, 0, 0, 0};
int ay[] = {0, 0, 1, -1, 0};

S i2s(int x) { return S(x / 2744, x % 2744); }
int s2i(const S &s) {
  // cout<<s.first<<" "<<s.second<<":"<<(s.first<<13)+s.second<<endl;
  return (s.first * 2744) + s.second;
}
S p2s(const P &p) {
  int y = 0, x = 0;
  for (int i = n - 1; i >= 0; i--) {
    y = y * 14 + p.first[i] - 1;
    x = x * 14 + p.second[i] - 1;
  }
  return S(y, x);
}

P s2p(const S &s) {
  vector<int> y, x;
  int a = s.first, b = s.second;
  for (int i = 0; i < n; i++) {
    y.push_back(a % 14 + 1);
    x.push_back(b % 14 + 1);
    a /= 14;
    b /= 14;
  }
  return P(y, x);
}

int p2i(const P &p) { return s2i(p2s(p)); }
P i2p(int x) { return s2p(i2s(x)); }

short m[7530000];
queue<int> q;

vector<int> ys, xs;

int calc() {
  int y = 0, x = 0;
  for (int i = n - 1; i >= 0; i--) {
    y = y * 14 + ys[i] - 1;
    x = x * 14 + xs[i] - 1;
  }
  return s2i(S(y, x));
}

void dfs(int d, P &p, int xyz) {
  if (d == n) {
    int abc = calc();
    if (~m[abc])
      return;
    m[abc] = m[xyz] + 1;
    q.push(abc);
    return;
  }
  for (int k = 0; k < 5; k++) {
    int ny = p.first[d] + ay[k], nx = p.second[d] + ax[k];
    if (ny < 0 || h <= ny || nx < 0 || w <= nx)
      continue;
    if (s[ny][nx] == '#')
      continue;
    bool f = 0;
    for (int j = 0; j < d; j++) {
      f |= (ys[j] == ny && xs[j] == nx);
      f |= (ys[j] == p.first[d] && xs[j] == p.second[d]) &&
           (ny == p.first[j] && nx == p.second[j]);
    }
    if (f)
      continue;
    ys.push_back(ny);
    xs.push_back(nx);
    dfs(d + 1, p, xyz);
    ys.pop_back();
    xs.pop_back();
  }
}

signed main() {
  while (cin >> w >> h >> n, w || h || n) {
    cin.ignore();
    for (int i = 0; i < h; i++)
      getline(cin, s[i]);
    vector<int> yt(n), xt(n);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (islower(s[i][j])) {
          yt[s[i][j] - 'a'] = i;
          xt[s[i][j] - 'a'] = j;
        }
    memset(m, -1, sizeof(m));
    while (!q.empty())
      q.pop();
    q.push(p2i(P(yt, xt)));
    m[p2i(P(yt, xt))] = 0;
    int ans = -1;
    while (!q.empty()) {
      int xyz = q.front();
      q.pop();
      P p = i2p(xyz);
      // cout<<xyz<<":"<<m[xyz]<<endl;
      // for(int i=0;i<n;i++) cout<<p.first[i]<<" "<<p.second[i]<<endl;
      bool flg = 1;
      for (int i = 0; i < n; i++)
        flg &= (s[p.first[i]][p.second[i]] == 'A' + i);
      if (flg) {
        ans = m[xyz];
        break;
      }
      dfs(0, p, xyz);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<vector<int>, vector<int>> P;
typedef pair<int, int> S;

int w, h, n;
string s[20];
int ax[] = {1, -1, 0, 0, 0};
int ay[] = {0, 0, 1, -1, 0};

S i2s(int x) { return S(x / 2744, x % 2744); }
int s2i(const S &s) {
  // cout<<s.first<<" "<<s.second<<":"<<(s.first<<13)+s.second<<endl;
  return (s.first * 2744) + s.second;
}
S p2s(const P &p) {
  int y = 0, x = 0;
  for (int i = n - 1; i >= 0; i--) {
    y = y * 14 + p.first[i] - 1;
    x = x * 14 + p.second[i] - 1;
  }
  return S(y, x);
}
P s2p(const S &s) {
  vector<int> y, x;
  int a = s.first, b = s.second;
  for (int i = 0; i < n; i++) {
    y.push_back(a % 14 + 1);
    x.push_back(b % 14 + 1);
    a /= 14;
    b /= 14;
  }
  return P(y, x);
}

int p2i(const P &p) { return s2i(p2s(p)); }
P i2p(int x) { return s2p(i2s(x)); }

char m[7530000];
queue<int> q;

vector<int> ys, xs;
void dfs(int d, P &p, int xyz) {
  if (d == n) {
    P r = P(ys, xs);
    int abc = p2i(r);
    if (~m[abc])
      return;
    m[abc] = m[xyz] + 1;
    q.push(abc);
    return;
  }
  for (int k = 0; k < 5; k++) {
    int ny = p.first[d] + ay[k], nx = p.second[d] + ax[k];
    if (ny < 0 || h <= ny || nx < 0 || w <= nx)
      continue;
    if (s[ny][nx] == '#')
      continue;
    bool f = 0;
    for (int j = 0; j < d; j++) {
      f |= (ys[j] == ny && xs[j] == nx);
      f |= (ys[j] == p.first[d] && xs[j] == p.second[d]) &&
           (ny == p.first[j] && nx == p.second[j]);
    }
    if (f)
      continue;
    ys.push_back(ny);
    xs.push_back(nx);
    dfs(d + 1, p, xyz);
    ys.pop_back();
    xs.pop_back();
  }
}

signed main() {
  while (cin >> w >> h >> n, w || h || n) {
    cin.ignore();
    for (int i = 0; i < h; i++)
      getline(cin, s[i]);
    vector<int> yt(n), xt(n);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (islower(s[i][j])) {
          yt[s[i][j] - 'a'] = i;
          xt[s[i][j] - 'a'] = j;
        }
    memset(m, -1, sizeof(m));
    while (!q.empty())
      q.pop();
    q.push(p2i(P(yt, xt)));
    m[p2i(P(yt, xt))] = 0;
    int ans = -1;
    while (!q.empty()) {
      int xyz = q.front();
      q.pop();
      P p = i2p(xyz);
      // cout<<xyz<<":"<<m[xyz]<<endl;
      // for(int i=0;i<n;i++) cout<<p.first[i]<<" "<<p.second[i]<<endl;
      bool flg = 1;
      for (int i = 0; i < n; i++)
        flg &= (s[p.first[i]][p.second[i]] == 'A' + i);
      if (flg) {
        ans = m[xyz];
        break;
      }
      dfs(0, p, xyz);
    }
    cout << ans << endl;
  }
  return 0;
}