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

    int step = 0;
    while (CheckEmpty(Current))
    {
        int x, y;
        int guesstime = 0;
        vector<int> guess_number;
        int original_number;
        FindMaxConstrain(Current, x, y);

        original_number = FindElement(Current, x, y);

        for (int i = 1; i <= 9; i++)
        {
            Current[9 * (x - 1) + (y - 1)] = i;
            if (CheckConsistency(Current))
            {
                guesstime++;
                guess_number.push_back(i);
            }
            Current[9 * (x - 1) + (y - 1)] = 0;
        }

        if (guesstime != 0)
        {
            Current[9 * (x - 1) + (y - 1)] = guess_number.back();
            // Here I use 5 digits to store the change
            RecordChange.push_back(guesstime * 10000 + x * 1000 + y * 100 + original_number * 10 + guess_number.back());
            GuessNumber.push_back(guess_number);
        }
        else
        {
            int ChangeSize = RecordChange.size();
            for (int i = ChangeSize - 1; i >= 0; i--)
            {
                int xc, yc, origin, guess, time;
                // recover the last change
                xc = (RecordChange[i] / 1000) % 10;
                yc = (RecordChange[i] / 100) % 10;
                origin = (RecordChange[i] / 10) % 10;
                guess = RecordChange[i] % 10;
                time = RecordChange[i] / 10000;

                if (time == 1)
                {
                    // if it's the last guess, undo the change
                    Current[9 * (xc - 1) + (yc - 1)] = 0;
                    RecordChange.pop_back(); // delete the change
                    GuessNumber.pop_back();
                }
                else
                {
                    // if we have other possible value, change a guess number
                    GuessNumber.at(i).pop_back();
                    Current[9 * (xc - 1) + (yc - 1)] = GuessNumber.at(i).back();
                    time--;
                    RecordChange.pop_back();
                    RecordChange.push_back(time * 10000 + xc * 1000 + yc * 100 + guess * 10 + GuessNumber.at(i).back());
                    break;
                }
            }
        }

        step++;
    }

    cout << "The Original Sudoku is " << endl;
    PrintSudoku(OriginalSudoku);
    cout << endl;

    cout << "The Filled Sudoku is " << endl;
    PrintSudoku(Current);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpb $0x1, 0x2e1(%rdi)
jne 0x28d4
movq %rdi, %r14
leaq 0x14c(%rdi), %r15
leaq 0x2e8(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x300(%rdi), %r13
xorl %ebp, %ebp
movq %r15, 0x28(%rsp)
movq %r13, 0x8(%rsp)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x28(%rax)
testb %al, %al
je 0x28fd
movl %ebp, 0x24(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq *0x30(%rax)
movl 0x14(%rsp), %eax
movl 0x10(%rsp), %ecx
leal (%rax,%rax,8), %edx
addl %ecx, %edx
addl $-0xa, %edx
movslq %edx, %rdx
movq 0x28(%rsp), %r15
movl (%r15,%rdx,4), %ebx
xorl %ebp, %ebp
pushq $0x1
popq %rdx
leaq 0x30(%rsp), %r12
movl %edx, 0x4(%rsp)
cmpl $0xa, %edx
jge 0x2732
leal (%rax,%rax,8), %eax
addl %ecx, %eax
addl $-0xa, %eax
cltq
movl %edx, 0x14c(%r14,%rax,4)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
testb %al, %al
je 0x2712
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x32ee
incl %ebp
movl 0x14(%rsp), %eax
leal (%rax,%rax,8), %edx
movl 0x10(%rsp), %ecx
addl %ecx, %edx
addl $-0xa, %edx
movslq %edx, %rdx
andl $0x0, (%r15,%rdx,4)
movl 0x4(%rsp), %edx
incl %edx
jmp 0x26d8
testl %ebp, %ebp
je 0x278a
movq 0x38(%rsp), %rdx
movl -0x4(%rdx), %edx
leal (%rax,%rax,8), %esi
addl %ecx, %esi
addl $-0xa, %esi
movslq %esi, %rsi
movl %edx, (%r15,%rsi,4)
imull $0x2710, %ebp, %esi     # imm = 0x2710
imull $0x3e8, %eax, %eax      # imm = 0x3E8
imull $0x64, %ecx, %ecx
addl %eax, %ecx
imull $0xa, %ebx, %eax
addl %esi, %eax
addl %ecx, %eax
addl %edx, %eax
movl %eax, 0x4(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x354e
movq %r13, %rdi
movq %r12, %rsi
callq 0x330c
jmp 0x28ba
movq 0x2f0(%r14), %rax
subq 0x2e8(%r14), %rax
shrq $0x2, %rax
movl %eax, %r12d
decq %r12
leal 0x1(%r12), %eax
testl %eax, %eax
jle 0x28ba
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl (%rax,%r12,4), %r15d
movl %r15d, %eax
cltd
movl $0x3e8, %ecx            # imm = 0x3E8
idivl %ecx
cltd
pushq $0xa
popq %rcx
idivl %ecx
movl %edx, %ebx
movl %r15d, %eax
cltd
pushq $0x64
popq %rsi
idivl %esi
cltd
idivl %ecx
movl %edx, %ebp
movl %r15d, %r13d
addl $0xffffd8f0, %r13d      # imm = 0xFFFFD8F0
cmpl $0x2710, %r13d          # imm = 0x2710
jae 0x2819
leal (%rbx,%rbx,8), %eax
addl %ebp, %eax
addl $-0xa, %eax
cltq
andl $0x0, 0x14c(%r14,%rax,4)
addq $-0x4, 0x2f0(%r14)
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x3338
jmp 0x279f
movl %r15d, %eax
cltd
idivl %ecx
movl %edx, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x334c
addq $-0x4, 0x8(%rax)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x334c
movq 0x8(%rax), %rax
movl -0x4(%rax), %eax
leal (%rbx,%rbx,8), %ecx
addl %ebp, %ecx
addl $-0xa, %ecx
movslq %ecx, %rcx
movl %eax, 0x14c(%r14,%rcx,4)
addq $-0x4, 0x2f0(%r14)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x334c
movq %rax, %rcx
movl %r15d, %eax
cltd
movl $0x2710, %esi           # imm = 0x2710
idivl %esi
imull $0x3e8, %ebx, %eax      # imm = 0x3E8
imull $0x64, %ebp, %esi
imull $0xa, 0x20(%rsp), %edi
movq 0x8(%rcx), %rcx
subl %edx, %r13d
addl %edi, %r13d
addl %eax, %r13d
addl %esi, %r13d
addl -0x4(%rcx), %r13d
movl %r13d, 0x4(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x354e
movq 0x8(%rsp), %r13
movl 0x24(%rsp), %ebp
incl %ebp
leaq 0x30(%rsp), %rdi
callq 0x3376
movq 0x28(%rsp), %r15
jmp 0x2676
movq 0x36fd(%rip), %rdi      # 0x5fd8
leaq 0x17d3(%rip), %rsi      # 0x40b5
callq 0x2130
movq %rax, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090
movq 0x36d4(%rip), %rbx      # 0x5fd8
leaq 0x17d3(%rip), %rsi      # 0x40de
movq %rbx, %rdi
callq 0x2130
movq %rax, %rdi
callq 0x2090
leaq 0x8(%r14), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x40(%rax)
movq %rbx, %rdi
callq 0x2090
leaq 0x17bf(%rip), %rsi      # 0x40f6
movq %rbx, %rdi
callq 0x2130
movq %rax, %rdi
callq 0x2090
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x40(%rax)
movq %rbx, %rdi
callq 0x2090
leaq 0x17aa(%rip), %rsi      # 0x410c
movq %rbx, %rdi
callq 0x2130
movq %rax, %rdi
movl %ebp, %esi
callq 0x21d0
movq %rax, %rdi
callq 0x2090
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3376
movq %rbx, %rdi
callq 0x21e0