// File: 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 %r14
pushq %rbx
subq $0x1f0, %rsp            # imm = 0x1F0
movq 0x238(%rsp), %rax
movl 0x230(%rsp), %eax
movl 0x228(%rsp), %eax
movl 0x220(%rsp), %eax
movl 0x218(%rsp), %eax
movq 0x210(%rsp), %rax
movq %rdi, 0x1e8(%rsp)
movq %rsi, 0x1e0(%rsp)
movq %rdx, 0x1d8(%rsp)
movl %ecx, 0x1d4(%rsp)
movq %r8, 0x1c8(%rsp)
movq %r9, 0x1c0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x11920
movl %eax, 0x1a8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x11930
movl %eax, 0x1a4(%rsp)
leaq 0x1a8(%rsp), %rdi
leaq 0x1a4(%rsp), %rsi
callq 0x118f0
movq %rax, 0x1ac(%rsp)
leaq 0x1ac(%rsp), %rdi
leaq 0x1d4(%rsp), %rsi
callq 0x126a0
movl %edx, 0x198(%rsp)
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rax
movq %rax, 0x1b4(%rsp)
movl 0x198(%rsp), %eax
movl %eax, 0x1bc(%rsp)
movq 0x1c0(%rsp), %rdi
leaq 0x1b4(%rsp), %rsi
callq 0x126f0
cmpq $0x0, %rax
je 0x90fd
jmp 0x9691
movq 0x1c0(%rsp), %rdi
leaq 0x1b4(%rsp), %rsi
callq 0x12750
movq %rax, 0x180(%rsp)
movb %dl, 0x188(%rsp)
movl $0xffffffff, 0x17c(%rsp) # imm = 0xFFFFFFFF
leaq 0x2d09d(%rip), %rax     # 0x361d0
addq $0x48, %rax
movq %rax, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
cmpl $0x6, 0x16c(%rsp)
jge 0x9426
movq 0x170(%rsp), %rax
movslq 0x16c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x168(%rsp)
movq 0x1d8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x160(%rsp)
movl 0x1d4(%rsp), %eax
movl %eax, 0x15c(%rsp)
movl 0x168(%rsp), %eax
movq %rax, 0x58(%rsp)
subq $0x5, %rax
ja 0x9336
movq 0x58(%rsp), %rax
leaq 0x2cf43(%rip), %rcx     # 0x360f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xfffffffe, 0x150(%rsp) # imm = 0xFFFFFFFE
movl $0x0, 0x14c(%rsp)
leaq 0x154(%rsp), %rdi
leaq 0x150(%rsp), %rsi
leaq 0x14c(%rsp), %rdx
callq 0x11530
leaq 0x160(%rsp), %rdi
leaq 0x154(%rsp), %rsi
callq 0x127b0
jmp 0x9336
movl $0x2, 0x140(%rsp)
movl $0x0, 0x13c(%rsp)
leaq 0x144(%rsp), %rdi
leaq 0x140(%rsp), %rsi
leaq 0x13c(%rsp), %rdx
callq 0x11530
leaq 0x160(%rsp), %rdi
leaq 0x144(%rsp), %rsi
callq 0x127b0
jmp 0x9336
movl $0xffffffff, 0x130(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x12c(%rsp)
leaq 0x134(%rsp), %rdi
leaq 0x130(%rsp), %rsi
leaq 0x12c(%rsp), %rdx
callq 0x11530
leaq 0x160(%rsp), %rdi
leaq 0x134(%rsp), %rsi
callq 0x127b0
jmp 0x9336
movl $0x1, 0x120(%rsp)
movl $0x1, 0x11c(%rsp)
leaq 0x124(%rsp), %rdi
leaq 0x120(%rsp), %rsi
leaq 0x11c(%rsp), %rdx
callq 0x11530
leaq 0x160(%rsp), %rdi
leaq 0x124(%rsp), %rsi
callq 0x127b0
jmp 0x9336
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movq 0x1e0(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x15c(%rsp)
jmp 0x9336
movl 0x15c(%rsp), %eax
movq 0x1e0(%rsp), %rcx
addl 0x20(%rcx), %eax
subl $0x1, %eax
movq 0x1e0(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x15c(%rsp)
movq 0x1e8(%rsp), %rdi
movq 0x1e0(%rsp), %rdx
movl 0x15c(%rsp), %ecx
leaq 0x160(%rsp), %rsi
callq 0x6e60
testb $0x1, %al
jne 0x9371
movl 0x168(%rsp), %eax
movl %eax, 0x17c(%rsp)
jmp 0x9410
movq 0x1c8(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x12800
movq 0x1e8(%rsp), %rdi
movq 0x1e0(%rsp), %rsi
movl 0x15c(%rsp), %ecx
movq 0x1c8(%rsp), %r8
movq 0x1c0(%rsp), %r9
movq 0x210(%rsp), %r14
movl 0x218(%rsp), %ebp
movl 0x168(%rsp), %ebx
movl 0x228(%rsp), %r11d
movl 0x230(%rsp), %r10d
movq 0x238(%rsp), %rax
leaq 0x160(%rsp), %rdx
movq %r14, (%rsp)
movl %ebp, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x8ff0
movq 0x1c8(%rsp), %rdi
callq 0x12870
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
jmp 0x914a
cmpl $0x0, 0x17c(%rsp)
jl 0x968f
cvtsi2sdl 0x230(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x1e8(%rsp), %rdi
movq 0x1d8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movl 0x1d4(%rsp), %ecx
movl 0x218(%rsp), %r8d
movq 0x1c8(%rsp), %r9
callq 0x7b90
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movq 0x210(%rsp), %rdi
callq 0x12f20
cmpl 0x228(%rsp), %eax
jl 0x9516
movq 0x210(%rsp), %rdi
callq 0x12f30
movq %rax, 0x108(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x12f60
movsd 0x58(%rax), %xmm0
ucomisd 0x110(%rsp), %xmm0
jb 0x94da
jmp 0x9691
movq 0x210(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x210(%rsp), %rdi
callq 0x12f30
movq 0x48(%rsp), %rdi
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rsi
callq 0x12f90
movq %rax, 0xf8(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x12890
movsd 0x110(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq 0x1c8(%rsp), %rdi
leaq 0x17c(%rsp), %rsi
callq 0x12800
jmp 0x954c
movq 0x1c8(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x128e0
jmp 0x9563
movq 0x1c8(%rsp), %rdi
callq 0x12870
movq 0x1e8(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x12250
jmp 0x9584
movq 0x1d8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movl 0x1d4(%rsp), %ecx
movl 0x218(%rsp), %r8d
leaq 0x70(%rsp), %rdi
callq 0x6f10
movq %rax, 0x40(%rsp)
jmp 0x95b4
movq 0x40(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0xc8(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x12ba0
jmp 0x95dd
movl 0x6c(%rsp), %eax
movl %eax, 0xd0(%rsp)
movl 0x230(%rsp), %eax
movl %eax, 0xcc(%rsp)
movq 0x238(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x128e0
jmp 0x960d
movq 0x210(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x12fe0
movq %rax, 0x38(%rsp)
jmp 0x9629
movq 0x38(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x12350
leaq 0x98(%rsp), %rdi
callq 0x12ee0
jmp 0x968f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x9680
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x12350
leaq 0x98(%rsp), %rdi
callq 0x12ee0
jmp 0x969d
jmp 0x9691
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbx
popq %r14
popq %rbp
retq
movq 0x90(%rsp), %rdi
callq 0x4620
nopw (%rax,%rax)