// File: /workspace/github_cpp_makefile_star2_O2/tanakh[P]icfpc2015/main.cpp
// Function: rec(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>>>&, candidate&, int, int)
void rec(const vector<vector<int>> &bd, const unit &u, const pt &pos, int rot,
         vector<int> &hist, set<pair<pair<int,int>, int>> &ss, candidate &cand, int ls_old, int prev)
{
  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,4,5,2,3},
  };

  const int *ord = tbl[3];

  // const int *ord = tbl[0];
  // if (prev == E) ord = tbl[1];
  // if (prev == SW) ord = tbl[2];

  // 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(bd, u, npos, nrot, hist, ss, cand, ls_old, mov);
    hist.pop_back();
  }

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

    if (tscore > cand.score) {
      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;

      cand = tmp;
    }
  }

  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %r13
movq %r8, %rbp
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdx), %rax
leaq 0xc8(%rsp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
movq %r9, %rdi
callq 0x99a4
testq %rax, %rax
je 0x5465
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x28(%rsp)
leaq 0xc8(%rsp), %rsi
movq %r13, %rdi
callq 0xe0b2
orl $-0x1, 0x10(%rsp)
leal -0x1(%r14), %eax
movq %rax, 0x48(%rsp)
leal 0x1(%r14), %eax
movl %eax, 0x14(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
leaq 0x60(%rsp), %rsi
movq %r15, %r8
movq %r15, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r14, 0x50(%rsp)
cmpq $0x6, %rax
je 0x55d9
movq %rax, 0x58(%rsp)
leaq 0xccda(%rip), %rcx      # 0x121a0
movl 0x48(%rcx,%rax,4), %edx
movl %edx, 0x30(%rsp)
movq (%r12), %rax
movq %rax, 0x60(%rsp)
movl %r14d, %r12d
cmpq $0x5, %rdx
ja 0x5536
movq %rax, %rcx
shrq $0x20, %rcx
leaq 0xcc22(%rip), %rdi      # 0x12110
movslq (%rdi,%rdx,4), %rdx
addq %rdi, %rdx
jmpq *%rdx
addl $-0x2, %eax
jmp 0x551c
movl 0x14(%rsp), %eax
cltd
idivl 0x20(%r8)
jmp 0x5533
decl %eax
jmp 0x550d
incl %eax
movl %eax, 0x60(%rsp)
incl %ecx
movl %ecx, 0x64(%rsp)
jmp 0x5520
addl $0x2, %eax
movl %eax, 0x60(%rsp)
movl %r14d, %r12d
jmp 0x5536
movl 0x20(%r8), %ecx
movq 0x48(%rsp), %rax
addl %ecx, %eax
cltd
idivl %ecx
movl %edx, %r12d
movq %r13, %rbx
movq %rbp, %r14
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %rsi, %r15
movq %r8, %rdx
movl %r12d, %ecx
movq %r8, %r13
callq 0x481a
testb %al, %al
je 0x55a6
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x99be
subq $0x10, %rsp
movq %rbp, %rdi
movq %r14, %rbp
movq %r13, %rsi
movq %r15, %rdx
movl %r12d, %ecx
movq %r14, %r8
movq %rbx, %r13
movq %rbx, %r9
movl 0x128(%rsp), %eax
pushq %rax
pushq 0x128(%rsp)
callq 0x5412
addq $0x20, %rsp
addq $-0x4, 0x8(%r14)
movq 0x18(%rsp), %r12
jmp 0x55bd
movl 0x30(%rsp), %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %r12
movq %r14, %rbp
movq %rbx, %r13
movq 0x58(%rsp), %rax
incq %rax
movq 0x50(%rsp), %r14
movq 0x20(%rsp), %r8
leaq 0x60(%rsp), %rsi
jmp 0x54b0
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rbx
js 0x5453
movq %rbx, %rdi
movq %r12, %rsi
movq %r8, %rdx
movl %r14d, %ecx
callq 0x4da5
movq 0x110(%rsp), %rax
ucomisd 0x58(%rax), %xmm0
jbe 0x5453
xorps %xmm1, %xmm1
movaps %xmm1, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm1, 0x70(%rsp)
movaps %xmm1, 0x80(%rsp)
movsd %xmm0, 0xb8(%rsp)
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
callq 0x99be
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x99dc
addq $-0x4, 0x8(%rbp)
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x98fe
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
movl 0x118(%rsp), %r8d
callq 0x4874
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
movl %ebx, -0x10(%rdi)
leaq 0x30(%rsp), %rsi
callq 0x9a9e
shrq $0x20, %rbx
leaq 0x60(%rsp), %rsi
movl %ebx, 0x38(%rsi)
movq 0x110(%rsp), %rdi
callq 0x9b66
leaq 0x30(%rsp), %rdi
callq 0x9958
leaq 0x60(%rsp), %rdi
callq 0x9bb4
jmp 0x5453
jmp 0x56d1
movq %rax, %rbx
jmp 0x56e3
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x9958
leaq 0x60(%rsp), %rdi
callq 0x9bb4
movq %rbx, %rdi
callq 0x34a0