// File: 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:
subq $0x198, %rsp            # imm = 0x198
movl 0x1b0(%rsp), %eax
movl 0x1a8(%rsp), %eax
movq 0x1a0(%rsp), %rax
movq %rdi, 0x190(%rsp)
movq %rsi, 0x188(%rsp)
movq %rdx, 0x180(%rsp)
movl %ecx, 0x17c(%rsp)
movq %r8, 0x170(%rsp)
movq %r9, 0x168(%rsp)
movq 0x180(%rsp), %rdi
callq 0x11920
movl %eax, 0x150(%rsp)
movq 0x180(%rsp), %rdi
callq 0x11930
movl %eax, 0x14c(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x14c(%rsp), %rsi
callq 0x118f0
movq %rax, 0x154(%rsp)
leaq 0x154(%rsp), %rdi
leaq 0x17c(%rsp), %rsi
callq 0x126a0
movl %edx, 0x140(%rsp)
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x15c(%rsp)
movl 0x140(%rsp), %eax
movl %eax, 0x164(%rsp)
movq 0x168(%rsp), %rdi
leaq 0x15c(%rsp), %rsi
callq 0x126f0
cmpq $0x0, %rax
je 0x8b43
jmp 0x8fd9
movq 0x168(%rsp), %rdi
leaq 0x15c(%rsp), %rsi
callq 0x12750
movq %rax, 0x128(%rsp)
movb %dl, 0x130(%rsp)
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
leaq 0x2d5f7(%rip), %rax     # 0x36170
addq $0x48, %rax
movq %rax, 0x118(%rsp)
movl $0x0, 0x114(%rsp)
cmpl $0x6, 0x114(%rsp)
jge 0x8e44
movq 0x118(%rsp), %rax
movslq 0x114(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x110(%rsp)
movq 0x180(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movl 0x17c(%rsp), %eax
movl %eax, 0x104(%rsp)
movl 0x110(%rsp), %eax
movq %rax, 0x20(%rsp)
subq $0x5, %rax
ja 0x8d7c
movq 0x20(%rsp), %rax
leaq 0x2d4e5(%rip), %rcx     # 0x360e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xfffffffe, 0xf8(%rsp) # imm = 0xFFFFFFFE
movl $0x0, 0xf4(%rsp)
leaq 0xfc(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0xf4(%rsp), %rdx
callq 0x11530
leaq 0x108(%rsp), %rdi
leaq 0xfc(%rsp), %rsi
callq 0x127b0
jmp 0x8d7c
movl $0x2, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
leaq 0xec(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0xe4(%rsp), %rdx
callq 0x11530
leaq 0x108(%rsp), %rdi
leaq 0xec(%rsp), %rsi
callq 0x127b0
jmp 0x8d7c
movl $0xffffffff, 0xd8(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xd4(%rsp)
leaq 0xdc(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0xd4(%rsp), %rdx
callq 0x11530
leaq 0x108(%rsp), %rdi
leaq 0xdc(%rsp), %rsi
callq 0x127b0
jmp 0x8d7c
movl $0x1, 0xc8(%rsp)
movl $0x1, 0xc4(%rsp)
leaq 0xcc(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xc4(%rsp), %rdx
callq 0x11530
leaq 0x108(%rsp), %rdi
leaq 0xcc(%rsp), %rsi
callq 0x127b0
jmp 0x8d7c
movl 0x104(%rsp), %eax
addl $0x1, %eax
movq 0x188(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x104(%rsp)
jmp 0x8d7c
movl 0x104(%rsp), %eax
movq 0x188(%rsp), %rcx
addl 0x20(%rcx), %eax
subl $0x1, %eax
movq 0x188(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x104(%rsp)
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rdx
movl 0x104(%rsp), %ecx
leaq 0x108(%rsp), %rsi
callq 0x6e60
testb $0x1, %al
jne 0x8db4
movl 0x110(%rsp), %eax
movl %eax, 0x124(%rsp)
jmp 0x8e2e
movq 0x170(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x12800
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rsi
movl 0x104(%rsp), %ecx
movq 0x170(%rsp), %r8
movq 0x168(%rsp), %r9
movq 0x1a0(%rsp), %r11
movl 0x1a8(%rsp), %r10d
movl 0x110(%rsp), %eax
leaq 0x108(%rsp), %rdx
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x8a50
movq 0x170(%rsp), %rdi
callq 0x12870
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x8b90
cmpl $0x0, 0x124(%rsp)
jl 0x8fd7
movq 0x190(%rsp), %rdi
movq 0x180(%rsp), %rsi
movq 0x188(%rsp), %rdx
movl 0x17c(%rsp), %ecx
movl 0x1a8(%rsp), %r8d
movq 0x170(%rsp), %r9
callq 0x7b90
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
movq 0x1a0(%rsp), %rax
ucomisd 0x58(%rax), %xmm0
jbe 0x8fd5
leaq 0x58(%rsp), %rdi
callq 0x12890
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x170(%rsp), %rdi
leaq 0x124(%rsp), %rsi
callq 0x12800
jmp 0x8ede
movq 0x170(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x128e0
jmp 0x8ef2
movq 0x170(%rsp), %rdi
callq 0x12870
movq 0x190(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x12250
jmp 0x8f13
movq 0x180(%rsp), %rsi
movq 0x188(%rsp), %rdx
movl 0x17c(%rsp), %ecx
movl 0x1a8(%rsp), %r8d
leaq 0x30(%rsp), %rdi
callq 0x6f10
movq %rax, 0x18(%rsp)
jmp 0x8f43
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x88(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x12ba0
jmp 0x8f6c
movl 0x2c(%rsp), %eax
movl %eax, 0x90(%rsp)
movq 0x1a0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x12e60
jmp 0x8f8b
leaq 0x30(%rsp), %rdi
callq 0x12350
leaq 0x58(%rsp), %rdi
callq 0x12ee0
jmp 0x8fd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
jmp 0x8fc9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x12350
leaq 0x58(%rsp), %rdi
callq 0x12ee0
jmp 0x8fe1
jmp 0x8fd7
jmp 0x8fd9
addq $0x198, %rsp            # imm = 0x198
retq
movq 0x50(%rsp), %rdi
callq 0x4620
nopl (%rax,%rax)