// File: /workspace/github_cpp_makefile_star2_O3/tanakh[P]icfpc2015/main.cpp
// Function: calc_score(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, std::complex<int> const&, unit const&, int, int, std::vector<int, std::allocator<int>> const&)
double calc_score(vector<vector<int>> &bd,
                  const pt &pos, const unit &u, int rot, int ls_old,
                  const vector<int> &cmds)
{
  int w = bd[0].size(), h = bd.size();

  static vector<pt> units;
  static vector<pt> pts;
  units.clear();
  pts.clear();

  double height = h;
  double feat[FN] = {};

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

    units.push_back(d);
    pts.push_back(d);
    for (int i = 0; i < 6; i++) {
      int cx = d.real() + adj6[i][0];
      int cy = d.imag() + adj6[i][1];

      if (!(cx >= 0 && cx < w && cy >= 0 && cy < h)) continue;
      pts.push_back(pt(cx, cy));
    }
  }

  for (auto &p: pts) {
    int cx = p.real();
    int cy = p.imag();
    if (bd[cy][cx] & (1 << 8)) continue;
    bd[cy][cx] |= (1 << 8);

    if ((bd[cy][cx] & ((1<<8)-1)) != 0) continue;

    int pat = 0;
    for (int i = 0; i < 6; i++) {
      int dx = cx + adj6[i][0];
      int dy = cy + adj6[i][1];

      if (!(dx >= 0 && dx < w && dy >= 0 && dy < h &&
            (bd[dy][dx] & ((1<<8)-1)) == 0))
        continue;

      pat |= (1 << i);
    }

    int f = 0;
    f = f * 3 + ((pat>>0)&1) + ((pat>>1)&1);
    f = f * 3 + ((pat>>2)&1) + ((pat>>3)&1);
    feat[f]++;
  }

  for (auto &p: pts)
    bd[p.imag()][p.real()] &= ~(1 << 8);

  for (auto &c: units)
    bd[c.imag()][c.real()] = 1;

  for (auto &p: pts) {
    int cx = p.real();
    int cy = p.imag();
    if (bd[cy][cx] & (1 << 8)) continue;
    bd[cy][cx] |= (1 << 8);

    if ((bd[cy][cx] & ((1<<8)-1)) != 0) continue;

    int pat = 0;
    for (int i = 0; i < 6; i++) {
      int dx = cx + adj6[i][0];
      int dy = cy + adj6[i][1];

      if (!(dx >= 0 && dx < w && dy >= 0 && dy < h &&
            (bd[dy][dx] & ((1<<8)-1)) == 0))
        continue;

      pat |= (1 << i);
    }

    int f = 0;
    f = f * 3 + ((pat>>0)&1) + ((pat>>1)&1);
    f = f * 3 + ((pat>>2)&1) + ((pat>>3)&1);
    feat[f]--;
  }

  for (auto &p: pts)
    bd[p.imag()][p.real()] &= ~(1 << 8);

  for (auto &c: units)
    bd[c.imag()][c.real()] = 0;

  double ret = 0;

  for (int i = 0; i < FN; i++)
    ret += eparam[i] * feat[i];

  ret += eparam[FN + 0] * height * w;

  return ret;

  // int w = bd[0].size(), h = bd.size();

  // double height = h;

  // for (int y = 0; y < h; y++) {
  //   for (int x = 0; x < w/2; x++) {
  //     int xx = x*2 + y%2;
  //     if (bd[y][xx] != 0) {
  //       height = min(height, (double)y);
  //     }

  //     if (bd[y][xx] != 0)
  //       continue;

  //     const int vect[][2] = {
  //       {-1, -1},
  //       {1, -1},
  //       {2, 0},
  //       {-2, 0},
  //       {1, 1},
  //       {-1, 1},
  //     };

  //     int pat = 0;
  //     for (int i = 0; i < 6; i++) {
  //       int cx = xx + vect[i][0];
  //       int cy = y + vect[i][1];

  //       if (!(cx >= 0 && cx < w && cy >= 0 && cy < h && bd[cy][cx] == 0)) continue;

  //       pat |= (1 << i);
  //     }

  //     int f = 0;
  //     f = f * 3 + ((pat>>0)&1) + ((pat>>1)&1);
  //     f = f * 3 + ((pat>>2)&1) + ((pat>>3)&1);
  //     // f = f * 3 + ((pat>>4)&1) + ((pat>>5)&1);
  //     // f = f * 2 + (bd[y][xx] != 0);

  //     feat[f]++;
  //   }
  // }

  // double ret = 0;

  // // vlog() << height << ", " << holes << ", " << roofs << ", " << sides << endl;
  // // height /= h;
  // // if (height >= 0.8) height = 1;

  // for (int i = 0; i < FN; i++)
  //   ret += eparam[i] * feat[i];

  // ret += eparam[FN + 0] * height * w;
  // ret += eparam[FN + 1] * (double)last.imag() * w;
  // ret += eparam[FN + 2] * (double)(lines+1)*lines/2 * w * h;
  // // ret += eparam[FN + 3] * (double)cmds.length();

  // return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, 0x2c(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x48(%rsp)
movq (%rdi), %rax
movq 0x8(%rax), %rbp
subq (%rax), %rbp
movq %rdi, 0x10(%rsp)
movq 0x8(%rdi), %rbx
subq %rax, %rbx
movb 0x18f9f(%rip), %al      # 0x1e860
testb %al, %al
je 0x5de4
shrq $0x3, %rbx
movb 0x18fad(%rip), %al      # 0x1e880
testb %al, %al
je 0x5dee
movq 0x18f66(%rip), %rax     # 0x1e848
cmpq %rax, 0x18f67(%rip)     # 0x1e850
je 0x58f2
movq %rax, 0x18f5e(%rip)     # 0x1e850
shrq $0x2, %rbp
imull $0xaaaaaaab, %ebx, %r14d # imm = 0xAAAAAAAB
movq 0x18f64(%rip), %rax     # 0x1e868
cmpq %rax, 0x18f65(%rip)     # 0x1e870
je 0x5914
movq %rax, 0x18f5c(%rip)     # 0x1e870
cvtsi2sd %r14d, %xmm1
pxor %xmm0, %xmm0
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x30(%rsp), %rcx
movq (%rcx), %r15
movq 0x8(%rcx), %rcx
movb $0x1, 0xf(%rsp)
movq %rcx, 0x40(%rsp)
cmpq %rcx, %r15
je 0x5c13
leaq 0x20(%rsp), %rbx
leaq 0xf821(%rip), %r12      # 0x15190
movl %r14d, 0x28(%rsp)
movsd %xmm1, 0x18(%rsp)
movq (%r15), %rax
movq 0x48(%rsp), %rsi
movl (%rsi), %ecx
leal (%rcx,%rax), %edx
shrq $0x20, %rax
movl 0x4(%rsi), %esi
addl %esi, %eax
movq 0x30(%rsp), %rdi
movq %rbx, %r14
movq 0x18(%rdi), %rbx
leal (%rbx,%rcx), %r13d
shrq $0x20, %rbx
addl %esi, %ebx
subl %r13d, %edx
subl %ebx, %eax
shlq $0x20, %rax
orq %rax, %rdx
movq %rdx, 0x20(%rsp)
movq %r14, %rdi
movl 0x2c(%rsp), %esi
callq 0xc93e
leal (%rax,%r13), %ecx
movq %rax, %r13
shrq $0x20, %r13
addl %ebx, %r13d
movq %r13, %rax
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq 0x18e69(%rip), %rsi     # 0x1e850
cmpq 0x18e6a(%rip), %rsi     # 0x1e858
je 0x59fd
movq %rcx, (%rsi)
addq $0x8, 0x18e55(%rip)     # 0x1e850
jmp 0x5a0c
leaq 0x18e44(%rip), %rdi     # 0x1e848
movq %r14, %rdx
callq 0x107a6
movq %r14, %rbx
movq 0x18e5a(%rip), %rsi     # 0x1e870
cmpq 0x18e5b(%rip), %rsi     # 0x1e878
movl 0x28(%rsp), %r14d
movsd 0x18(%rsp), %xmm1
je 0x5a3c
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18e36(%rip)     # 0x1e870
jmp 0x5a51
leaq 0x18e25(%rip), %rdi     # 0x1e868
movq %rbx, %rdx
callq 0x107a6
movsd 0x18(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
minsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %r13d, %r13d
movq 0x20(%rsp), %xmm1
movq (%r13,%r12), %xmm0
paddd %xmm1, %xmm0
movd %xmm0, %eax
testl %eax, %eax
js 0x5ace
cmpl %ebp, %eax
jge 0x5ace
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %eax
testl %eax, %eax
js 0x5ace
cmpl %r14d, %eax
jge 0x5ace
movq %xmm0, 0x38(%rsp)
movq 0x18dce(%rip), %rsi     # 0x1e870
cmpq 0x18dcf(%rip), %rsi     # 0x1e878
je 0x5abd
movq 0x38(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18db5(%rip)     # 0x1e870
jmp 0x5ace
leaq 0x18da4(%rip), %rdi     # 0x1e868
leaq 0x38(%rsp), %rdx
callq 0xfbfe
addq $0x8, %r13
cmpq $0x30, %r13
jne 0x5a66
addq $0x8, %r15
movsd 0x18(%rsp), %xmm2
movapd %xmm2, %xmm1
cmpq 0x40(%rsp), %r15
jne 0x5974
movq 0x18d70(%rip), %rcx     # 0x1e868
movq 0x18d71(%rip), %rax     # 0x1e870
cmpq %rax, %rcx
sete %dl
je 0x5c22
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
movsd 0xf5ad(%rip), %xmm0     # 0x150c8
movq %rcx, %rdi
movslq (%rdi), %r8
movslq 0x4(%rdi), %r9
leaq (%r9,%r9,2), %r10
movq (%rsi,%r10,8), %r11
movl (%r11,%r8,4), %r10d
btl $0x8, %r10d
jb 0x5bca
movl %r10d, %ebx
orl $0x100, %ebx            # imm = 0x100
movl %ebx, (%r11,%r8,4)
testb %r10b, %r10b
jne 0x5bca
xorl %r11d, %r11d
xorl %r10d, %r10d
movl (%r12,%r11,8), %ebx
addl %r8d, %ebx
js 0x5b88
cmpl %ebp, %ebx
jge 0x5b88
movl 0x4(%r12,%r11,8), %r15d
addl %r9d, %r15d
js 0x5b88
cmpl %r14d, %r15d
jge 0x5b88
movl %r15d, %r15d
leaq (%r15,%r15,2), %r15
movl %ebx, %ebx
movq (%rsi,%r15,8), %r15
cmpb $0x0, (%r15,%rbx,4)
jne 0x5b88
btsl %r11d, %r10d
incq %r11
cmpq $0x6, %r11
jne 0x5b54
movl %r10d, %r8d
andl $0x1, %r8d
btl $0x1, %r10d
adcl $0x0, %r8d
leal (%r8,%r8,2), %r8d
movl %r10d, %r9d
shrl $0x3, %r9d
andl $0x1, %r9d
btl $0x2, %r10d
adcl %r8d, %r9d
movsd 0x50(%rsp,%r9,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp,%r9,8)
addq $0x8, %rdi
cmpq %rax, %rdi
jne 0x5b1e
cmpq %rax, %rcx
je 0x5c1c
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
movq %rcx, %rdi
movsd 0x18(%rsp), %xmm2
movslq 0x4(%rdi), %r8
leaq (%r8,%r8,2), %r8
movslq (%rdi), %r9
movq (%rsi,%r8,8), %r8
andl $0xfffffeff, (%r8,%r9,4) # imm = 0xFFFFFEFF
addq $0x8, %rdi
cmpq %rax, %rdi
jne 0x5bed
movb %dl, 0xf(%rsp)
jmp 0x5c22
movq %rax, %rcx
movapd %xmm1, %xmm2
jmp 0x5c22
movsd 0x18(%rsp), %xmm2
movq 0x18c1f(%rip), %rdx     # 0x1e848
movq 0x18c20(%rip), %rsi     # 0x1e850
cmpq %rsi, %rdx
je 0x5c60
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
movq %rdx, %r8
movslq 0x4(%r8), %r9
leaq (%r9,%r9,2), %r9
movslq (%r8), %r10
movq (%rdi,%r9,8), %r9
movl $0x1, (%r9,%r10,4)
addq $0x8, %r8
cmpq %rsi, %r8
jne 0x5c40
cmpb $0x0, 0xf(%rsp)
jne 0x5d6d
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
leaq 0xf516(%rip), %r8       # 0x15190
movsd 0xf44e(%rip), %xmm0     # 0x150d0
movq %rcx, %r9
movslq (%r9), %r10
movslq 0x4(%r9), %r11
leaq (%r11,%r11,2), %rbx
movq (%rdi,%rbx,8), %r15
movl (%r15,%r10,4), %ebx
btl $0x8, %ebx
jb 0x5d31
movl %ebx, %r12d
orl $0x100, %r12d           # imm = 0x100
movl %r12d, (%r15,%r10,4)
testb %bl, %bl
jne 0x5d31
xorl %r15d, %r15d
xorl %ebx, %ebx
movl (%r8,%r15,8), %r12d
addl %r10d, %r12d
js 0x5cf1
cmpl %ebp, %r12d
jge 0x5cf1
movl 0x4(%r8,%r15,8), %r13d
addl %r11d, %r13d
js 0x5cf1
cmpl %r14d, %r13d
jge 0x5cf1
movl %r13d, %r13d
leaq (%r13,%r13,2), %r13
movl %r12d, %r12d
movq (%rdi,%r13,8), %r13
cmpb $0x0, (%r13,%r12,4)
jne 0x5cf1
btsl %r15d, %ebx
incq %r15
cmpq $0x6, %r15
jne 0x5cb9
movl %ebx, %r10d
andl $0x1, %r10d
btl $0x1, %ebx
adcl $0x0, %r10d
leal (%r10,%r10,2), %r10d
movl %ebx, %r11d
shrl $0x3, %r11d
andl $0x1, %r11d
btl $0x2, %ebx
adcl %r10d, %r11d
movsd 0x50(%rsp,%r11,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp,%r11,8)
addq $0x8, %r9
cmpq %rax, %r9
jne 0x5c85
cmpb $0x0, 0xf(%rsp)
jne 0x5d6d
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
movslq 0x4(%rcx), %r8
leaq (%r8,%r8,2), %r8
movslq (%rcx), %r9
movq (%rdi,%r8,8), %r8
andl $0xfffffeff, (%r8,%r9,4) # imm = 0xFFFFFEFF
addq $0x8, %rcx
cmpq %rax, %rcx
jne 0x5d4d
cmpq %rsi, %rdx
je 0x5d99
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq 0x4(%rdx), %rcx
leaq (%rcx,%rcx,2), %rcx
movslq (%rdx), %rdi
movq (%rax,%rcx,8), %rcx
movl $0x0, (%rcx,%rdi,4)
addq $0x8, %rdx
cmpq %rsi, %rdx
jne 0x5d7a
xorpd %xmm1, %xmm1
movq 0x18a8c(%rip), %rax     # 0x1e830
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
mulsd 0x50(%rsp,%rcx,8), %xmm0
addsd %xmm0, %xmm1
incq %rcx
cmpq $0x9, %rcx
jne 0x5da6
mulsd 0x48(%rax), %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3530
jmp 0x58c9
callq 0x357c
jmp 0x58db