// File: /workspace/github_cpp_makefile_star2_O2/SilentAssassinMa[P]Sudoku-Solver/SudokuSolver.cpp
// Function: SudokuSolver::SimulatedAnnealing(double, int)
void SudokuSolver::SimulatedAnnealing(double temp, int steps)
{
    if (!isSolvable)
    {
        cout << "Error : The Input Sudoku Is Not Solvable" << endl;
        return;
    }

    /* 
    Here we use 243 - (sum of unique elements) as the energy of the sudoku,
    and we initialize it so that each subsquare containes all integers once only
    */
    cout << "The Original Sudoku is " << endl;
    PrintSudoku(OriginalSudoku);
    cout << endl;

    // Initialization
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= 9; j++)
        {
            if ((i % 3) == 1 && (j % 3) == 1)
            {
                int subsquare[10] = {0};
                // Find the missing integers
                for (int m = 0; m < 3; m++)
                {
                    for (int n = 0; n < 3; n++)
                    {
                        if (FindElement(OriginalSudoku, i + m, j + n) != 0)
                        {
                            subsquare[FindElement(OriginalSudoku, i + m, j + n)] = 1;
                        }
                    }
                }

                // Fill the rest
                for (int m = 0; m < 3; m++)
                {
                    for (int n = 0; n < 3; n++)
                    {

                        if (isChangeable[9 * (i + m - 1) + j + n - 1])
                        {
                            for (int l = 1; l <= 9; l++)
                            {
                                if (subsquare[l] == 0)
                                {
                                    Current[9 * (i + m - 1) + j + n - 1] = l;
                                    subsquare[l] = 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    srand((unsigned)time(NULL));

    double Temperature = temp < 0 ? 0.25 : temp;
    int MaxSteps = steps <= 0 ? 1e6 : steps;

    int Ebefore = 243 - UniqueElements(Current);

    // start loop
    int step;
    for (step = 0; step < MaxSteps; step++)
    {
        // pick a random block and two random changeable elements

        int xblock = 3 * (rand() % 3) + 1;
        int yblock = 3 * (rand() % 3) + 1;
        int x1, x2, y1, y2;

        do
        {
            x1 = rand() % 3 + xblock;
            y1 = rand() % 3 + yblock;
            x2 = rand() % 3 + xblock;
            y2 = rand() % 3 + yblock;
        } while (!isChangeable[9 * (x1 - 1) + (y1 - 1)] || !isChangeable[9 * (x2 - 1) + (y2 - 1)] ||
                 (x1 == x2 && y1 == y2));

        // exchange the chosen elements
        int temp;
        temp = Current[9 * (x1 - 1) + (y1 - 1)];
        Current[9 * (x1 - 1) + (y1 - 1)] = Current[9 * (x2 - 1) + (y2 - 1)];
        Current[9 * (x2 - 1) + (y2 - 1)] = temp;

        int Eafter = 243 - UniqueElements(Current);

        if (Eafter < Ebefore)
        {
            // cout << step << " " << Eafter << endl;
        }

        if (Eafter == 0)
            break;

        if (exp((Ebefore - Eafter) / Temperature) > (rand() % 10000 / (double)10001))
        {
            Ebefore = Eafter;
        }
        else
        {
            temp = Current[9 * (x1 - 1) + (y1 - 1)];
            Current[9 * (x1 - 1) + (y1 - 1)] = Current[9 * (x2 - 1) + (y2 - 1)];
            Current[9 * (x2 - 1) + (y2 - 1)] = temp;
        }
    }

    if (CheckConsistency(Current))
    {
        cout << "The Filled Sudoku is " << endl;
        PrintSudoku(Current);
    }
    else
    {
        cout << "The Sudoku is not solved, try more steps or different temperature" << endl;
    }

    cout << endl;
    cout << "Total Steps : " << step << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpb $0x0, 0x2e1(%rdi)
je 0x2b14
movq %rdi, %rbx
movapd %xmm0, 0x20(%rsp)
movl %esi, 0x10(%rsp)
movq 0x35fe(%rip), %r14      # 0x5fd8
leaq 0x16fd(%rip), %rsi      # 0x40de
movq %r14, %rdi
callq 0x2130
movq %rax, %rdi
callq 0x2090
leaq 0x8(%rbx), %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x40(%rax)
movq %r14, %rdi
callq 0x2090
leaq 0x14c(%rbx), %r14
pushq $0x1
popq %r9
movb $0x3, %dil
xorpd %xmm0, %xmm0
movq %r9, %rcx
cmpq $0xa, %rcx
je 0x2b2c
movl %ecx, %eax
xorl %edx, %edx
pushq $0x3
popq %rsi
divl %esi
movq %r12, %r8
movq %r9, %r15
cmpq $0xa, %r15
je 0x2b08
movzbl %r15b, %eax
divb %dil
movzbl %ah, %eax
cmpl $0x1, %edx
jne 0x2afc
cmpb $0x1, %al
jne 0x2afc
movapd %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movapd %xmm0, 0x40(%rsp)
leaq -0xa(%r15), %rax
movq %r8, %r10
xorl %r11d, %r11d
cmpq $0x3, %r11
je 0x2aa6
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x2a9d
movslq (%r10,%rsi,4), %r13
testq %r13, %r13
je 0x2a98
movl $0x1, 0x40(%rsp,%r13,4)
incq %rsi
jmp 0x2a80
incq %r11
addq $0x24, %r10
jmp 0x2a78
xorl %r10d, %r10d
cmpq $0x3, %r10
je 0x2afc
leaq (%r10,%rcx), %rsi
leaq (%rsi,%rsi,8), %r11
addq %rax, %r11
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0x2af7
leaq (%r11,%r13), %rbp
cmpb $0x1, 0x290(%rbx,%rbp)
jne 0x2af2
movq %r9, %rsi
cmpq $0xa, %rsi
je 0x2af2
cmpl $0x0, 0x40(%rsp,%rsi,4)
je 0x2ae6
incq %rsi
jmp 0x2ad4
movl %esi, (%r14,%rbp,4)
movl $0x1, 0x40(%rsp,%rsi,4)
incq %r13
jmp 0x2abd
incq %r10
jmp 0x2aa9
incq %r15
addq $0x4, %r8
jmp 0x2a37
incq %rcx
addq $0x24, %r12
jmp 0x2a1e
movq 0x34bd(%rip), %rdi      # 0x5fd8
leaq 0x1593(%rip), %rsi      # 0x40b5
callq 0x2130
jmp 0x2d69
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x2110
movl %eax, %edi
callq 0x2120
xorpd %xmm0, %xmm0
movapd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm2
cmpltsd %xmm0, %xmm2
movsd 0x14f8(%rip), %xmm0     # 0x4050
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movapd %xmm2, 0x70(%rsp)
movl 0x10(%rsp), %ecx
testl %ecx, %ecx
movl $0xf4240, %eax          # imm = 0xF4240
cmovgl %ecx, %eax
movl %eax, 0xc(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x38(%rax)
movl $0xf3, %r15d
subl %eax, %r15d
cmpl 0xc(%rsp), %r12d
je 0x2cf2
movl %r15d, 0x1c(%rsp)
movq %r12, 0x38(%rsp)
callq 0x2050
cltd
pushq $0x3
popq %rbp
idivl %ebp
leal (%rdx,%rdx,2), %r15d
callq 0x2050
cltd
idivl %ebp
leal (%rdx,%rdx,2), %edi
movq %r15, 0x20(%rsp)
movq %rdi, %r15
callq 0x2050
cltd
pushq $0x3
popq %rbp
idivl %ebp
movl %edx, %r12d
callq 0x2050
cltd
idivl %ebp
movl %edx, %r13d
callq 0x2050
cltd
idivl %ebp
movq %rdx, 0x10(%rsp)
callq 0x2050
movq %r15, %rdi
movq 0x20(%rsp), %rsi
cltd
idivl %ebp
leal (%r12,%rsi), %eax
leal (%rax,%rax,8), %eax
addl %r13d, %eax
addl %r15d, %eax
movslq %eax, %rbp
cmpb $0x1, 0x290(%rbx,%rbp)
jne 0x2bc5
movq 0x10(%rsp), %rax
addl %esi, %eax
leal (%rax,%rax,8), %eax
addl %edx, %eax
addl %edi, %eax
movslq %eax, %r15
cmpb $0x1, 0x290(%rbx,%r15)
jne 0x2bc5
cmpl 0x10(%rsp), %r12d
jne 0x2c40
cmpl %edx, %r13d
je 0x2bc5
movl 0x14c(%rbx,%rbp,4), %eax
movl 0x14c(%rbx,%r15,4), %ecx
movl %ecx, 0x14c(%rbx,%rbp,4)
movl %eax, 0x14c(%rbx,%r15,4)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x38(%rax)
cmpl $0xf3, %eax
je 0x2ce9
movq %r15, 0x20(%rsp)
movl $0xf3, %r15d
subl %eax, %r15d
movl 0x1c(%rsp), %r13d
movl %r13d, %eax
subl %r15d, %eax
cvtsi2sd %eax, %xmm0
divsd 0x70(%rsp), %xmm0
callq 0x21f0
movsd %xmm0, 0x10(%rsp)
callq 0x2050
cltd
movl $0x2710, %ecx           # imm = 0x2710
idivl %ecx
cvtsi2sd %edx, %xmm0
divsd 0x13a0(%rip), %xmm0     # 0x4058
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq 0x38(%rsp), %r12
ja 0x2ce1
movl (%r14,%rbp,4), %eax
movq 0x20(%rsp), %rdx
movl (%r14,%rdx,4), %ecx
movl %ecx, (%r14,%rbp,4)
movl %eax, (%r14,%rdx,4)
movl %r13d, %r15d
incl %r12d
jmp 0x2b91
movq 0x38(%rsp), %rax
movl %eax, 0xc(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movq 0x32d3(%rip), %rdi      # 0x5fd8
testb %al, %al
je 0x2d2b
leaq 0x13e6(%rip), %rsi      # 0x40f6
callq 0x2130
movq %rax, %rdi
callq 0x2090
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x40(%rax)
jmp 0x2d3f
leaq 0x13e9(%rip), %rsi      # 0x411b
callq 0x2130
movq %rax, %rdi
callq 0x2090
movq 0x3292(%rip), %rbx      # 0x5fd8
movq %rbx, %rdi
callq 0x2090
leaq 0x13b7(%rip), %rsi      # 0x410c
movq %rbx, %rdi
callq 0x2130
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x21d0
movq %rax, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090