// File: /workspace/github_cpp_makefile_star2_O2/tanakh[P]icfpc2015/main.cpp
// Function: can_use(problem const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unit, std::allocator<unit>> const&, std::vector<int, std::allocator<int>> const&, int&, std::complex<int>&, int&, std::vector<std::vector<int, std::allocator<int>>, std::allocator<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>>>&)
int can_use(const problem &prob,
            const string &seq, const vector<unit> &units, const vector<int> &unit_ord,
            int &turn, pt &pos, int &rot,
            vector<vector<int>> &bd,
            set<pair<pair<int,int>, int>> &prevs)
{

  int score = 0;

  for (auto &c: seq) {
    pair<pair<int,int>, int> cc(pt2pair(pos), rot);

    if (prevs.count(cc)) return -1;
    prevs.insert(cc);

    pt npos = pos;
    int nrot = rot;

    const unit &u = units[unit_ord[turn]];
    command mov = to_command(c);
    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)) {
      pos = npos;
      rot = nrot;
    }
    else {
      // cerr << "*** " << seq << endl;
      // print_board(bd);
      // print_board_(bd, pos, u, rot);

      int highest = prob.height;
      for (auto &c: u.members) {
        auto d = rot_pivot(c + pos, u.pivot + pos, rot);
        highest = min(highest, d.imag());
      }

      if (highest <= 5) return -1;

      score += put_unit(bd, pos, u, rot, 0).first;
      turn++;
      if (turn >= (int)unit_ord.size())
        return -1;
      prevs.clear();

      pos = get_init_pos(prob, units[unit_ord[turn]]);
      rot = 0;

      if (!check(bd, pos, units[unit_ord[turn]], rot))
        return -1;
    }
  }

  return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, %rbp
movq %rdx, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xb0(%rsp), %r14
movq 0xa0(%rsp), %r15
movq (%rsi), %r12
movq 0x8(%rsi), %rax
addq %r12, %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x18(%rsp), %r13
movq %rcx, 0x40(%rsp)
cmpq 0x48(%rsp), %r12
je 0x644a
movq (%rbx), %rax
movq %rax, 0x18(%rsp)
movl (%r15), %eax
movl %eax, 0x20(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x99a4
testq %rax, %rax
jne 0x6445
movq %r14, %rdi
movq %r13, %rsi
callq 0xe0b2
movq (%rbx), %rax
movq %rax, 0x8(%rsp)
movl (%r15), %r14d
movq 0x38(%rsp), %rax
movslq (%rax), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
movq %r12, %rcx
imulq $0x28, %rax, %r12
movq 0x30(%rsp), %rax
addq (%rax), %r12
movq %rcx, 0x50(%rsp)
movsbl (%rcx), %edi
callq 0x44ae
cmpl $0x5, %eax
ja 0x62dd
movl %eax, %eax
leaq 0xbec9(%rip), %rcx      # 0x12140
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq 0xa8(%rsp), %rdi
jmpq *%rax
addl $-0x2, 0x8(%rsp)
jmp 0x62e5
incl %r14d
movl %r14d, %eax
cltd
idivl 0x20(%r12)
jmp 0x62d8
movq 0x8(%rsp), %xmm0
paddd 0xbd85(%rip), %xmm0     # 0x12030
jmp 0x62bb
movq 0x8(%rsp), %xmm0
psubd 0xbd85(%rip), %xmm0     # 0x12040
movq %xmm0, 0x8(%rsp)
jmp 0x62e5
addl $0x2, 0x8(%rsp)
jmp 0x62e5
movl 0x20(%r12), %ecx
leal (%r14,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %edx, %r14d
jmp 0x62e5
movq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rdx
movl %r14d, %ecx
callq 0x481a
testb %al, %al
je 0x6309
movq 0x8(%rsp), %rax
movq %rax, (%rbx)
movl %r14d, (%r15)
jmp 0x642b
movq 0x28(%rsp), %rax
movl 0x24(%rax), %ebp
movq (%r12), %r14
movq 0x8(%r12), %r13
cmpq %r13, %r14
je 0x6378
movq (%r14), %rax
movl (%rbx), %ecx
leal (%rcx,%rax), %edx
shrq $0x20, %rax
movl 0x4(%rbx), %esi
addl %esi, %eax
shlq $0x20, %rax
orq %rax, %rdx
movq %rdx, 0x60(%rsp)
movq 0x18(%r12), %rax
addl %eax, %ecx
shrq $0x20, %rax
addl %esi, %eax
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, 0x58(%rsp)
movl (%r15), %edx
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x9666
shrq $0x20, %rax
cmpl %eax, %ebp
cmovll %ebp, %eax
addq $0x8, %r14
movl %eax, %ebp
jmp 0x631a
cmpl $0x6, %ebp
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %rbp
jl 0x6445
movl (%r15), %ecx
movq 0xa8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0x4874
movq %rax, %r14
movl (%r13), %eax
incl %eax
movl %eax, (%r13)
movq 0x8(%rbp), %rcx
subq (%rbp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x6445
movq 0xb0(%rsp), %rdi
callq 0xf440
movslq (%r13), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
imulq $0x28, %rax, %rsi
movq 0x30(%rsp), %r12
addq (%r12), %rsi
movq 0x28(%rsp), %rdi
callq 0x4d3c
movq %rax, (%rbx)
andl $0x0, (%r15)
movslq (%r13), %rax
movq (%rbp), %rcx
movslq (%rcx,%rax,4), %rax
imulq $0x28, %rax, %rdx
addq (%r12), %rdx
movq 0xa8(%rsp), %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x481a
testb %al, %al
je 0x6445
addl %r14d, 0x14(%rsp)
leaq 0x18(%rsp), %r13
movq 0x50(%rsp), %r12
incq %r12
movq 0xb0(%rsp), %r14
jmp 0x61f9
pushq $-0x1
popq %rax
jmp 0x644e
movl 0x14(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq