// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x40(%rsp)
movq (%rdi), %rax
movq 0x8(%rax), %rbp
subq (%rax), %rbp
movq %rdi, (%rsp)
movq 0x8(%rdi), %rbx
subq %rax, %rbx
movb 0x18f9f(%rip), %al      # 0x1e870
testb %al, %al
je 0x5def
shrq $0x3, %rbx
movb 0x18fad(%rip), %al      # 0x1e890
testb %al, %al
je 0x5df9
movq 0x18f66(%rip), %rax     # 0x1e858
cmpq %rax, 0x18f67(%rip)     # 0x1e860
je 0x5902
movq %rax, 0x18f5e(%rip)     # 0x1e860
shrq $0x2, %rbp
imull $0xaaaaaaab, %ebx, %r14d # imm = 0xAAAAAAAB
movq 0x18f64(%rip), %rax     # 0x1e878
cmpq %rax, 0x18f65(%rip)     # 0x1e880
je 0x5924
movq %rax, 0x18f5c(%rip)     # 0x1e880
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 0x20(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
cmpq %rax, %r12
je 0x5b12
leaq 0x10(%rsp), %rbx
leaq 0xf836(%rip), %r15      # 0x151b0
movsd %xmm1, 0x28(%rsp)
movq %r12, 0x48(%rsp)
movq (%r12), %rax
movq 0x40(%rsp), %rsi
movl (%rsi), %ecx
leal (%rcx,%rax), %edx
shrq $0x20, %rax
movl 0x4(%rsi), %esi
addl %esi, %eax
movq 0x20(%rsp), %rdi
movq 0x18(%rdi), %r13
movq %rbx, %r12
leal (%rcx,%r13), %ebx
shrq $0x20, %r13
addl %esi, %r13d
subl %ebx, %edx
subl %r13d, %eax
shlq $0x20, %rax
orq %rax, %rdx
movq %rdx, 0x10(%rsp)
movq %r12, %rdi
movl 0x1c(%rsp), %esi
callq 0xc88c
leal (%rax,%rbx), %ecx
shrq $0x20, %rax
addl %r13d, %eax
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rsp), %xmm0
movq 0x18e6a(%rip), %rsi     # 0x1e860
cmpq 0x18e6b(%rip), %rsi     # 0x1e868
je 0x5a0c
movq %rcx, (%rsi)
addq $0x8, 0x18e56(%rip)     # 0x1e860
jmp 0x5a27
leaq 0x18e45(%rip), %rdi     # 0x1e858
movq %r12, %rdx
movsd %xmm0, 0x8(%rsp)
callq 0x106ba
movsd 0x8(%rsp), %xmm0
movq %r12, %rbx
movq 0x18e4f(%rip), %rsi     # 0x1e880
cmpq 0x18e50(%rip), %rsi     # 0x1e888
movq 0x48(%rsp), %r12
movsd 0x28(%rsp), %xmm1
je 0x5a57
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18e2b(%rip)     # 0x1e880
jmp 0x5a78
leaq 0x18e1a(%rip), %rdi     # 0x1e878
movq %rbx, %rdx
movsd %xmm0, 0x8(%rsp)
callq 0x106ba
movsd 0x28(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
minsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %xmm1
movq (%r13,%r15), %xmm0
paddd %xmm1, %xmm0
movd %xmm0, %eax
testl %eax, %eax
js 0x5aed
cmpl %ebp, %eax
jge 0x5aed
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %eax
testl %eax, %eax
js 0x5aed
cmpl %r14d, %eax
jge 0x5aed
movq %xmm0, 0x30(%rsp)
movq 0x18dbf(%rip), %rsi     # 0x1e880
cmpq 0x18dc0(%rip), %rsi     # 0x1e888
je 0x5adc
movq 0x30(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18da6(%rip)     # 0x1e880
jmp 0x5aed
leaq 0x18d95(%rip), %rdi     # 0x1e878
leaq 0x30(%rsp), %rdx
callq 0xfaec
addq $0x8, %r13
cmpq $0x30, %r13
jne 0x5a85
addq $0x8, %r12
movsd 0x8(%rsp), %xmm2
movapd %xmm2, %xmm1
cmpq 0x38(%rsp), %r12
jne 0x597a
jmp 0x5b16
movapd %xmm1, %xmm2
movq 0x18d5b(%rip), %rax     # 0x1e878
movq 0x18d5c(%rip), %rcx     # 0x1e880
cmpq %rcx, %rax
je 0x5bff
movq (%rsp), %rdx
movq (%rdx), %rdx
leaq 0xf675(%rip), %rsi      # 0x151b0
movsd 0xf5a5(%rip), %xmm0     # 0x150e8
movq %rax, %rdi
movslq (%rdi), %r8
movslq 0x4(%rdi), %r9
leaq (%r9,%r9,2), %r10
movq (%rdx,%r10,8), %r11
movl (%r11,%r8,4), %r10d
btl $0x8, %r10d
jb 0x5bf2
movl %r10d, %ebx
orl $0x100, %ebx            # imm = 0x100
movl %ebx, (%r11,%r8,4)
testb %r10b, %r10b
jne 0x5bf2
xorl %r11d, %r11d
xorl %r10d, %r10d
movl (%rsi,%r11,8), %ebx
addl %r8d, %ebx
js 0x5bb0
cmpl %ebp, %ebx
jge 0x5bb0
movl 0x4(%rsi,%r11,8), %r15d
addl %r9d, %r15d
js 0x5bb0
cmpl %r14d, %r15d
jge 0x5bb0
movl %r15d, %r15d
leaq (%r15,%r15,2), %r15
movl %ebx, %ebx
movq (%rdx,%r15,8), %r15
cmpb $0x0, (%r15,%rbx,4)
jne 0x5bb0
btsl %r11d, %r10d
incq %r11
cmpq $0x6, %r11
jne 0x5b7c
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 %rcx, %rdi
jne 0x5b46
cmpq %rcx, %rax
je 0x5c2e
movq (%rsp), %rdx
movq (%rdx), %rdx
movq %rax, %rsi
movslq 0x4(%rsi), %rdi
leaq (%rdi,%rdi,2), %rdi
movslq (%rsi), %r8
movq (%rdx,%rdi,8), %rdi
andl $0xfffffeff, (%rdi,%r8,4) # imm = 0xFFFFFEFF
addq $0x8, %rsi
cmpq %rcx, %rsi
jne 0x5c0e
movq 0x18c23(%rip), %rdx     # 0x1e858
movq 0x18c24(%rip), %rsi     # 0x1e860
cmpq %rsi, %rdx
je 0x5c6b
movq (%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 0x5c4b
cmpq %rcx, %rax
je 0x5d46
movq (%rsp), %rdi
movq (%rdi), %rdi
leaq 0xf52e(%rip), %r8       # 0x151b0
movsd 0xf466(%rip), %xmm0     # 0x150f0
movq %rax, %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 0x5d39
movl %ebx, %r12d
orl $0x100, %r12d           # imm = 0x100
movl %r12d, (%r15,%r10,4)
testb %bl, %bl
jne 0x5d39
xorl %r15d, %r15d
xorl %ebx, %ebx
movl (%r8,%r15,8), %r12d
addl %r10d, %r12d
js 0x5cf9
cmpl %ebp, %r12d
jge 0x5cf9
movl 0x4(%r8,%r15,8), %r13d
addl %r11d, %r13d
js 0x5cf9
cmpl %r14d, %r13d
jge 0x5cf9
movl %r13d, %r13d
leaq (%r13,%r13,2), %r13
movl %r12d, %r12d
movq (%rdi,%r13,8), %r13
cmpb $0x0, (%r13,%r12,4)
jne 0x5cf9
btsl %r15d, %ebx
incq %r15
cmpq $0x6, %r15
jne 0x5cc1
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 %rcx, %r9
jne 0x5c8d
cmpq %rcx, %rax
je 0x5d72
movq (%rsp), %rdi
movq (%rdi), %rdi
movslq 0x4(%rax), %r8
leaq (%r8,%r8,2), %r8
movslq (%rax), %r9
movq (%rdi,%r8,8), %r8
andl $0xfffffeff, (%r8,%r9,4) # imm = 0xFFFFFEFF
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x5d52
cmpq %rsi, %rdx
je 0x5d9d
movq (%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 0x5d7e
xorpd %xmm1, %xmm1
movq 0x18a98(%rip), %rax     # 0x1e840
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
mulsd 0x50(%rsp,%rcx,8), %xmm0
addsd %xmm0, %xmm1
incq %rcx
cmpq $0x9, %rcx
jne 0x5daa
movq 0x18a77(%rip), %rax     # 0x1e840
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 0x3540
jmp 0x58d9
callq 0x358c
jmp 0x58eb