// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
testb $0x1, 0x2e1(%rax)
jne 0x2a37
movq 0x75c1(%rip), %rdi      # 0x9fd8
leaq 0x4687(%rip), %rsi      # 0x70a5
callq 0x2120
movq %rax, %rdi
movq 0x759b(%rip), %rsi      # 0x9fc8
callq 0x2150
jmp 0x2f4f
movl $0x0, -0xc(%rbp)
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
addq $0x14c, %rsi            # imm = 0x14C
movq (%rdi), %rax
callq *0x28(%rax)
testb $0x1, %al
jne 0x2a5b
jmp 0x2e93
movl $0x0, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3fb0
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
addq $0x14c, %rsi            # imm = 0x14C
movq (%rdi), %rax
movq 0x30(%rax), %rax
leaq -0x10(%rbp), %rdx
leaq -0x14(%rbp), %rcx
callq *%rax
jmp 0x2a8c
movq -0x78(%rbp), %rdi
addq $0x14c, %rdi            # imm = 0x14C
movl -0x10(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x40b0
movl %eax, -0x7c(%rbp)
jmp 0x2aa7
movl -0x7c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0x48(%rbp)
cmpl $0x9, -0x48(%rbp)
jg 0x2b68
movq -0x78(%rbp), %rdi
movl -0x48(%rbp), %ecx
movq %rdi, %rsi
addq $0x14c, %rsi            # imm = 0x14C
movl -0x10(%rbp), %edx
movl %edx, %eax
leal (%rax,%rax,8), %r8d
movl -0x14(%rbp), %edx
movl %edx, %eax
movl %r8d, %edx
leal -0xa(%rax,%rdx), %eax
cltq
movl %ecx, 0x14c(%rdi,%rax,4)
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movb %al, -0x7d(%rbp)
jmp 0x2afb
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0x2b04
jmp 0x2b37
movl -0x18(%rbp), %eax
incl %eax
movl %eax, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x40e0
jmp 0x2b1b
jmp 0x2b37
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4050
jmp 0x2f58
movq -0x78(%rbp), %rax
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
imull $0x9, %ecx, %ecx
movl -0x14(%rbp), %edx
subl $0x1, %edx
addl %edx, %ecx
movslq %ecx, %rcx
movl $0x0, 0x14c(%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x2ab4
cmpl $0x0, -0x18(%rbp)
je 0x2c31
leaq -0x30(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x4150
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movq -0x78(%rbp), %rax
movl (%rcx), %edx
movl -0x10(%rbp), %esi
movl %esi, %ecx
leal (%rcx,%rcx,8), %r8d
movl -0x14(%rbp), %esi
movl %esi, %ecx
movl %r8d, %esi
leal -0xa(%rcx,%rsi), %ecx
movslq %ecx, %rcx
movl %edx, 0x14c(%rax,%rcx,4)
addq $0x2e8, %rax            # imm = 0x2E8
movq %rax, -0x90(%rbp)
movl -0x18(%rbp), %eax
imull $0x2710, %eax, %edx     # imm = 0x2710
movl -0x10(%rbp), %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
addl %eax, %edx
movl -0x14(%rbp), %eax
imull $0x64, %eax, %eax
addl %eax, %edx
movl -0x34(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movl %eax, -0x84(%rbp)
callq 0x4150
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl -0x84(%rbp), %eax
movl (%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x4190
jmp 0x2c16
movq -0x78(%rbp), %rdi
addq $0x300, %rdi            # imm = 0x300
leaq -0x30(%rbp), %rsi
callq 0x41c0
jmp 0x2c2c
jmp 0x2e7c
movq -0x78(%rbp), %rdi
addq $0x2e8, %rdi            # imm = 0x2E8
callq 0x4230
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jl 0x2e7a
movq -0x78(%rbp), %rdi
addq $0x2e8, %rdi            # imm = 0x2E8
movslq -0x54(%rbp), %rsi
callq 0x4250
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x58(%rbp)
addq $0x2e8, %rdi            # imm = 0x2E8
movslq -0x54(%rbp), %rsi
callq 0x4250
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x5c(%rbp)
addq $0x2e8, %rdi            # imm = 0x2E8
movslq -0x54(%rbp), %rsi
callq 0x4250
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x60(%rbp)
addq $0x2e8, %rdi            # imm = 0x2E8
movslq -0x54(%rbp), %rsi
callq 0x4250
movq -0x78(%rbp), %rdi
movl (%rax), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x64(%rbp)
addq $0x2e8, %rdi            # imm = 0x2E8
movslq -0x54(%rbp), %rsi
callq 0x4250
movl (%rax), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
movl %eax, -0x68(%rbp)
cmpl $0x1, -0x68(%rbp)
jne 0x2d5d
movq -0x78(%rbp), %rdi
movl -0x58(%rbp), %eax
subl $0x1, %eax
imull $0x9, %eax, %eax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
addl %ecx, %eax
cltq
movl $0x0, 0x14c(%rdi,%rax,4)
addq $0x2e8, %rdi            # imm = 0x2E8
callq 0x4270
movq -0x78(%rbp), %rdi
addq $0x300, %rdi            # imm = 0x300
callq 0x42a0
jmp 0x2e6a
movq -0x78(%rbp), %rdi
addq $0x300, %rdi            # imm = 0x300
movslq -0x54(%rbp), %rsi
callq 0x42d0
movq %rax, -0xa0(%rbp)
jmp 0x2d7a
movq -0xa0(%rbp), %rdi
callq 0x4270
movq -0x78(%rbp), %rdi
addq $0x300, %rdi            # imm = 0x300
movslq -0x54(%rbp), %rsi
callq 0x42d0
movq %rax, -0xa8(%rbp)
jmp 0x2da3
movq -0xa8(%rbp), %rdi
callq 0x4150
movq -0x78(%rbp), %rdi
movl (%rax), %ecx
movl -0x58(%rbp), %edx
movl %edx, %eax
leal (%rax,%rax,8), %esi
movl -0x5c(%rbp), %edx
movl %edx, %eax
movl %esi, %edx
leal -0xa(%rax,%rdx), %eax
cltq
movl %ecx, 0x14c(%rdi,%rax,4)
movl -0x68(%rbp), %eax
decl %eax
movl %eax, -0x68(%rbp)
addq $0x2e8, %rdi            # imm = 0x2E8
movq %rdi, -0xc0(%rbp)
callq 0x4270
movq -0x78(%rbp), %rdi
movl -0x68(%rbp), %eax
imull $0x2710, %eax, %edx     # imm = 0x2710
movl -0x58(%rbp), %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
addl %eax, %edx
movl -0x5c(%rbp), %eax
imull $0x64, %eax, %eax
addl %eax, %edx
movl -0x64(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movl %eax, -0xb4(%rbp)
addq $0x300, %rdi            # imm = 0x300
movslq -0x54(%rbp), %rsi
callq 0x42d0
movq %rax, -0xb0(%rbp)
jmp 0x2e3a
movq -0xb0(%rbp), %rdi
callq 0x4150
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl -0xb4(%rbp), %eax
movl (%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x4190
jmp 0x2e68
jmp 0x2e7a
jmp 0x2e6c
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2c4d
jmp 0x2e7c
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4050
jmp 0x2a3e
movq 0x713e(%rip), %rdi      # 0x9fd8
leaq 0x422d(%rip), %rsi      # 0x70ce
callq 0x2120
movq %rax, %rdi
movq 0x7118(%rip), %rsi      # 0x9fc8
callq 0x2150
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
addq $0x8, %rsi
movq (%rdi), %rax
callq *0x40(%rax)
movq 0x710b(%rip), %rdi      # 0x9fd8
movq 0x70f4(%rip), %rsi      # 0x9fc8
callq 0x2150
movq 0x70f8(%rip), %rdi      # 0x9fd8
leaq 0x41ff(%rip), %rsi      # 0x70e6
callq 0x2120
movq %rax, %rdi
movq 0x70d2(%rip), %rsi      # 0x9fc8
callq 0x2150
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
addq $0x14c, %rsi            # imm = 0x14C
movq (%rdi), %rax
callq *0x40(%rax)
movq 0x70c2(%rip), %rdi      # 0x9fd8
movq 0x70ab(%rip), %rsi      # 0x9fc8
callq 0x2150
movq 0x70af(%rip), %rdi      # 0x9fd8
leaq 0x41cc(%rip), %rsi      # 0x70fc
callq 0x2120
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x21c0
movq %rax, %rdi
movq 0x707e(%rip), %rsi      # 0x9fc8
callq 0x2150
addq $0xc0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x21d0
nopw %cs:(%rax,%rax)