// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, 0x28(%rsp)
movq 0x8(%rsi), %r14
testq %r14, %r14
je 0x74a5
movq %r9, %rbx
movq %rcx, %r12
movq %r8, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
movq 0xb0(%rsp), %r13
movq 0xa0(%rsp), %rbp
movq (%rsi), %rax
movq %rax, 0x18(%rsp)
addq %rax, %r14
leaq 0x8(%r13), %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
leaq 0x40(%rsp), %r15
movq %rcx, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movq (%rbx), %rax
movq %rax, 0x40(%rsp)
movl (%rbp), %eax
movl %eax, 0x48(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x10798
cmpq 0x50(%rsp), %rax
je 0x7246
xorl %r13d, %r13d
jmp 0x7480
movq %r13, %rdi
movq %r15, %rsi
callq 0x10848
movq (%rbx), %rax
movq %rax, 0x8(%rsp)
movl (%rbp), %r13d
movq 0x30(%rsp), %rax
movslq (%rax), %rax
movq (%r12), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %r15
shlq $0x3, %r15
movq 0x38(%rsp), %rax
addq (%rax), %r15
movq 0x18(%rsp), %rax
movsbl (%rax), %edi
callq 0x4ceb
cmpl $0x5, %eax
ja 0x72fc
movl %eax, %eax
leaq 0xdee8(%rip), %rcx      # 0x15180
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq 0xa8(%rsp), %rdi
jmpq *%rax
addl $-0x2, 0x8(%rsp)
jmp 0x7304
incl %r13d
movl %r13d, %eax
cltd
idivl 0x20(%r15)
jmp 0x72f7
movq 0x8(%rsp), %xmm0
paddd 0xdda5(%rip), %xmm0     # 0x15070
jmp 0x72db
movq 0x8(%rsp), %xmm0
psubd 0xdda5(%rip), %xmm0     # 0x15080
movq %xmm0, 0x8(%rsp)
jmp 0x7304
addl $0x2, 0x8(%rsp)
jmp 0x7304
movl 0x20(%r15), %ecx
leal (%rcx,%r13), %eax
decl %eax
cltd
idivl %ecx
movl %edx, %r13d
jmp 0x7304
movq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
movl %r13d, %ecx
callq 0x5062
testb %al, %al
je 0x732c
movq 0x8(%rsp), %rax
movq %rax, (%rbx)
movl %r13d, (%rbp)
movb $0x1, %r13b
jmp 0x73ba
movq 0x28(%rsp), %rax
movl 0x24(%rax), %r12d
movq (%r15), %r13
movq 0x8(%r15), %rax
movq %rax, 0x58(%rsp)
cmpq %rax, %r13
je 0x73a7
movq (%r13), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movl 0x4(%rbx), %edx
addl %edx, %ecx
movq 0x18(%r15), %rsi
subl %esi, %eax
movq %r15, %r14
movq %rbx, %r15
movq %rsi, %rbx
shrq $0x20, %rbx
addl %edx, %ebx
movl (%rbp), %esi
subl %ebx, %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xc88c
shrq $0x20, %rax
addl %ebx, %eax
movq %r15, %rbx
movq %r14, %r15
cmpl %r12d, %eax
cmovgel %r12d, %eax
addq $0x8, %r13
movl %eax, %r12d
cmpq 0x58(%rsp), %r13
jne 0x7346
cmpl $0x6, %r12d
movq 0x38(%rsp), %r14
jge 0x73c4
xorl %r13d, %r13d
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %r14
jmp 0x747b
movl (%rbp), %ecx
xorl %r13d, %r13d
movq 0xa8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0x514f
addl %eax, 0x4(%rsp)
movq 0x30(%rsp), %r15
movl (%r15), %eax
incl %eax
movl %eax, (%r15)
movq 0x20(%rsp), %r12
movq 0x8(%r12), %rcx
subq (%r12), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x746e
movq 0xb0(%rsp), %rdi
callq 0x11d86
movslq (%r15), %rax
movq (%r12), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq (%r14), %rsi
movq 0x28(%rsp), %rdi
callq 0x5816
movq %rax, (%rbx)
movq 0xa0(%rsp), %rax
movl $0x0, (%rax)
movslq (%r15), %rax
movq (%r12), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq (%r14), %rdx
movq %rbp, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x5062
movl %eax, %r13d
movq 0x10(%rsp), %r14
movq 0xa0(%rsp), %rbp
leaq 0x40(%rsp), %r15
testb %r13b, %r13b
je 0x74af
movq 0x18(%rsp), %rax
incq %rax
movq %rax, 0x18(%rsp)
cmpq %r14, %rax
movq 0xb0(%rsp), %r13
jne 0x721d
jmp 0x74b7
movl $0x0, 0x4(%rsp)
jmp 0x74b7
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl 0x4(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq