// File: /workspace/github_cpp_makefile_star2_O2/Kei18[P]pibt/./src/solver/cbs.cpp
// Function: CBS::solvePart(std::vector<std::vector<Node*, std::allocator<Node*>>, std::allocator<std::vector<Node*, std::allocator<Node*>>>>&, std::vector<Agent*, std::allocator<Agent*>>&)
bool CBS::solvePart(Paths& paths, Agents& block) {
  CTNode* node;
  Constraints constraints;
  std::vector<CTNode*> OPEN, ALL;
  bool status = true;

  CTNode* root = new CTNode { {}, {}, 0, nullptr, true, {}, 0 };
  invoke(root, block);
  OPEN.push_back(root);
  ALL.push_back(root);
  bool CAT = std::any_of(paths.begin(), paths.end(),
                         [](Nodes p) { return !p.empty(); });

  while (!OPEN.empty()) {
    auto itr = std::min_element(OPEN.begin(), OPEN.end(),
                                [CAT, this, &paths]
                                (CTNode* n1, CTNode* n2) {
                                  if (CAT && n1->cost == n2->cost) {
                                    int c1 = this->countCollisions(n1, paths);
                                    int c2 = this->countCollisions(n2, paths);
                                    return c1 < c2;
                                  }
                                  return n1->cost < n2->cost;
                                });
    node = *itr;
    constraints = valid(node, block);
    if (constraints.empty()) break;
    OPEN.erase(itr);

    for (auto constraint : constraints) {
      CTNode* newNode = new CTNode { constraint, node->paths,
                                     0, node, true,
                                     {}, 0 };

      // it works, but it is late
      // if (isDuplicatedCTNode(newNode, ALL)) continue;

      invoke(newNode, block);
      if (newNode->valid) {
        OPEN.push_back(newNode);
        ALL.push_back(newNode);
      }
    }
    constraints.clear();
  }

  if (!OPEN.empty()) {  // sucssess
    for (int i = 0; i < paths.size(); ++i) {
      if (!node->paths[i].empty()) {
        paths[i] = node->paths[i];
      }
    }
    status = status && true;
  } else {
    status = false;
  }

  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, %rbx
movq %rdi, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
pushq $0x68
popq %rdi
callq 0x63b0
andq $0x0, 0x38(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
andl $0x0, 0x30(%rax)
movb $0x1, 0x40(%rax)
movups %xmm0, 0x48(%rax)
movups %xmm0, 0x54(%rax)
movq %rax, 0x78(%rsp)
movq 0x38(%rsp), %rdi
movq (%rdi), %rcx
movq %rax, %rsi
movq 0x40(%rsp), %rdx
callq *0x20(%rcx)
leaq 0x10(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x22768
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x22768
movq (%rbx), %r13
movq %rbx, 0x70(%rsp)
movq 0x8(%rbx), %rbx
movq %rbx, 0x30(%rsp)
subq %r13, %rbx
pushq $0x18
popq %rcx
movq %rbx, %rax
cqto
idivq %rcx
movq %rax, %r14
sarq $0x2, %r14
testq %r14, %r14
jle 0x1f882
movq %r13, %rdi
callq 0x21d22
testb %al, %al
jne 0x1f8d9
leaq 0x18(%r13), %rdi
movq %rdi, 0x8(%rsp)
callq 0x21d22
testb %al, %al
jne 0x1f8de
leaq 0x30(%r13), %rdi
movq %rdi, 0x8(%rsp)
callq 0x21d22
testb %al, %al
jne 0x1f8de
leaq 0x48(%r13), %rdi
movq %rdi, 0x8(%rsp)
callq 0x21d22
testb %al, %al
jne 0x1f8de
addq $0x60, %r13
decq %r14
addq $-0x60, %rbx
jmp 0x1f826
movq %rbx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq $0x1, %rax
je 0x1f8c9
cmpq $0x2, %rax
je 0x1f8b9
movq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rsp)
cmpq $0x3, %rax
jne 0x1f8de
movq %r13, %rdi
callq 0x21d22
testb %al, %al
jne 0x1f8d9
addq $0x18, %r13
movq %r13, %rdi
callq 0x21d22
testb %al, %al
jne 0x1f8d9
addq $0x18, %r13
movq %r13, %rdi
callq 0x21d22
testb %al, %al
cmoveq 0x30(%rsp), %r13
movq %r13, 0x8(%rsp)
movq 0x70(%rsp), %r13
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0x1fae7
leaq 0x8(%r14), %r12
cmpq %r15, %r12
je 0x1f94d
movq (%r12), %rsi
movq (%r14), %rbx
movl 0x30(%rsi), %eax
movl 0x30(%rbx), %ecx
movq 0x8(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
je 0x1f935
cmpl %ecx, %eax
jne 0x1f935
movq %r13, %rdx
callq 0x202d6
movl %eax, %ebp
movq %rbx, %rsi
movq %r13, %rdx
callq 0x202d6
cmpl %eax, %ebp
jmp 0x1f937
cmpl %ecx, %eax
setl %al
movq %r12, %rcx
testb %al, %al
jne 0x1f944
movq %r14, %rcx
addq $0x8, %r12
movq %rcx, %r14
jmp 0x1f8fa
movq (%r14), %rbp
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq %rbp, %rdx
movq 0x40(%rsp), %rcx
callq 0x1fb86
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2376e
movq %rbx, %rdi
callq 0x22786
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r15
movq 0x18(%rsp), %rdx
cmpq %r15, %rbx
je 0x1fa96
leaq 0x8(%r14), %rsi
cmpq %rdx, %rsi
je 0x1f9bd
subq %rsi, %rdx
movq %r14, %rdi
callq 0x65c0
movq 0x18(%rsp), %rdx
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r15
addq $-0x8, %rdx
movq %rdx, 0x18(%rsp)
leaq 0x18(%rbp), %r14
cmpq %r15, %rbx
je 0x1fa87
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x227a8
pushq $0x68
popq %rdi
callq 0x63b0
movq %rax, %r12
movq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x227a8
movq %r12, %rdi
addq $0x18, %rdi
movq %r14, %rsi
callq 0x227f6
andl $0x0, 0x30(%r12)
movq %rbp, 0x38(%r12)
movb $0x1, 0x40(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%r12)
movups %xmm0, 0x54(%r12)
movq %r12, 0x48(%rsp)
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
movq %r12, %rsi
movq 0x40(%rsp), %rdx
callq *0x20(%rax)
movq 0x48(%rsp), %rax
cmpb $0x1, 0x40(%rax)
jne 0x1fa71
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x22768
leaq 0x80(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x22768
leaq 0xa0(%rsp), %rdi
callq 0x235f6
addq $0x18, %rbx
jmp 0x1f9ca
leaq 0x50(%rsp), %rdi
callq 0x22850
jmp 0x1f8e3
cmpq %rdx, 0x10(%rsp)
setne %bl
je 0x1fae7
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x70(%rsp), %r12
movq (%r12), %rdi
movq 0x8(%r12), %rax
subq %rdi, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r15, %rax
jbe 0x1fae9
movq 0x18(%rbp), %rsi
movq (%rsi,%r14), %rax
cmpq 0x8(%rsi,%r14), %rax
je 0x1fade
addq %r14, %rsi
addq %r14, %rdi
callq 0x16f90
incq %r15
addq $0x18, %r14
jmp 0x1faab
xorl %ebx, %ebx
leaq 0x80(%rsp), %rdi
callq 0x235c2
leaq 0x10(%rsp), %rdi
callq 0x235c2
leaq 0x50(%rsp), %rdi
callq 0x22786
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1fb47
jmp 0x1fb47
jmp 0x1fb47
jmp 0x1fb47
jmp 0x1fb47
movq %rax, %rbx
movq %r12, %rdi
callq 0x235f6
jmp 0x1fb3a
jmp 0x1fb47
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r12, %rdi
callq 0x63c0
jmp 0x1fb4f
movq %rax, %rbx
jmp 0x1fb5c
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x235f6
leaq 0x80(%rsp), %rdi
callq 0x235c2
leaq 0x10(%rsp), %rdi
callq 0x235c2
leaq 0x50(%rsp), %rdi
callq 0x22786
movq %rbx, %rdi
callq 0x6650
nop