library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub maspypy/library

:warning: other/sliding_puzzle_solver.hpp

Depends on

Code

// Source: https://github.com/maspypy/library/blob/main/other/sliding_puzzle_solver.hpp
#include "linalg/transpose.hpp"
#include "seq/inversion.hpp"

/*
O(HW(H+W))
空マスは -1 (unique)
同じ値が複数あってもよい
操作回数を K として、長さ K+1 の空マスの座標列をかえす
*/
struct Sliding_Puzzle_Solver {
  using P = pair<int, int>;
  vc<P> solve(vvc<int> A, vvc<int> B) {
    int H = len(A), W = len(A[0]);
    auto find = [&](vvc<int>& A, int k) -> P {
      FOR(x, H) FOR(y, W) if (A[x][y] == k) return {x, y};
      assert(0);
      return {0, 0};
    };
    auto [ax, ay] = find(A, -1);
    auto [bx, by] = find(B, -1);
    vc<P> ANS_1, ANS_2;
    while (ax > 0) {
      ANS_1.eb(ax, ay), swap(A[ax][ay], A[ax - 1][ay]), --ax;
    }
    while (ay > 0) {
      ANS_1.eb(ax, ay), swap(A[ax][ay], A[ax][ay - 1]), --ay;
    }
    while (bx > 0) {
      ANS_2.eb(bx, by), swap(B[bx][by], B[bx - 1][by]), --bx;
    }
    while (by > 0) {
      ANS_2.eb(bx, by), swap(B[bx][by], B[bx][by - 1]), --by;
    }
    vc<P> ANS = solve_00(A, B);
    if (ANS.empty()) return {};
    reverse(all(ANS_2));
    return concat(ANS_1, ANS, ANS_2);
  }

 private:
  vc<P> solve_00(vvc<int> A, vvc<int> B) {
    assert(A[0][0] == -1 && B[0][0] == -1);
    int H = len(A), W = len(A[0]);
    if (H == 1 || W == 1) {
      if (A != B) return {};
      vc<P> ANS;
      ANS.eb(0, 0);
      return ANS;
    }
    vc<P> XYA, XYB;
    FOR(x, H) FOR(y, W) XYA.eb(x, y), XYB.eb(x, y);
    sort(all(XYA), [&](auto& a, auto& b) -> bool {
      return A[a.fi][a.se] < A[b.fi][b.se];
    });
    sort(all(XYB), [&](auto& a, auto& b) -> bool {
      return B[a.fi][a.se] < B[b.fi][b.se];
    });
    auto check = [&]() -> bool {
      vc<int> S, T;
      FOR(i, H * W) {
        auto [x1, y1] = XYA[i];
        auto [x2, y2] = XYB[i];
        if (A[x1][y1] != B[x2][y2]) return 0;
        S.eb(W * x1 + y1);
        T.eb(W * x2 + y2);
      }
      ll x = inversion_between(S, T);
      return x % 2 == 0;
    };
    if (!check()) {
      FOR(i, H * W - 1) {
        auto [x1, y1] = XYA[i];
        auto [x2, y2] = XYA[i + 1];
        if (A[x1][y1] != A[x2][y2]) continue;
        swap(XYA[i], XYA[i + 1]);
        break;
      }
      if (!check()) return {};
    }
    vv(P, X, H, W);
    FOR(i, H * W) {
      auto [x1, y1] = XYA[i];
      auto [x2, y2] = XYB[i];
      X[x1][y1] = {x2, y2};
    }
    vc<P> ANS;
    ANS.eb(0, 0);
    solve_sort(X, ANS, false);
    return ANS;
  }

  // 移動先の座標の列を並べたグリッドを与える.
  // (0,0) が空マス
  void solve_sort(vvc<pair<int, int>>& A, vc<P>& ANS, bool tr) {
    int H = len(A), W = len(A[0]);
    vv(P, pos, H, W);
    FOR(x, H) FOR(y, W) {
      P p = A[x][y];
      pos[p.fi][p.se] = {x, y};
    }

    auto [px, py] = pos[0][0];

    auto ope = [&](int x, int y) -> void {
      assert(abs(px - x) + abs(py - y) == 1);
      swap(A[px][py], A[x][y]);
      if (!tr) ANS.eb(x, y);
      if (tr) ANS.eb(y, x);
      pos[A[px][py].fi][A[px][py].se] = {px, py};
      px = x, py = y;
      pos[A[px][py].fi][A[px][py].se] = {px, py};
    };
    if (H == 2 && W == 2) {
      auto check = [&]() -> bool {
        FOR(x, H) FOR(y, W) if (A[x][y].fi != x || A[x][y].se != y) return 0;
        return 1;
      };
      while (!check()) {
        if (px == 0 && py == 0) ope(1, 0);
        if (px == 1 && py == 0) ope(1, 1);
        if (px == 1 && py == 1) ope(0, 1);
        if (px == 0 && py == 1) ope(0, 0);
      }
      return;
    }
    if (H < W) {
      FOR(x, H) FOR(y, W) { swap(A[x][y].fi, A[x][y].se); }
      A = transpose(A, H, W);
      solve_sort(A, ANS, !tr);
      return;
    }
    assert(H >= 3 && W >= 2);
    // 最後の行をそろえる
    FOR_R(y, 1, W) {
      auto& [tx, ty] = pos[H - 1][y];
      if (px == H - 1) ope(px - 1, py);
      while (px - 1 > tx) ope(px - 1, py), tie(tx, ty) = pos[H - 1][y];
      while (px + 1 < tx) ope(px + 1, py), tie(tx, ty) = pos[H - 1][y];
      if (px == tx) {
        if (px == 0)
          ope(px + 1, py);
        else
          ope(px - 1, py);
      }
      assert(abs(px - tx) == 1);
      while (py < ty) ope(px, py + 1);
      while (py > ty) ope(px, py - 1);
      if (px == tx + 1) ope(px - 1, py), tie(tx, ty) = pos[H - 1][y];
      assert(px == tx - 1 && py == ty);
      while (ty < y) {
        ope(px, py + 1), ope(px + 1, py), ope(px, py - 1), ope(px - 1, py),
            ope(px, py + 1);
      }
      while (ty > y) {
        ope(px, py - 1), ope(px + 1, py), ope(px, py + 1), ope(px - 1, py),
            ope(px, py - 1);
      }
      tie(tx, ty) = pos[H - 1][y];
      while (tx < H - 1) {
        ope(px, py - 1), ope(px + 1, py), ope(px + 1, py), ope(px, py + 1),
            ope(px - 1, py);
        tie(tx, ty) = pos[H - 1][y];
      }
      assert(A[H - 1][y] == (pair<int, int>{H - 1, y}));
    }
    auto& [tx, ty] = pos[H - 1][0];
    if (px == H - 1) ope(px - 1, py);
    while (px - 1 > tx) ope(px - 1, py), tie(tx, ty) = pos[H - 1][0];
    while (px + 1 < tx) ope(px + 1, py), tie(tx, ty) = pos[H - 1][0];
    if (px == tx) {
      if (px == 0)
        ope(px + 1, py);
      else
        ope(px - 1, py);
    }
    tie(tx, ty) = pos[H - 1][0];
    assert(abs(px - tx) == 1);
    while (py < ty) ope(px, py + 1);
    while (py > ty) ope(px, py - 1);
    if (px == tx + 1) ope(px - 1, py);
    tie(tx, ty) = pos[H - 1][0];
    if (tx < H - 1) {
      assert(px == tx - 1 && py == ty);
      while (ty > 0) {
        ope(px, py - 1), ope(px + 1, py), ope(px, py + 1), ope(px - 1, py),
            ope(px, py - 1);
      }
      tie(tx, ty) = pos[H - 1][0];
      while (tx < H - 2) {
        ope(px, py + 1), ope(px + 1, py), ope(px + 1, py), ope(px, py - 1),
            ope(px - 1, py);
      }
      ope(px + 1, py), ope(px + 1, py), ope(px, py + 1);
      ope(px - 1, py), ope(px, py - 1), ope(px - 1, py);
      ope(px, py + 1), ope(px + 1, py), ope(px + 1, py);
      ope(px, py - 1), ope(px - 1, py);
    }
    FOR(y, W) assert(A[H - 1][y] == (pair<int, int>{H - 1, y}));
    POP(A);
    solve_sort(A, ANS, tr);
  }
};
#line 1 "other/sliding_puzzle_solver.hpp"
// Source: https://github.com/maspypy/library/blob/main/other/sliding_puzzle_solver.hpp
#line 1 "linalg/transpose.hpp"
// Source: https://github.com/maspypy/library/blob/main/linalg/transpose.hpp
template <typename VC>
vc<VC> transpose(const vc<VC>& A, int H = -1, int W = -1) {
  if (H == -1) {
    H = len(A), W = (len(A) == 0 ? 0 : len(A[0]));
  }
  assert(W != -1);
  vc<VC> B(W, VC(H, 0));
  FOR(x, H) FOR(y, W) B[y][x] = A[x][y];
  return B;
}
#line 1 "seq/inversion.hpp"
// Source: https://github.com/maspypy/library/blob/main/seq/inversion.hpp
#line 1 "ds/fenwicktree/fenwicktree_01.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/fenwicktree/fenwicktree_01.hpp

#line 1 "ds/fenwicktree/fenwicktree.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/fenwicktree/fenwicktree.hpp
#line 1 "alg/monoid/add.hpp"
// Source: https://github.com/maspypy/library/blob/main/alg/monoid/add.hpp

template <typename E>
struct Monoid_Add {
  using X = E;
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
  static constexpr X id() { return X(0); }
  static constexpr bool commute = true;
};
#line 3 "ds/fenwicktree/fenwicktree.hpp"

template <typename Monoid>
struct FenwickTree {
  using G = Monoid;
  using MX = Monoid;
  using E = typename G::value_type;
  static_assert(G::commute);
  int n;
  vector<E> dat;
  E total;

  FenwickTree() {}
  FenwickTree(int n) { build(n); }
  template <typename F>
  FenwickTree(int n, F f) {
    build(n, f);
  }
  FenwickTree(const vc<E>& v) { build(v); }

  void build(int m) {
    n = m;
    dat.assign(m, G::id());
    total = G::id();
  }
  void build(const vc<E>& v) {
    build(len(v), [&](int i) -> E { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m;
    dat.clear();
    dat.reserve(n);
    total = G::id();
    FOR(i, n) { dat.eb(f(i)); }
    for (int i = 1; i <= n; ++i) {
      int j = i + (i & -i);
      if (j <= n) dat[j - 1] = G::op(dat[i - 1], dat[j - 1]);
    }
    total = prefix_sum(m);
  }

  E prod_all() const { return total; }
  E sum_all() const { return total; }
  E sum(int k) const { return prefix_sum(k); }
  E prod(int k) const { return prefix_prod(k); }
  E prefix_sum(int k) const { return prefix_prod(k); }
  E prefix_prod(int k) const {
    chmin(k, n);
    E ret = G::id();
    for (; k > 0; k -= k & -k) ret = G::op(ret, dat[k - 1]);
    return ret;
  }
  E sum(int L, int R) const { return prod(L, R); }
  E prod(int L, int R) const {
    chmax(L, 0), chmin(R, n);
    if (L == 0) return prefix_prod(R);
    assert(0 <= L && L <= R && R <= n);
    E pos = G::id(), neg = G::id();
    while (L < R) {
      pos = G::op(pos, dat[R - 1]), R -= R & -R;
    }
    while (R < L) {
      neg = G::op(neg, dat[L - 1]), L -= L & -L;
    }
    return G::op(pos, G::inverse(neg));
  }

  vc<E> get_all() const {
    vc<E> res(n);
    FOR(i, n) res[i] = prod(i, i + 1);
    return res;
  }

  void add(int k, E x) { multiply(k, x); }
  void multiply(int k, E x) {
    assert(0 <= k && k < n);
    total = G::op(total, x);
    for (++k; k <= n; k += k & -k) dat[k - 1] = G::op(dat[k - 1], x);
  }
  void set(int k, E x) { add(k, G::op(G::inverse(prod(k, k + 1)), x)); }

  template <class F>
  int max_right(const F check, int L = 0) const {
    assert(check(G::id()));
    E s = G::id();
    int i = L;
    // 2^k 進むとダメ
    int k = [&]() {
      while (1) {
        if (i % 2 == 1) {
          s = G::op(s, G::inverse(dat[i - 1])), i -= 1;
        }
        if (i == 0) {
          return topbit(n) + 1;
        }
        int k = lowbit(i) - 1;
        if (i + (1 << k) > n) return k;
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (!check(t)) {
          return k;
        }
        s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
      }
    }();
    while (k) {
      --k;
      if (i + (1 << k) - 1 < len(dat)) {
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (i + (1 << k) <= L || check(t)) {
          i += (1 << k), s = t;
        }
      }
    }
    return i;
  }

  // check(i, x)
  template <class F>
  int max_right_with_index(const F check, int L = 0) const {
    assert(check(L, G::id()));
    E s = G::id();
    int i = L;
    // 2^k 進むとダメ
    int k = [&]() {
      while (1) {
        if (i % 2 == 1) {
          s = G::op(s, G::inverse(dat[i - 1])), i -= 1;
        }
        if (i == 0) {
          return topbit(n) + 1;
        }
        int k = lowbit(i) - 1;
        if (i + (1 << k) > n) return k;
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (!check(i + (1 << k), t)) {
          return k;
        }
        s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
      }
    }();
    while (k) {
      --k;
      if (i + (1 << k) - 1 < len(dat)) {
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (i + (1 << k) <= L || check(i + (1 << k), t)) {
          i += (1 << k), s = t;
        }
      }
    }
    return i;
  }

  template <class F>
  int min_left(const F check, int R) const {
    assert(check(G::id()));
    E s = G::id();
    int i = R;
    // false になるところまで戻る
    int k = 0;
    while (i > 0 && check(s)) {
      s = G::op(s, dat[i - 1]);
      k = lowbit(i);
      i -= i & -i;
    }
    if (check(s)) {
      assert(i == 0);
      return 0;
    }
    // 2^k 進むと ok になる
    // false を維持して進む
    while (k) {
      --k;
      E t = G::op(s, G::inverse(dat[i + (1 << k) - 1]));
      if (!check(t)) {
        i += (1 << k), s = t;
      }
    }
    return i + 1;
  }

  int kth(E k, int L = 0) const {
    return max_right([&k](E x) -> bool { return x <= k; }, L);
  }
};
#line 4 "ds/fenwicktree/fenwicktree_01.hpp"

struct FenwickTree_01 {
  using MX = Monoid_Add<int>;
  int N, n;
  vc<u64> dat;
  FenwickTree<Monoid_Add<int>> bit;
  FenwickTree_01() {}
  FenwickTree_01(int n) { build(n); }
  template <typename F>
  FenwickTree_01(int n, F f) {
    build(n, f);
  }

  void build(int m) {
    N = m;
    n = ceil<int>(N + 1, 64);
    dat.assign(n, u64(0));
    bit.build(n);
  }
  void build(vc<int> dat) {
    build(len(dat), [&](int i) -> int { return dat[i]; });
  }

  template <typename F>
  void build(int m, F f) {
    N = m;
    n = ceil<int>(N + 1, 64);
    dat.assign(n, u64(0));
    FOR(i, N) {
      u64 x = f(i);
      assert(x == 0 || x == 1);
      dat[i / 64] |= x << (i % 64);
    }
    bit.build(n, [&](int i) -> int { return popcnt(dat[i]); });
  }

  int sum_all() { return bit.sum_all(); }
  int sum(int k) { return prefix_sum(k); }
  int prefix_sum(int k) {
    assert(0 <= k && k <= N);
    int ans = bit.sum(k / 64);
    ans += popcnt(dat[k / 64] & ((u64(1) << (k % 64)) - 1));
    return ans;
  }
  int sum(int L, int R) {
    assert(0 <= L && L <= R && R <= N);
    if (L == 0) return prefix_sum(R);
    int ans = 0;
    ans -= popcnt(dat[L / 64] & ((u64(1) << (L % 64)) - 1));
    ans += popcnt(dat[R / 64] & ((u64(1) << (R % 64)) - 1));
    ans += bit.sum(L / 64, R / 64);
    return ans;
  }
  int get(int i) {
    assert(0 <= i && i < N);
    return sum(i, i + 1);
  }
  int prod(int L, int R) { return sum(L, R); }

  void add(int k, int x) {
    assert(0 <= k && k < N);
    if (x == 1) {
      assert(sum(k, k + 1) == 0);
      dat[k / 64] |= u64(1) << (k % 64);
      bit.add(k / 64, 1);
    }
    elif (x == -1) {
      assert(sum(k, k + 1) == 1);
      dat[k / 64] &= ~(u64(1) << (k % 64));
      bit.add(k / 64, -1);
    }
    else assert(0);
  }
  void multiply(int k, int x) { add(k, x); }

  void add(int k) { add(k, 1); }
  void remove(int k) { add(k, -1); }

  int kth(int k, int L = 0) {
    assert(0 <= L && L <= N);
    assert(0 <= k && k < sum_all());
    k += popcnt(dat[L / 64] & ((u64(1) << (L % 64)) - 1));
    L /= 64;
    int mid = 0;
    auto check = [&](auto e) -> bool {
      if (e <= k) chmax(mid, e);
      return e <= k;
    };
    int idx = bit.max_right(check, L);
    if (idx == n) return N;
    k -= mid;
    u64 x = dat[idx];
    int p = popcnt(x);
    if (p <= k) return N;
    k = binary_search(
        [&](int n) -> bool { return (p - popcnt(x >> n)) <= k; }, 0, 64, 0);
    return 64 * idx + k;
  }

  int next(int k) {
    assert(0 <= k && k <= N);
    int idx = k / 64;
    k %= 64;
    u64 x = dat[idx] & ~((u64(1) << k) - 1);
    if (x) return 64 * idx + lowbit(x);
    idx = bit.kth(0, idx + 1);
    if (idx == n || !dat[idx]) return N;
    return 64 * idx + lowbit(dat[idx]);
  }

  int prev(int k) {
    assert(0 <= k && k <= N);
    if (k == N) --k;
    int idx = k / 64;
    k %= 64;
    u64 x = dat[idx];
    if (k < 63) x &= (u64(1) << (k + 1)) - 1;
    if (x) return 64 * idx + topbit(x);
    idx = bit.min_left([&](auto e) -> bool { return e <= 0; }, idx) - 1;
    if (idx == -1) return -1;
    return 64 * idx + topbit(dat[idx]);
  }

  string to_string() {
    string out;
    FOR(i, N) out += '0' + (dat[i / 64] >> (i & 63) & 1);
    return out;
  }
};
#line 1 "ds/index_compression.hpp"
// Source: https://github.com/maspypy/library/blob/main/ds/index_compression.hpp
template <typename T>
struct Index_Compression_DISTINCT_SMALL {
  static_assert(std::is_same_v<T, int>);
  bool built = false;
  int mi, ma;
  vc<T> dat;
  vc<T> build(vc<int> X) {
    assert(!built);
    built = true;
    mi = 0, ma = -1;
    if (!X.empty()) mi = MIN(X), ma = MAX(X);
    dat.assign(ma - mi + 2, 0);
    for (auto& x : X) dat[x - mi + 1]++;
    FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
    for (auto& x : X) {
      x = dat[x - mi]++;
    }
    FOR_R(i, 1, len(dat)) dat[i] = dat[i - 1];
    dat[0] = 0;
    return X;
  }
  int val_to_idx(T x) const {
    if (x <= mi) return dat[0];
    if (x > ma) return dat.back();
    return dat[clamp<ll>(x - mi, 0, ma - mi + 1)];
  }
};

template <typename T>
struct Index_Compression_SAME_SMALL {
  static_assert(std::is_same_v<T, int>);
  bool built = false;
  int mi, ma;
  vc<T> dat;
  vc<T> build(vc<T> X) {
    assert(!built);
    built = true;
    mi = 0, ma = -1;
    if (!X.empty()) mi = MIN(X), ma = MAX(X);
    dat.assign(ma - mi + 2, 0);
    for (auto& x : X) dat[x - mi + 1] = 1;
    FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
    for (auto& x : X) {
      x = dat[x - mi];
    }
    return X;
  }
  int val_to_idx(T x) const {
    if (x <= mi) return dat[0];
    if (x > ma) return dat.back();
    return dat[clamp<ll>(x - mi, 0, ma - mi + 1)];
  }
};

template <typename T>
struct Index_Compression_SAME_LARGE {
  bool built = false;
  vc<T> dat;
  vc<int> build(const vc<T>& X) {
    assert(!built);
    built = true;
    dat.reserve(len(X));
    vc<pair<T, int>> tmp(len(X));
    FOR(i, len(X)) tmp[i] = {X[i], i};
    sort(all(tmp));
    vc<int> ANS(len(X));
    for (auto [x, j] : tmp) {
      if (dat.empty() || dat.back() != x) dat.eb(x);
      ANS[j] = len(dat) - 1;
    }
    return ANS;
  }
  int size() const { return len(dat); }
  int val_to_idx(T x) const { return LB(dat, x); }
  int idx_to_val(int i) const { return dat[i]; }
};

template <typename T>
struct Index_Compression_DISTINCT_LARGE {
  bool built = false;
  vc<T> dat;
  vc<int> build(vc<T> X) {
    assert(!built);
    built = true;
    dat.reserve(len(X));
    vc<pair<T, int>> tmp(len(X));
    FOR(i, len(X)) tmp[i] = {X[i], i};
    sort(all(tmp));
    vc<int> ANS(len(X));
    for (auto [x, j] : tmp) {
      dat.eb(x);
      ANS[j] = len(dat) - 1;
    }
    return ANS;
  }
  int size() const { return len(dat); }
  int val_to_idx(T x) const { return LB(dat, x); }
  int idx_to_val(int i) const { return dat[i]; }
};

template <typename T, bool SMALL>
using Index_Compression_DISTINCT =
    typename std::conditional<SMALL, Index_Compression_DISTINCT_SMALL<T>,
        Index_Compression_DISTINCT_LARGE<T>>::type;
template <typename T, bool SMALL>
using Index_Compression_SAME = typename std::conditional<SMALL,
    Index_Compression_SAME_SMALL<T>, Index_Compression_SAME_LARGE<T>>::type;

// SAME: [2,3,2] -> [0,1,0]
// DISTINCT: [2,2,3] -> [0,2,1]
// build で列を圧縮してくれる. そのあと
// (x): lower_bound(X,x) をかえす
template <typename T, bool SAME, bool SMALL>
using Index_Compression =
    typename std::conditional<SAME, Index_Compression_SAME<T, SMALL>,
        Index_Compression_DISTINCT<T, SMALL>>::type;
#line 4 "seq/inversion.hpp"

template <typename T>
ll inversion(const vc<T>& A) {
  int N = len(A);
  if (A.empty()) return 0;
  ll ANS = 0;
  FenwickTree_01 bit(N);
  auto I = argsort(A);
  for (auto& i : I) {
    ANS += bit.sum_all() - bit.sum(i);
    bit.add(i, 1);
  }
  return ANS;
}

// i 番目:A_i が先頭になるように rotate したときの転倒数
template <typename T, bool SMALL = false>
vi inversion_rotate(const vc<T>& A) {
  if (A.empty()) return {};
  const int N = len(A);
  Index_Compression_SAME<T, SMALL> comp;
  auto rank = comp.build(A);
  int K = MAX(rank) + 1;
  ll ANS = 0;
  FenwickTree<Monoid_Add<int>> bit(K);
  for (auto&& x : rank) {
    ANS += bit.sum(x + 1, K);
    bit.add(x, 1);
  }
  vi res(N);
  FOR(i, N) {
    res[i] = ANS;
    ll x = rank[i];
    ANS = ANS + bit.sum(x + 1, K) - bit.prefix_sum(x);
  }
  return res;
}

// inv[i][j] = inversion A[i:j) であるような (N+1, N+1) テーブル
template <typename T>
vvc<int> all_range_inversion(const vc<T>& A) {
  int N = len(A);
  vv(int, dp, N + 1, N + 1);
  FOR_R(L, N + 1) FOR(R, L + 2, N + 1) {
    dp[L][R] = dp[L][R - 1] + dp[L + 1][R] - dp[L + 1][R - 1];
    if (A[L] > A[R - 1]) ++dp[L][R];
  }
  return dp;
}

template <typename T>
ll inversion_between(const vc<T>& A, const vc<T>& B) {
  assert(len(A) == len(B));
  int N = len(A);
  map<T, vc<int>> MP;
  FOR(i, N) MP[B[i]].eb(i);
  vc<int> TO(N);
  FOR_R(i, N) {
    auto& I = MP[A[i]];
    if (I.empty()) return -1;
    TO[i] = POP(I);
  }
  return inversion(TO);
}
#line 4 "other/sliding_puzzle_solver.hpp"

/*
O(HW(H+W))
空マスは -1 (unique)
同じ値が複数あってもよい
操作回数を K として、長さ K+1 の空マスの座標列をかえす
*/
struct Sliding_Puzzle_Solver {
  using P = pair<int, int>;
  vc<P> solve(vvc<int> A, vvc<int> B) {
    int H = len(A), W = len(A[0]);
    auto find = [&](vvc<int>& A, int k) -> P {
      FOR(x, H) FOR(y, W) if (A[x][y] == k) return {x, y};
      assert(0);
      return {0, 0};
    };
    auto [ax, ay] = find(A, -1);
    auto [bx, by] = find(B, -1);
    vc<P> ANS_1, ANS_2;
    while (ax > 0) {
      ANS_1.eb(ax, ay), swap(A[ax][ay], A[ax - 1][ay]), --ax;
    }
    while (ay > 0) {
      ANS_1.eb(ax, ay), swap(A[ax][ay], A[ax][ay - 1]), --ay;
    }
    while (bx > 0) {
      ANS_2.eb(bx, by), swap(B[bx][by], B[bx - 1][by]), --bx;
    }
    while (by > 0) {
      ANS_2.eb(bx, by), swap(B[bx][by], B[bx][by - 1]), --by;
    }
    vc<P> ANS = solve_00(A, B);
    if (ANS.empty()) return {};
    reverse(all(ANS_2));
    return concat(ANS_1, ANS, ANS_2);
  }

 private:
  vc<P> solve_00(vvc<int> A, vvc<int> B) {
    assert(A[0][0] == -1 && B[0][0] == -1);
    int H = len(A), W = len(A[0]);
    if (H == 1 || W == 1) {
      if (A != B) return {};
      vc<P> ANS;
      ANS.eb(0, 0);
      return ANS;
    }
    vc<P> XYA, XYB;
    FOR(x, H) FOR(y, W) XYA.eb(x, y), XYB.eb(x, y);
    sort(all(XYA), [&](auto& a, auto& b) -> bool {
      return A[a.fi][a.se] < A[b.fi][b.se];
    });
    sort(all(XYB), [&](auto& a, auto& b) -> bool {
      return B[a.fi][a.se] < B[b.fi][b.se];
    });
    auto check = [&]() -> bool {
      vc<int> S, T;
      FOR(i, H * W) {
        auto [x1, y1] = XYA[i];
        auto [x2, y2] = XYB[i];
        if (A[x1][y1] != B[x2][y2]) return 0;
        S.eb(W * x1 + y1);
        T.eb(W * x2 + y2);
      }
      ll x = inversion_between(S, T);
      return x % 2 == 0;
    };
    if (!check()) {
      FOR(i, H * W - 1) {
        auto [x1, y1] = XYA[i];
        auto [x2, y2] = XYA[i + 1];
        if (A[x1][y1] != A[x2][y2]) continue;
        swap(XYA[i], XYA[i + 1]);
        break;
      }
      if (!check()) return {};
    }
    vv(P, X, H, W);
    FOR(i, H * W) {
      auto [x1, y1] = XYA[i];
      auto [x2, y2] = XYB[i];
      X[x1][y1] = {x2, y2};
    }
    vc<P> ANS;
    ANS.eb(0, 0);
    solve_sort(X, ANS, false);
    return ANS;
  }

  // 移動先の座標の列を並べたグリッドを与える.
  // (0,0) が空マス
  void solve_sort(vvc<pair<int, int>>& A, vc<P>& ANS, bool tr) {
    int H = len(A), W = len(A[0]);
    vv(P, pos, H, W);
    FOR(x, H) FOR(y, W) {
      P p = A[x][y];
      pos[p.fi][p.se] = {x, y};
    }

    auto [px, py] = pos[0][0];

    auto ope = [&](int x, int y) -> void {
      assert(abs(px - x) + abs(py - y) == 1);
      swap(A[px][py], A[x][y]);
      if (!tr) ANS.eb(x, y);
      if (tr) ANS.eb(y, x);
      pos[A[px][py].fi][A[px][py].se] = {px, py};
      px = x, py = y;
      pos[A[px][py].fi][A[px][py].se] = {px, py};
    };
    if (H == 2 && W == 2) {
      auto check = [&]() -> bool {
        FOR(x, H) FOR(y, W) if (A[x][y].fi != x || A[x][y].se != y) return 0;
        return 1;
      };
      while (!check()) {
        if (px == 0 && py == 0) ope(1, 0);
        if (px == 1 && py == 0) ope(1, 1);
        if (px == 1 && py == 1) ope(0, 1);
        if (px == 0 && py == 1) ope(0, 0);
      }
      return;
    }
    if (H < W) {
      FOR(x, H) FOR(y, W) { swap(A[x][y].fi, A[x][y].se); }
      A = transpose(A, H, W);
      solve_sort(A, ANS, !tr);
      return;
    }
    assert(H >= 3 && W >= 2);
    // 最後の行をそろえる
    FOR_R(y, 1, W) {
      auto& [tx, ty] = pos[H - 1][y];
      if (px == H - 1) ope(px - 1, py);
      while (px - 1 > tx) ope(px - 1, py), tie(tx, ty) = pos[H - 1][y];
      while (px + 1 < tx) ope(px + 1, py), tie(tx, ty) = pos[H - 1][y];
      if (px == tx) {
        if (px == 0)
          ope(px + 1, py);
        else
          ope(px - 1, py);
      }
      assert(abs(px - tx) == 1);
      while (py < ty) ope(px, py + 1);
      while (py > ty) ope(px, py - 1);
      if (px == tx + 1) ope(px - 1, py), tie(tx, ty) = pos[H - 1][y];
      assert(px == tx - 1 && py == ty);
      while (ty < y) {
        ope(px, py + 1), ope(px + 1, py), ope(px, py - 1), ope(px - 1, py),
            ope(px, py + 1);
      }
      while (ty > y) {
        ope(px, py - 1), ope(px + 1, py), ope(px, py + 1), ope(px - 1, py),
            ope(px, py - 1);
      }
      tie(tx, ty) = pos[H - 1][y];
      while (tx < H - 1) {
        ope(px, py - 1), ope(px + 1, py), ope(px + 1, py), ope(px, py + 1),
            ope(px - 1, py);
        tie(tx, ty) = pos[H - 1][y];
      }
      assert(A[H - 1][y] == (pair<int, int>{H - 1, y}));
    }
    auto& [tx, ty] = pos[H - 1][0];
    if (px == H - 1) ope(px - 1, py);
    while (px - 1 > tx) ope(px - 1, py), tie(tx, ty) = pos[H - 1][0];
    while (px + 1 < tx) ope(px + 1, py), tie(tx, ty) = pos[H - 1][0];
    if (px == tx) {
      if (px == 0)
        ope(px + 1, py);
      else
        ope(px - 1, py);
    }
    tie(tx, ty) = pos[H - 1][0];
    assert(abs(px - tx) == 1);
    while (py < ty) ope(px, py + 1);
    while (py > ty) ope(px, py - 1);
    if (px == tx + 1) ope(px - 1, py);
    tie(tx, ty) = pos[H - 1][0];
    if (tx < H - 1) {
      assert(px == tx - 1 && py == ty);
      while (ty > 0) {
        ope(px, py - 1), ope(px + 1, py), ope(px, py + 1), ope(px - 1, py),
            ope(px, py - 1);
      }
      tie(tx, ty) = pos[H - 1][0];
      while (tx < H - 2) {
        ope(px, py + 1), ope(px + 1, py), ope(px + 1, py), ope(px, py - 1),
            ope(px - 1, py);
      }
      ope(px + 1, py), ope(px + 1, py), ope(px, py + 1);
      ope(px - 1, py), ope(px, py - 1), ope(px - 1, py);
      ope(px, py + 1), ope(px + 1, py), ope(px + 1, py);
      ope(px, py - 1), ope(px - 1, py);
    }
    FOR(y, W) assert(A[H - 1][y] == (pair<int, int>{H - 1, y}));
    POP(A);
    solve_sort(A, ANS, tr);
  }
};
Back to top page