// File: /workspace/github_cpp_makefile_star2_O3/tanakh[P]icfpc2015/main.cpp
// Function: solve[abi:cxx11](problem const&, int, int, int, std::vector<double, std::allocator<double>> const&, bool)
pair<string, int> solve(const problem &prob, int seed, int tle, int mle,
                        const vector<double> &param, bool progress)
{
  eparam = param;

  auto bd = make_board(prob);
  if (progress) print_board(bd);

  int move_score = 0;
  vector<int> moves;

  int ls_old = 0;

  int word_used = 0;

  auto words = get_words();

  rng r(seed);
  vector<int> unit_ids;
  for (int i = 0; i < prob.source_length; i++)
    unit_ids.push_back(r.get() % prob.units.size());

  set<pair<pair<int,int>, int>> current_poss;
  int turn = 0;
  pt pos = get_init_pos(prob, prob.units[unit_ids[turn]]);
  int rot = 0;

  if (!check(bd, pos, prob.units[unit_ids[turn]], rot))
    return make_pair("", 0);

  for (; turn < (int)unit_ids.size(); ) {

    bool injected = false;

    for (int j = 0; j < (int)words.size(); j++) {
      if (words[j].length() > 20) continue;

      if (word_used & (1 << j)) continue;

      // if (genrand_int31() < 0.5) continue;

      int tmp_turn = turn;
      pt tmp_pos = pos;
      int tmp_rot = rot;
      vector<vector<int>> tmp_bd = bd;
      set<pair<pair<int,int>, int>> tmp_prevs = current_poss;

      // cerr << "*** << " << tmp_bd.size() << ", " << bd.size() << ", " << words[j] << endl;

      auto sc =
        can_use(prob, words[j], prob.units, unit_ids, tmp_turn, tmp_pos, tmp_rot, tmp_bd, tmp_prevs);

      if (sc >= 0) {
        // cerr << "FOUND: " << words[j] << endl;
        // print_board(tmp_bd);

        word_used |= (1 << j);

        for (auto &ch: words[j])
          moves.push_back(to_command(ch));
        move_score += sc;

        // update
        turn = tmp_turn;
        pos = tmp_pos;
        rot = tmp_rot;
        bd = tmp_bd;
        current_poss = tmp_prevs;

        injected = true;
        break;
      }
    }

    if (injected) continue;

    vector<int> hist;
    candidate best;
    // current_poss.erase(make_pair(pt2pair(pos), rot));
    rec(bd, prob.units[unit_ids[turn]], pos, rot, hist, current_poss, best, ls_old, CW);

    bd = best.board;
    for (auto &m: best.moves)
      moves.push_back(m);
    move_score += best.move_score;
    ls_old = best.ls_old;

    turn++;
    current_poss.clear();
    rot = 0;
    if (turn < (int)unit_ids.size())
      pos = get_init_pos(prob, prob.units[unit_ids[turn]]);
  }

  auto ms = to_ans(moves);
  return make_pair(ms, move_score + power_score(ms));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r14
movb 0x1b0(%rsp), %bpl
leaq 0x17379(%rip), %rdi     # 0x1e830
movq %r9, %rsi
callq 0xc2d0
leaq 0xc8(%rsp), %rdi
movq %r13, %rsi
callq 0x6611
testb %bpl, %bpl
je 0x74e1
leaq 0xc8(%rsp), %rdi
callq 0x54ac
movq %r14, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x670d
leaq 0x20(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
xorl %r14d, %r14d
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0xe0(%rsp), %r8
movl %ebx, %eax
shrl $0x10, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq 0x10(%r13), %rcx
subq 0x8(%r13), %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
xorl %edx, %edx
divq %rcx
movl %edx, 0xe0(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x756f
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x7582
movq %r15, %rdi
movq %r8, %rdx
callq 0x10328
leaq 0xe0(%rsp), %r8
imull $0x41c64e6d, %ebx, %ebx # imm = 0x41C64E6D
addl $0x3039, %ebx           # imm = 0x3039
incl %r14d
cmpl 0x40(%r13), %r14d
jl 0x7530
movq 0x20(%rsp), %rax
leaq 0x150(%rsp), %rcx
movl $0x0, (%rcx)
xorl %edx, %edx
movq %rdx, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rdx, 0x20(%rcx)
movslq (%rax), %rax
leaq (%rax,%rax,4), %rbx
shlq $0x3, %rbx
addq 0x8(%r13), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5805
leaq 0x38(%rsp), %rsi
movq %rax, 0x8(%rsp)
movq %rax, (%rsi)
leaq 0xc8(%rsp), %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x504f
movq 0x80(%rsp), %r14
testb %al, %al
je 0x7a42
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
testl %ecx, %ecx
jle 0x7a63
leaq 0x8(%r13), %rcx
movq %rcx, 0xa0(%rsp)
leaq 0x120(%rsp), %rbx
leaq 0xc8(%rsp), %r14
movl $0x0, 0x4(%rsp)
movq $0x0, 0x88(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, (%rsp)
xorl %r15d, %r15d
movq %r13, 0x90(%rsp)
movq 0xb0(%rsp), %rdx
movq 0xb8(%rsp), %rsi
movq %rsi, %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
testl %ecx, %ecx
jle 0x787c
xorl %ebx, %ebx
movq $0x0, 0x98(%rsp)
movq %rbx, %rbp
shlq $0x5, %rbp
cmpq $0x14, 0x8(%rdx,%rbp)
ja 0x7836
movl $0x1, %r12d
movl %ebx, %ecx
shll %cl, %r12d
movl 0x14(%rsp), %eax
btl %ebx, %eax
jb 0x7836
movl (%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl %r15d, 0x18(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xbb1c
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
leaq 0x148(%rsp), %rsi
callq 0xf106
movq 0xb0(%rsp), %rsi
addq %rbp, %rsi
subq $0x8, %rsp
movq %r13, %rdi
movq 0xa8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0xb0(%rsp), %r9
pushq %r14
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
callq 0x7173
addq $0x20, %rsp
movl %eax, %r13d
testl %eax, %eax
js 0x77fe
movq 0xb0(%rsp), %rax
movq 0x8(%rax,%rbp), %r14
testq %r14, %r14
je 0x77aa
movq (%rax,%rbp), %r15
xorl %ebp, %ebp
movsbl (%r15,%rbp), %edi
callq 0x4cdd
movl %eax, 0x5c(%rsp)
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x7793
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x77a2
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rdx
callq 0x10328
incq %rbp
cmpq %rbp, %r14
jne 0x776c
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x18(%rsp), %r15d
leaq 0xc8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xbcec
leaq 0x148(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xf2e6
orl %r12d, 0x14(%rsp)
addl %r13d, 0x4(%rsp)
movb $0x1, %al
movq %rax, 0x98(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xf560
leaq 0x60(%rsp), %rdi
callq 0xbbae
testl %r13d, %r13d
jns 0x7a35
movq 0xb0(%rsp), %rdx
movq 0xb8(%rsp), %rsi
movq 0x90(%rsp), %r13
incq %rbx
movq %rsi, %rax
subq %rdx, %rax
shrq $0x5, %rax
cltq
cmpq %rax, %rbx
jl 0x7698
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x38(%rsp)
testb $0x1, 0x98(%rsp)
leaq 0x120(%rsp), %rbx
leaq 0xc8(%rsp), %r14
je 0x7886
jmp 0x7a1e
movq 0x8(%rsp), %rcx
movq %rcx, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x70(%rsp)
movq %rcx, 0x10(%rbx)
movups %xmm0, (%rbx)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movabsq $-0x3dfd5fa0e0000000, %rcx # imm = 0xC202A05F20000000
movq %rcx, 0x138(%rsp)
movslq (%rsp), %rcx
movslq (%rax,%rcx,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
movq 0xa0(%rsp), %rax
addq (%rax), %rsi
subq $0x10, %rsp
movq %r14, %rdi
leaq 0x48(%rsp), %rdx
movl %r15d, %ecx
leaq 0x70(%rsp), %r8
leaq 0x158(%rsp), %r9
pushq 0x98(%rsp)
leaq 0xf8(%rsp), %rax
pushq %rax
callq 0x5f45
addq $0x20, %rsp
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbcec
movq 0xe0(%rsp), %r15
movq 0xe8(%rsp), %rbx
cmpq %rbx, %r15
je 0x7965
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x7952
movl (%r15), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x795f
leaq 0x40(%rsp), %rdi
movq %r15, %rdx
callq 0x10aaa
addq $0x4, %r15
jmp 0x7931
movl 0x4(%rsp), %eax
addl 0x110(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x118(%rsp), %eax
movq %rax, 0x88(%rsp)
movl (%rsp), %ebx
incl %ebx
leaq 0x148(%rsp), %rdi
callq 0x11e36
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movl %ebx, (%rsp)
cmpl %ecx, %ebx
jge 0x79d3
movslq (%rsp), %rcx
movslq (%rax,%rcx,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0x8(%r13), %rsi
movq %r13, %rdi
callq 0x5805
movq %rax, 0x8(%rsp)
movq %rax, 0x38(%rsp)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0xbbae
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x79f5
callq 0x32f0
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x7a07
callq 0x32f0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7a16
callq 0x32f0
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
cmpl %ecx, (%rsp)
jl 0x7668
jmp 0x7a6b
movq 0x90(%rsp), %r13
jmp 0x784e
leaq 0xdc69(%rip), %rsi      # 0x156b2
leaq 0xe0(%rsp), %rdx
movq %r14, %rdi
callq 0x3110
movl $0x0, 0x20(%r14)
jmp 0x7ad4
movl $0x0, 0x4(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5df8
movq 0x80(%rsp), %r14
leaq 0xe0(%rsp), %rdi
callq 0x6821
movl %eax, %ebx
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0xe0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeed8
addl 0x4(%rsp), %ebx
movl %ebx, 0x20(%r14)
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7ad4
callq 0x32f0
leaq 0x148(%rsp), %rdi
callq 0xf560
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x7af0
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0xc178
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7b0c
callq 0x32f0
leaq 0xc8(%rsp), %rdi
callq 0xbbae
movq %r14, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7b63
jmp 0x7b63
movq %rax, %rbx
jmp 0x7bdd
movq %rax, %rbx
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7ba5
jmp 0x7b82
jmp 0x7b63
movq %rax, %rbx
jmp 0x7bce
movq %rax, %rbx
jmp 0x7bb2
jmp 0x7b68
movq %rax, %rbx
jmp 0x7b9b
movq %rax, %rbx
jmp 0x7ba5
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xbe74
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7ba5
callq 0x32f0
jmp 0x7ba5
jmp 0x7b8b
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xf560
leaq 0x60(%rsp), %rdi
callq 0xbbae
leaq 0x148(%rsp), %rdi
callq 0xf560
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x7bc1
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0xc178
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7bdd
callq 0x32f0
leaq 0xc8(%rsp), %rdi
callq 0xbbae
movq %rbx, %rdi
callq 0x34a0