// File: /workspace/github_cpp_makefile_star2_O2/tanakh[P]icfpc2015/main.cpp
// Function: rec_beam(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>> const&, unit const&, std::complex<int> const&, int, std::vector<int, std::allocator<int>>&, std::set<std::pair<std::pair<int, int>, int>, std::less<std::pair<std::pair<int, int>, int>>, std::allocator<std::pair<std::pair<int, int>, int>>>&, std::multiset<candidate, std::less<candidate>, std::allocator<candidate>>&, int, int, int, int, std::vector<int, std::allocator<int>> const&)
void rec_beam(const vector<vector<int>> &bd, const unit &u, const pt &pos, int rot,
              vector<int> &hist, set<pair<pair<int,int>, int>> &ss,
              multiset<candidate> &cand, int ls_old, int prev,
              int beam_width, int prev_score, const vector<int> &prev_moves)
{
  auto key = make_pair(make_pair(pos.real(), pos.imag()), rot);
  if (ss.count(key)) return;
  ss.insert(key);

  int invalid = -1;

  static const int tbl[][6] = {
    {1,0,2,3,4,5},
    {2,1,0,3,4,5},
    {0,1,2,3,4,5},
    {0,1,2,4,5,3},
  };
  const int *ord = tbl[3];


  // int ord[] = {0, 1, 2, 3, 4, 5};
  // for (int i = 0; i < 6; i++)
  //   swap(ord[i], ord[i+genrand_int31()%(6-i)]);

  for (int mov_ = 0; mov_ < 6; mov_++) {
    int mov = ord[mov_];
    pt npos = pos;
    int nrot = rot;
    switch(mov) {
    case W:   npos += pt(-2, 0); break;
    case E:   npos += pt(2, 0); break;
    case SW:  npos += pt(-1, 1); break;
    case SE:  npos += pt(1, 1); break;
    case CW:  nrot = (nrot + 1) % u.rot_max; break;
    case CCW: nrot = (nrot + u.rot_max - 1) % u.rot_max; break;
    }
    if (!check(bd, npos, u, nrot)) {
      invalid = mov;
      continue;
    }

    hist.push_back(mov);
    rec_beam(bd, u, npos, nrot, hist, ss, cand, ls_old, mov, beam_width, prev_score, prev_moves);
    hist.pop_back();
  }

  if (invalid >= 0) {
    double tscore =
      prev_score +
      calc_score(const_cast<vector<vector<int>>&>(bd), pos, u, rot, ls_old, hist);

    if ((int)cand.size() >= beam_width) {
      if (cand.begin()->score >= tscore) return;
      cand.erase(cand.begin());
    }

    candidate tmp;
    tmp.score = tscore;

    hist.push_back(invalid);
    tmp.moves = hist;
    hist.pop_back();

    vector<vector<int>> bdd = bd;
    auto tt = put_unit(bdd, pos, u, rot, ls_old);
    tmp.move_score = tt.first;
    tmp.board = bdd;
    tmp.ls_old = tt.second;

    tmp.prev_score = prev_score;
    tmp.prev_moves = prev_moves;

    cand.insert(tmp);
  }

  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r9, %rbp
movq %r8, %r13
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movq (%rdx), %rax
leaq 0xf8(%rsp), %rsi
movq %rax, (%rsi)
movq %rcx, 0x38(%rsp)
movl %ecx, 0x8(%rsi)
movq %r9, %rdi
callq 0x99a4
testq %rax, %rax
jne 0x5a45
movq %r13, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
leaq 0xf8(%rsp), %rsi
movq %rbp, 0x88(%rsp)
movq %rbp, %rdi
callq 0xe0b2
orl $-0x1, 0x48(%rsp)
movq 0x38(%rsp), %rcx
leal -0x1(%rcx), %eax
movq %rax, 0x80(%rsp)
leal 0x1(%rcx), %eax
movl %eax, 0x4c(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x40(%rsp)
xorl %ebp, %ebp
leaq 0xca71(%rip), %rbx      # 0x12200
leaq 0x90(%rsp), %r15
movq 0x58(%rsp), %r14
cmpq $0x6, %rbp
je 0x58dc
movl 0x48(%rbx,%rbp,4), %edx
movl %edx, 0x68(%rsp)
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
movq 0x38(%rsp), %rcx
movl %ecx, %r13d
cmpq $0x5, %rdx
ja 0x5835
movq %rax, %rcx
shrq $0x20, %rcx
leaq 0xc94e(%rip), %rsi      # 0x12128
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
addl $-0x2, %eax
jmp 0x580f
movl 0x4c(%rsp), %eax
cltd
idivl 0x20(%r12)
jmp 0x5832
decl %eax
jmp 0x57fa
incl %eax
movl %eax, 0x90(%rsp)
incl %ecx
movl %ecx, 0x94(%rsp)
jmp 0x5816
addl $0x2, %eax
movl %eax, 0x90(%rsp)
movq 0x38(%rsp), %rax
movl %eax, %r13d
jmp 0x5835
movl 0x20(%r12), %ecx
movq 0x80(%rsp), %rax
addl %ecx, %eax
cltd
idivl %ecx
movl %edx, %r13d
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x481a
testb %al, %al
je 0x58c0
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
callq 0x99be
movq 0x168(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x160(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x158(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x148(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x140(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movq %rbx, %r8
movq 0x88(%rsp), %r9
callq 0x56f5
addq $-0x4, 0x8(%rbx)
leaq 0xc942(%rip), %rbx      # 0x12200
jmp 0x58cc
movl 0x68(%rsp), %eax
movl %eax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
incq %rbp
leaq 0x90(%rsp), %r15
jmp 0x579c
cmpl $0x0, 0x40(%rsp)
movq 0x140(%rsp), %r13
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %rbp
js 0x5a45
cvtsi2sdl 0x160(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
callq 0x4da5
addsd 0x40(%rsp), %xmm0
movl 0x158(%rsp), %eax
cmpl %eax, 0x28(%r13)
jl 0x5955
movq 0x18(%r13), %rsi
movsd 0x78(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jae 0x5a45
movq %r13, %rdi
movsd %xmm0, 0x40(%rsp)
callq 0xe454
movsd 0x40(%rsp), %xmm0
xorpd %xmm1, %xmm1
movapd %xmm1, 0xd0(%rsp)
andq $0x0, 0xe0(%rsp)
movapd %xmm1, 0x90(%rsp)
movapd %xmm1, 0xa0(%rsp)
movapd %xmm1, 0xb0(%rsp)
movsd %xmm0, 0xe8(%rsp)
leaq 0x48(%rsp), %rsi
movq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x99be
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x99dc
addq $-0x4, 0x8(%r14)
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x98fe
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movl 0x148(%rsp), %r8d
callq 0x4874
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
movl %ebx, -0x10(%rdi)
leaq 0x68(%rsp), %rsi
callq 0x9a9e
shrq $0x20, %rbx
leaq 0xa8(%rsp), %rdi
movl %ebx, 0x20(%rdi)
movl 0x160(%rsp), %eax
movl %eax, 0x1c(%rdi)
movq 0x168(%rsp), %rsi
callq 0x99dc
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
callq 0xe4a2
leaq 0x68(%rsp), %rdi
callq 0x9958
leaq 0x90(%rsp), %rdi
callq 0x9bb4
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5a59
movq %rax, %rbx
jmp 0x5a6b
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x9958
leaq 0x90(%rsp), %rdi
callq 0x9bb4
movq %rbx, %rdi
callq 0x34a0