// File: 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:
subq $0x348, %rsp            # imm = 0x348
movq %rdi, 0xb0(%rsp)
movq %rdi, %rax
movq %rax, 0xb8(%rsp)
movb 0x350(%rsp), %al
movq %rdi, 0x340(%rsp)
movq %rsi, 0x338(%rsp)
movl %edx, 0x334(%rsp)
movl %ecx, 0x330(%rsp)
movl %r8d, 0x32c(%rsp)
movq %r9, 0x320(%rsp)
andb $0x1, %al
movb %al, 0x31f(%rsp)
movq 0x320(%rsp), %rsi
leaq 0x40ba9(%rip), %rdi     # 0x4b940
callq 0x138e0
movq 0x338(%rsp), %rsi
leaq 0x300(%rsp), %rdi
callq 0x96b0
testb $0x1, 0x31f(%rsp)
je 0xade5
leaq 0x300(%rsp), %rdi
callq 0x7440
jmp 0xadca
jmp 0xade5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb835
movl $0x0, 0x2f0(%rsp)
leaq 0x2d8(%rsp), %rdi
callq 0x13ba0
movl $0x0, 0x2d4(%rsp)
movl $0x0, 0x2d0(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0x9960
jmp 0xae22
movl 0x334(%rsp), %esi
leaq 0x2b4(%rsp), %rdi
callq 0x13bb0
jmp 0xae38
leaq 0x298(%rsp), %rdi
callq 0x13ba0
movl $0x0, 0x294(%rsp)
movl 0x294(%rsp), %eax
movq 0x338(%rsp), %rcx
cmpl 0x40(%rcx), %eax
jge 0xaf33
leaq 0x2b4(%rsp), %rdi
callq 0x13bd0
movl %eax, 0xac(%rsp)
jmp 0xae7e
movl 0xac(%rsp), %eax
cltq
movq %rax, 0xa0(%rsp)
movq 0x338(%rsp), %rdi
addq $0x8, %rdi
callq 0x13c00
movq %rax, %rcx
movq 0xa0(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x290(%rsp)
leaq 0x298(%rsp), %rdi
leaq 0x290(%rsp), %rsi
callq 0x11b30
jmp 0xaed0
jmp 0xaed2
movl 0x294(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x294(%rsp)
jmp 0xae50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb828
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb81b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb80e
leaq 0x260(%rsp), %rdi
callq 0x13c20
movl $0x0, 0x25c(%rsp)
movq 0x338(%rsp), %rax
movq %rax, 0x90(%rsp)
addq $0x8, %rax
movq %rax, 0x88(%rsp)
movslq 0x25c(%rsp), %rsi
leaq 0x298(%rsp), %rdi
callq 0x121a0
movq 0x88(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0x7a10
movq %rax, 0x98(%rsp)
jmp 0xafa6
movq 0x98(%rsp), %rax
movq %rax, 0x250(%rsp)
movl $0x0, 0x24c(%rsp)
movq 0x338(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x78(%rsp)
movslq 0x25c(%rsp), %rsi
leaq 0x298(%rsp), %rdi
callq 0x121a0
movq 0x78(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq %rax, %rdx
movl 0x24c(%rsp), %ecx
leaq 0x300(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x6e60
movb %al, 0x87(%rsp)
jmp 0xb01c
movb 0x87(%rsp), %al
testb $0x1, %al
jne 0xb0cd
jmp 0xb02d
movl $0x0, 0x234(%rsp)
leaq 0x2b75a(%rip), %rdi     # 0x36799
leaq 0x234(%rsp), %rsi
callq 0x13c30
movl %edx, 0x6c(%rsp)
movq %rax, 0x70(%rsp)
jmp 0xb057
movq 0xb0(%rsp), %rdi
movl 0x6c(%rsp), %eax
movq 0x70(%rsp), %rcx
movq %rcx, 0x220(%rsp)
movl %eax, 0x228(%rsp)
movl 0x228(%rsp), %eax
movl %eax, 0x240(%rsp)
movq 0x220(%rsp), %rax
movq %rax, 0x238(%rsp)
leaq 0x238(%rsp), %rsi
callq 0x13c70
jmp 0xb0a4
movl $0x1, 0x21c(%rsp)
jmp 0xb7b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb801
jmp 0xb0cf
movl 0x25c(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x12180
movq %rax, %rcx
movl 0x68(%rsp), %eax
cmpl %ecx, %eax
jge 0xb70e
movb $0x0, 0x21b(%rsp)
movl $0x0, 0x214(%rsp)
movl 0x214(%rsp), %eax
movl %eax, 0x64(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0x13d00
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0xb46f
movslq 0x214(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
callq 0x13d20
movq %rax, %rdi
callq 0x4570
cmpq $0x14, %rax
jbe 0xb158
jmp 0xb447
movl 0x2d0(%rsp), %eax
movl 0x214(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xb17b
jmp 0xb447
movl 0x25c(%rsp), %eax
movl %eax, 0x210(%rsp)
movq 0x250(%rsp), %rax
movq %rax, 0x208(%rsp)
movl 0x24c(%rsp), %eax
movl %eax, 0x204(%rsp)
leaq 0x1e8(%rsp), %rdi
leaq 0x300(%rsp), %rsi
callq 0x12250
jmp 0xb1be
leaq 0x1b8(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x13d40
jmp 0xb1d5
movq 0x338(%rsp), %rax
movq %rax, 0x58(%rsp)
movslq 0x214(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
callq 0x13d20
movq 0x58(%rsp), %rdi
movq %rax, %rsi
movq 0x338(%rsp), %rdx
addq $0x8, %rdx
movq %rsp, %rax
leaq 0x1b8(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x1e8(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x204(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x298(%rsp), %rcx
leaq 0x210(%rsp), %r8
leaq 0x208(%rsp), %r9
callq 0xa6b0
movl %eax, 0x60(%rsp)
jmp 0xb254
movl 0x60(%rsp), %eax
movl %eax, 0x1b4(%rsp)
cmpl $0x0, 0x1b4(%rsp)
jl 0xb411
movl 0x214(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
orl 0x2d0(%rsp), %eax
movl %eax, 0x2d0(%rsp)
movslq 0x214(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
callq 0x13d20
movq %rax, 0x1a8(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x4340
movq %rax, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x41d0
movq %rax, 0x198(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x13d70
testb $0x1, %al
jne 0xb2ee
jmp 0xb38b
leaq 0x1a0(%rsp), %rdi
callq 0x13db0
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rax
movsbl (%rax), %edi
callq 0x6a20
movl %eax, 0x54(%rsp)
jmp 0xb319
movl 0x54(%rsp), %eax
movl %eax, 0x18c(%rsp)
leaq 0x2d8(%rsp), %rdi
leaq 0x18c(%rsp), %rsi
callq 0x11b30
jmp 0xb33b
jmp 0xb33d
leaq 0x1a0(%rsp), %rdi
callq 0x13dc0
jmp 0xb2d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb45d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x13e10
jmp 0xb45d
movl 0x1b4(%rsp), %ecx
movl 0x2f0(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x2f0(%rsp)
movl 0x210(%rsp), %eax
movl %eax, 0x25c(%rsp)
movq 0x208(%rsp), %rax
movq %rax, 0x250(%rsp)
movl 0x204(%rsp), %eax
movl %eax, 0x24c(%rsp)
leaq 0x300(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x12ba0
jmp 0xb3e5
leaq 0x260(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x13de0
jmp 0xb3fc
movb $0x1, 0x21b(%rsp)
movl $0x7, 0x21c(%rsp)
jmp 0xb41c
movl $0x0, 0x21c(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x13e10
leaq 0x1e8(%rsp), %rdi
callq 0x12350
movl 0x21c(%rsp), %eax
testl %eax, %eax
je 0xb445
jmp 0xb443
jmp 0xb46f
jmp 0xb447
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0xb109
leaq 0x1e8(%rsp), %rdi
callq 0x12350
jmp 0xb801
testb $0x1, 0x21b(%rsp)
je 0xb47e
jmp 0xb0cf
leaq 0x170(%rsp), %rdi
callq 0x13ba0
leaq 0x110(%rsp), %rdi
callq 0x12890
jmp 0xb49a
movq 0x338(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x48(%rsp)
movslq 0x25c(%rsp), %rsi
leaq 0x298(%rsp), %rdi
callq 0x121a0
movq 0x48(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq %rax, %rsi
movl 0x24c(%rsp), %ecx
movl 0x2d4(%rsp), %edx
movq %rsp, %rax
movl %edx, 0x8(%rax)
leaq 0x110(%rsp), %rdx
movq %rdx, (%rax)
movl $0x4, 0x10(%rax)
leaq 0x300(%rsp), %rdi
leaq 0x250(%rsp), %rdx
leaq 0x170(%rsp), %r8
leaq 0x260(%rsp), %r9
callq 0x8a50
jmp 0xb51d
leaq 0x150(%rsp), %rsi
leaq 0x300(%rsp), %rdi
callq 0x12ba0
jmp 0xb534
leaq 0x110(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rdi
callq 0x13e20
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rdi
callq 0x13e50
movq %rax, 0xf8(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x13e80
testb $0x1, %al
jne 0xb589
jmp 0xb605
leaq 0x100(%rsp), %rdi
callq 0x13ec0
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rsi
leaq 0x2d8(%rsp), %rdi
callq 0x12800
jmp 0xb5b5
jmp 0xb5b7
leaq 0x100(%rsp), %rdi
callq 0x13ed0
jmp 0xb56e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
jmp 0xb6fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12ee0
jmp 0xb6fc
movl 0x140(%rsp), %eax
addl 0x2f0(%rsp), %eax
movl %eax, 0x2f0(%rsp)
movl 0x148(%rsp), %eax
movl %eax, 0x2d4(%rsp)
movl 0x25c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x25c(%rsp)
leaq 0x260(%rsp), %rdi
callq 0x138b0
movl $0x0, 0x24c(%rsp)
movl 0x25c(%rsp), %eax
movl %eax, 0x44(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x12180
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0xb6dd
movq 0x338(%rsp), %rax
movq %rax, 0x30(%rsp)
addq $0x8, %rax
movq %rax, 0x28(%rsp)
movslq 0x25c(%rsp), %rsi
leaq 0x298(%rsp), %rdi
callq 0x121a0
movq 0x28(%rsp), %rdi
movslq (%rax), %rsi
callq 0x13890
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x7a10
movq %rax, 0x38(%rsp)
jmp 0xb6c0
movq 0x38(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x250(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x12ee0
leaq 0x170(%rsp), %rdi
callq 0x13150
jmp 0xb0cf
leaq 0x170(%rsp), %rdi
callq 0x13150
jmp 0xb801
leaq 0xc8(%rsp), %rdi
leaq 0x2d8(%rsp), %rsi
callq 0x8820
jmp 0xb725
movl 0x2f0(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x9b40
movl %eax, 0x24(%rsp)
jmp 0xb743
movq 0xb0(%rsp), %rdi
movl 0x24(%rsp), %ecx
movl 0x20(%rsp), %eax
addl %ecx, %eax
movl %eax, 0xc4(%rsp)
leaq 0xc8(%rsp), %rsi
leaq 0xc4(%rsp), %rdx
callq 0x13ef0
jmp 0xb773
movl $0x1, 0x21c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4700
jmp 0xb7b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2f8(%rsp)
movl %eax, 0x2f4(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4700
jmp 0xb801
leaq 0x260(%rsp), %rdi
callq 0x13e10
leaq 0x298(%rsp), %rdi
callq 0x13150
leaq 0x2b8(%rsp), %rdi
callq 0x13440
leaq 0x2d8(%rsp), %rdi
callq 0x13150
leaq 0x300(%rsp), %rdi
callq 0x12350
movq 0xb8(%rsp), %rax
addq $0x348, %rsp            # imm = 0x348
retq
leaq 0x260(%rsp), %rdi
callq 0x13e10
leaq 0x298(%rsp), %rdi
callq 0x13150
leaq 0x2b8(%rsp), %rdi
callq 0x13440
leaq 0x2d8(%rsp), %rdi
callq 0x13150
leaq 0x300(%rsp), %rdi
callq 0x12350
movq 0x2f8(%rsp), %rdi
callq 0x4620
nop