// File: /workspace/github_cpp_makefile_star2_O1/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 $0x188, %rsp            # imm = 0x188
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
movb 0x1c0(%rsp), %bpl
leaq 0x1734e(%rip), %rdi     # 0x1e840
movq %r9, %rsi
callq 0xc260
leaq 0xd8(%rsp), %rdi
movq %r15, 0x70(%rsp)
movq %r15, %rsi
callq 0x666c
testb %bpl, %bpl
je 0x7521
leaq 0xd8(%rsp), %rdi
callq 0x54bd
movq %r14, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x6763
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x70(%rsp), %rbp
cmpl $0x0, 0x40(%rbp)
jle 0x75dc
xorl %r14d, %r14d
movabsq $-0x3333333333333333, %r13 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x30(%rsp), %r15
leaq 0xf0(%rsp), %r12
movl %ebx, %eax
shrl $0x10, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq 0x10(%rbp), %rcx
subq 0x8(%rbp), %rcx
sarq $0x3, %rcx
imulq %r13, %rcx
xorl %edx, %edx
divq %rcx
movl %edx, 0xf0(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x75bc
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x75c7
movq %r15, %rdi
movq %r12, %rdx
callq 0x1023a
imull $0x41c64e6d, %ebx, %ebx # imm = 0x41C64E6D
addl $0x3039, %ebx           # imm = 0x3039
incl %r14d
cmpl 0x40(%rbp), %r14d
jl 0x757d
leaq 0x160(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x30(%rsp), %rax
movslq (%rax), %rax
leaq (%rax,%rax,4), %rbx
shlq $0x3, %rbx
addq 0x8(%rbp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5816
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xd8(%rsp), %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x5062
testb %al, %al
movq 0xa0(%rsp), %r14
je 0x7a57
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
shrq $0x2, %rax
testl %eax, %eax
jle 0x7a78
leaq 0x8(%rbp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0xd8(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq $0x0, 0xa8(%rsp)
movl $0x0, 0x24(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
testl %ecx, %ecx
jle 0x7884
xorl %r14d, %r14d
movl $0x0, 0x20(%rsp)
movq %r14, %rbp
shlq $0x5, %rbp
cmpq $0x14, 0x8(%rax,%rbp)
ja 0x7853
movl $0x1, %r15d
movl %r14d, %ecx
shll %cl, %r15d
movl 0x24(%rsp), %eax
btl %r14d, %eax
jb 0x7853
movl %ebx, 0x2c(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0xb8(%rsp)
movl %r13d, 0x28(%rsp)
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0xbb66
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
leaq 0x158(%rsp), %rsi
callq 0xefcc
movq 0xc0(%rsp), %rsi
addq %rbp, %rsi
subq $0x8, %rsp
movq 0x78(%rsp), %rdi
movq 0xb8(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x34(%rsp), %r8
leaq 0xc0(%rsp), %r9
pushq %r12
leaq 0x90(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x71b2
addq $0x20, %rsp
movl %eax, %r12d
testl %eax, %eax
js 0x782c
movq 0xc0(%rsp), %rax
movq 0x8(%rax,%rbp), %rbx
testq %rbx, %rbx
je 0x77dc
movq (%rax,%rbp), %r13
xorl %ebp, %ebp
movsbl (%r13,%rbp), %edi
callq 0x4ceb
movl %eax, 0x7c(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x77c5
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x77d4
leaq 0x50(%rsp), %rdi
leaq 0x7c(%rsp), %rdx
callq 0x1023a
incq %rbp
cmpq %rbp, %rbx
jne 0x779d
movl 0x2c(%rsp), %ebx
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x28(%rsp), %r13d
leaq 0xd8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xbd22
leaq 0x158(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0xf1ac
orl %r15d, 0x24(%rsp)
addl %r12d, 0xc(%rsp)
movb $0x1, %al
movl %eax, 0x20(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0xf43c
leaq 0x80(%rsp), %rdi
callq 0xbbf8
testl %r12d, %r12d
leaq 0xd8(%rsp), %r12
jns 0x7879
incq %r14
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x76c7
movq 0x70(%rsp), %rbp
movl 0x20(%rsp), %ecx
jmp 0x7886
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
testb $0x1, %cl
jne 0x7a3f
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
xorl %eax, %eax
movq %rax, 0x90(%rsp)
leaq 0x130(%rsp), %r15
movq %rax, 0x10(%r15)
movups %xmm0, (%r15)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movabsq $-0x3dfd5fa0e0000000, %rax # imm = 0xC202A05F20000000
movq %rax, 0x148(%rsp)
movslq %ebx, %rax
movq 0x30(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
movq 0xb0(%rsp), %rax
addq (%rax), %rsi
subq $0x10, %rsp
movq %r12, %rdi
leaq 0x28(%rsp), %rdx
movl %r13d, %ecx
leaq 0x90(%rsp), %r8
leaq 0x168(%rsp), %r9
pushq 0xb8(%rsp)
leaq 0x108(%rsp), %rax
pushq %rax
callq 0x5f8e
addq $0x20, %rsp
movq %r12, %rdi
movq %r15, %rsi
callq 0xbd22
movq 0xf0(%rsp), %r12
movq 0xf8(%rsp), %r14
cmpq %r14, %r12
je 0x798f
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x797c
movl (%r12), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x7989
leaq 0x50(%rsp), %rdi
movq %r12, %rdx
callq 0x1098a
addq $0x4, %r12
jmp 0x795a
movl 0xc(%rsp), %eax
addl 0x120(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x128(%rsp), %eax
movq %rax, 0xa8(%rsp)
incl %ebx
leaq 0x158(%rsp), %rdi
callq 0x11d86
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
cmpl %ecx, %ebx
jge 0x79f6
movslq %ebx, %rcx
movslq (%rax,%rcx,4), %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0x8(%rbp), %rsi
movq %rbp, %rdi
callq 0x5816
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0xbbf8
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
leaq 0xd8(%rsp), %r12
je 0x7a18
callq 0x32f0
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x7a2a
callq 0x32f0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7a3c
callq 0x32f0
xorl %r13d, %r13d
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
shrq $0x2, %rax
cmpl %eax, %ebx
jl 0x769d
jmp 0x7a80
leaq 0xdc74(%rip), %rsi      # 0x156d2
leaq 0xf0(%rsp), %rdx
movq %r14, %rdi
callq 0x3110
movl $0x0, 0x20(%r14)
jmp 0x7ae9
movl $0x0, 0xc(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x5e03
movq 0xa0(%rsp), %r14
leaq 0xf0(%rsp), %rdi
callq 0x686e
movl %eax, %ebx
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0xf0(%rsp), %rsi
movq 0xf8(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeda4
addl 0xc(%rsp), %ebx
movl %ebx, 0x20(%r14)
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7ae9
callq 0x32f0
leaq 0x158(%rsp), %rdi
callq 0xf43c
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7b05
callq 0x32f0
leaq 0xc0(%rsp), %rdi
callq 0xc17a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x7b21
callq 0x32f0
leaq 0xd8(%rsp), %rdi
callq 0xbbf8
movq %r14, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7b69
jmp 0x7b69
movq %rax, %rbx
jmp 0x7c20
movq %rax, %rbx
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7be8
jmp 0x7bb8
movq %rax, %rbx
jmp 0x7be8
movq %rax, %rbx
jmp 0x7c11
movq %rax, %rbx
jmp 0x7bf5
jmp 0x7b9b
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x7bdb
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x7be8
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0xbe84
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7be8
callq 0x32f0
jmp 0x7be8
jmp 0x7bc1
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0xf43c
leaq 0x80(%rsp), %rdi
callq 0xbbf8
leaq 0x158(%rsp), %rdi
callq 0xf43c
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7c04
callq 0x32f0
leaq 0xc0(%rsp), %rdi
callq 0xc17a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x7c20
callq 0x32f0
leaq 0xd8(%rsp), %rdi
callq 0xbbf8
movq %rbx, %rdi
callq 0x34b0