// File: /workspace/github_cpp_makefile_star2_O1/razimantv[P]DancingLinks/sudoku/sudoku.cpp
// Function: main
int main() {
  // Every placement of a number in a position is a subset. Hence R=9*9*9
  // There are constraints of 4 types:
  //  A cell can have only 1 number : 9*9 constraints
  //  A row can have a number only once : 9*9 constraints
  //  A column can have a number only once : 9*9 constraints
  //  A box can have a number only once : 9*9 constraints
  // Overall, C = 9*9*4
  const int rows = 9 * 9 * 9;
  const int columns = 9 * 9 * 4;

  // Existing numbers in sudoku cells result in already satisfied constraints
  //  and forbidden columns
  // ar1 stores whether a particular row is non-forbidden and is later used to
  // find the ith non-forbidden row
  // ar2 stores whether a particular column is non-forbidden and is later used
  // to
  //  find the ith non-forbidden column
  int ar1[rows], ar2[columns];
  memset(ar1, 1, sizeof ar1);
  memset(ar2, 1, sizeof ar2);

  // Read the initial position of the board and make rows/columns forbidden
  //  based on existing entries
  int board[9][9];
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      scanf("%d", &board[i][j]);
      if ((board[i][j] < 0) || (board[i][j] > 9)) {
        printf("Invalid input board\n");
        return 1;
      }
      if (board[i][j] != 0) {
        // Cell is already filled
        // Find the rows and columns it blocks
        for (int k = 0; k < 9; k++) {
          // Other numbers can't end up in the same cell now
          ar1[i * 81 + j * 9 + k] = 0;

          // This number can't appear in another column in the same row
          ar1[i * 81 + k * 9 + board[i][j] - 1] = 0;

          // This number can't appear in another row in the same column
          ar1[k * 81 + j * 9 + board[i][j] - 1] = 0;

          // This number can't appear in another cell in the same box
          ar1[((i / 3) * 3 + k / 3) * 81 + ((j / 3) * 3 + k % 3) * 9 +
              board[i][j] - 1] = 0;
        }

        // Cell constraint satisfied
        ar2[i * 9 + j] = 0;

        // Row constraint satisfied
        ar2[81 + i * 9 + board[i][j] - 1] = 0;

        // Colum constraint satisfied
        ar2[162 + j * 9 + board[i][j] - 1] = 0;

        // Box constraint satisfied
        ar2[243 + (i / 3) * 27 + (j / 3) * 9 + board[i][j] - 1] = 0;
      }
    }
  }

  // Find partial sums of ar1 and ar2 to find out their position among
  // non-forbidden rows and columns. invar1 gives the inverse of ar1 - that is,
  // the ith non-forbidden row
  int invar1[rows];
  int cum1 = 0, cum2 = 0;
  for (int i = 0; i < rows; i++) {
    if (ar1[i]) {
      ar1[i] = cum1;
      invar1[cum1] = i;
      cum1++;
    } else
      ar1[i] = -1;
  }
  for (int i = 0; i < columns; i++) {
    if (ar2[i])
      ar2[i] = cum2++;
    else
      ar2[i] = -1;
  }

  // The number of rows and columns we really need to deal with are just cum1
  // and
  //  cum2 respectively
  int R = cum1, C1 = cum2, C2 = 0, C = C1 + C2;

  // Matrix to store the constraint adjacency matrix
  char *mat;
  mat = new (nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);

  // For non-forbidden rows and columns, fill in elements in the matrix
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      for (int k = 0; k < 9; k++) {
        int r = i * 81 + j * 9 + k;
        if (ar1[r] == -1) continue;
        int c1 = i * 9 + j, c2 = 81 + i * 9 + k, c3 = 162 + j * 9 + k,
            c4 = 243 + (i / 3) * 27 + (j / 3) * 9 + k;
        if (ar2[c1] != -1) mat[ar1[r] * C + ar2[c1]] = 1;
        if (ar2[c2] != -1) mat[ar1[r] * C + ar2[c2]] = 1;
        if (ar2[c3] != -1) mat[ar1[r] * C + ar2[c3]] = 1;
        if (ar2[c4] != -1) mat[ar1[r] * C + ar2[c4]] = 1;
      }
    }
  }

  // Now just solve it with dancing links
  // Note that we are asking it to find only one solution
  DancingLinks N;
  N.Create(R, C1, C2, mat);
  std::vector<std::vector<int> > all;
  std::vector<int> cur;
  N.solve(all, cur, false);

  if (all.size() == 1) {
    // Fill in the board using chosen subsets
    for (int i = 0; i < all[0].size(); i++) {
      int x = invar1[all[0][i]];
      board[x / 81][(x / 9) % 9] = x % 9 + 1;
    }

    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        printf("%d ", board[i][j]);
      }
      printf("\n");
    }
  }
  delete[] mat;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1dd8, %rsp           # imm = 0x1DD8
leaq 0x700(%rsp), %rbx
movl $0xb64, %edx            # imm = 0xB64
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1090
leaq 0x1f0(%rsp), %rdi
movl $0x510, %edx            # imm = 0x510
movl $0x1, %esi
callq 0x1090
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq $0x0, (%rsp)
xorl %ecx, %ecx
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movq %rcx, 0x38(%rsp)
leaq (%rcx,%rcx,8), %r13
leaq (%rsp,%r13,4), %rbp
addq $0xa0, %rbp
xorl %r14d, %r14d
leaq 0x1cd7(%rip), %rdi      # 0x3004
movq %rbp, %rsi
xorl %eax, %eax
callq 0x10e0
movq %rbp, 0x10(%rsp)
movl (%rbp), %eax
cmpl $0x9, %eax
ja 0x14c6
movl %r12d, 0x20(%rsp)
movl 0xc(%rsp), %ecx
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rdx, %rcx
shrq $0x21, %rcx
imull $0xf3, %ecx, %ecx
movq %rcx, 0x48(%rsp)
movl 0x38(%rsp), %ecx
imulq %rdx, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,8), %ecx
leal (%rcx,%rcx,2), %ecx
addl $0xf2, %ecx
movl %ecx, 0x24(%rsp)
leal 0x50(%r13), %ecx
movq %rcx, 0x40(%rsp)
movq %rbx, 0x28(%rsp)
xorl %r12d, %r12d
movq %r12, %rbp
testl %eax, %eax
je 0x1470
movl %ebp, %ecx
movl $0xaaaaaaab, %r10d      # imm = 0xAAAAAAAB
imulq %r10, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,8), %ecx
leal (%rcx,%rcx,2), %ecx
leal (%rax,%r14), %edx
movq (%rsp), %rsi
addl %eax, %esi
movq 0x48(%rsp), %rdi
addl %eax, %edi
addl %edi, %ecx
xorl %edi, %edi
movl %edi, %r8d
imulq %r10, %r8
shrq $0x21, %r8
imull $0x36, %r8d, %r8d
movl %r15d, (%rbx,%rdi,4)
leal -0x1(%rsi), %r9d
movslq %r9d, %r9
movl %r15d, 0x700(%rsp,%r9,4)
leal -0x1(%rdx), %r9d
movslq %r9d, %r9
movl %r15d, 0x700(%rsp,%r9,4)
addl %ecx, %r8d
decl %r8d
movslq %r8d, %r8
movl %r15d, 0x700(%rsp,%r8,4)
incq %rdi
addl $0x51, %edx
addl $0x9, %esi
addl $0x9, %ecx
cmpq $0x9, %rdi
jne 0x13cc
movq %rbp, %rcx
addq %r13, %rcx
movl %r15d, 0x1f0(%rsp,%rcx,4)
movq 0x40(%rsp), %rcx
addl %eax, %ecx
movl %r15d, 0x1f0(%rsp,%rcx,4)
leal (%rbp,%rbp,8), %ecx
leal 0xa1(%rax,%rcx), %ecx
movslq %ecx, %rcx
movl %r15d, 0x1f0(%rsp,%rcx,4)
movl %ebp, %ecx
imulq %r10, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,8), %ecx
addl 0x24(%rsp), %eax
addl %ecx, %eax
cltq
movl %r15d, 0x1f0(%rsp,%rax,4)
leaq 0x1(%rbp), %r12
cmpq $0x9, %r12
je 0x14da
movq %r14, 0x18(%rsp)
movq %r13, %r14
movq 0x10(%rsp), %rax
leaq (%rax,%r12,4), %r13
leaq 0x1b72(%rip), %rdi      # 0x3004
movq %r13, %rsi
xorl %eax, %eax
callq 0x10e0
movl (%r13), %eax
movq %r14, %r13
movq 0x18(%rsp), %r14
addl $0x9, %r14d
addq $0x24, %rbx
cmpl $0xa, %eax
jb 0x1396
cmpq $0x8, %rbp
setae %r14b
movq 0x28(%rsp), %rbx
leaq 0x1b3e(%rip), %rdi      # 0x300b
callq 0x1120
movl $0x1, %r12d
jmp 0x14ec
cmpq $0x8, %rbp
setae %r14b
movq 0x28(%rsp), %rbx
movl 0x20(%rsp), %r12d
testb %r14b, %r14b
movq 0x38(%rsp), %rax
je 0x1529
cmpq $0x8, %rax
leaq 0x1(%rax), %rax
setae %cl
movq %rcx, 0x30(%rsp)
movq (%rsp), %rcx
addl $0x51, %ecx
movq %rcx, (%rsp)
addq $0x144, %rbx            # imm = 0x144
incl 0xc(%rsp)
movq %rax, %rcx
cmpq $0x9, %rax
jne 0x130f
testb $0x1, 0x30(%rsp)
je 0x188f
xorl %eax, %eax
xorl %r14d, %r14d
cmpl $0x0, 0x700(%rsp,%rax,4)
je 0x155b
movl %r14d, 0x700(%rsp,%rax,4)
movslq %r14d, %r14
movl %eax, 0x1270(%rsp,%r14,4)
incl %r14d
jmp 0x1566
movl $0xffffffff, 0x700(%rsp,%rax,4) # imm = 0xFFFFFFFF
incq %rax
cmpq $0x2d9, %rax            # imm = 0x2D9
jne 0x1539
xorl %eax, %eax
xorl %ebp, %ebp
movl 0x1f0(%rsp,%rax,4), %ecx
cmpl $0x1, %ecx
movl $0x0, %edx
sbbl %edx, %edx
movl %ebp, %esi
cmpl $0x1, %ecx
sbbl $-0x1, %ebp
orl %esi, %edx
movl %edx, 0x1f0(%rsp,%rax,4)
incq %rax
cmpq $0x144, %rax            # imm = 0x144
jne 0x1575
movl %ebp, %eax
imull %r14d, %eax
movslq %eax, %r15
movq 0x3a1e(%rip), %rsi      # 0x4fd0
movq %r15, %rdi
callq 0x1150
testq %rax, %rax
je 0x187d
movq %rax, %rbx
movq %r14, (%rsp)
xorl %r14d, %r14d
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1090
leaq 0x700(%rsp), %r9
leaq 0x334(%rsp), %rdx
movl $0xaaaaaaab, %esi       # imm = 0xAAAAAAAB
movl %r14d, %eax
imulq %rsi, %rax
shrq $0x21, %rax
leal (%rax,%rax,8), %eax
movq %r14, 0x18(%rsp)
leaq (%r14,%r14,8), %rdi
leal (%rax,%rax,2), %r8d
addl $0xf3, %r8d
movq %r9, 0x10(%rsp)
leaq 0x478(%rsp), %r10
xorl %r11d, %r11d
movl %r11d, %eax
imulq %rsi, %rax
shrq $0x21, %rax
leal (%rax,%rax,8), %eax
addl %r8d, %eax
leaq (%rsp,%rax,4), %r15
addq $0x1f0, %r15            # imm = 0x1F0
leaq (%r11,%rdi), %r13
xorl %eax, %eax
movl (%r9,%rax), %r12d
cmpl $-0x1, %r12d
je 0x16b5
movl 0x1f0(%rsp,%r13,4), %ecx
cmpl $-0x1, %ecx
je 0x166b
movl %r12d, %r14d
imull %ebp, %r14d
addl %r14d, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%rdx,%rax), %ecx
cmpl $-0x1, %ecx
je 0x1684
movl %r12d, %r14d
imull %ebp, %r14d
addl %r14d, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%r10,%rax), %ecx
cmpl $-0x1, %ecx
je 0x169e
movl %r12d, %r14d
imull %ebp, %r14d
addl %r14d, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%r15,%rax), %ecx
cmpl $-0x1, %ecx
je 0x16b5
imull %ebp, %r12d
addl %r12d, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
addq $0x4, %rax
cmpq $0x24, %rax
jne 0x1643
incq %r11
addq $0x24, %r10
addq $0x24, %r9
cmpq $0x9, %r11
jne 0x1621
movq 0x18(%rsp), %r14
incq %r14
addq $0x24, %rdx
movq 0x10(%rsp), %r9
addq $0x144, %r9             # imm = 0x144
cmpq $0x9, %r14
jne 0x15ef
leaq 0x88(%rsp), %r15
movq %r15, %rdi
callq 0x1930
movq %r15, %rdi
movq (%rsp), %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x1a28
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0x50(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0x88(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x1d66
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %rax
subq %rcx, %rax
cmpq $0x18, %rax
jne 0x1842
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0x17ff
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movslq (%rax,%rdx,4), %rsi
movslq 0x1270(%rsp,%rsi,4), %rsi
imulq $0x38e38e39, %rsi, %rdi # imm = 0x38E38E39
movq %rdi, %r8
shrq $0x3f, %r8
sarq $0x21, %rdi
addl %r8d, %edi
leal (%rdi,%rdi,8), %r8d
imulq $0x1948b0fd, %rsi, %r9  # imm = 0x1948B0FD
subl %r8d, %esi
incl %esi
movq %r9, %r8
shrq $0x3f, %r8
sarq $0x23, %r9
addl %r8d, %r9d
movslq %r9d, %r8
movslq %edi, %rdi
imulq $0x38e38e39, %rdi, %r9  # imm = 0x38E38E39
movq %r9, %r10
shrq $0x3f, %r10
sarq $0x21, %r9
addl %r10d, %r9d
leal (%r9,%r9,8), %r9d
subl %r9d, %edi
movslq %edi, %rdi
leaq (%r8,%r8,8), %r8
leaq (%rsp,%r8,4), %r8
addq $0xa0, %r8
movl %esi, (%r8,%rdi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x1780
leaq 0xa0(%rsp), %r15
leaq 0x17f9(%rip), %r14      # 0x3007
xorl %r12d, %r12d
xorl %r13d, %r13d
movl (%r15,%r13,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
incq %r13
cmpq $0x9, %r13
jne 0x1814
movl $0xa, %edi
callq 0x1100
incq %r12
addq $0x24, %r15
cmpq $0x9, %r12
jne 0x1811
movq %rbx, %rdi
callq 0x10f0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1861
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x70(%rsp), %rdi
callq 0x18e2
leaq 0x88(%rsp), %rdi
callq 0x197c
xorl %r12d, %r12d
jmp 0x188f
leaq 0x179b(%rip), %rdi      # 0x301f
callq 0x1120
movl $0x1, %r12d
movl %r12d, %eax
addq $0x1dd8, %rsp           # imm = 0x1DD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x18be
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x70(%rsp), %rdi
callq 0x18e2
jmp 0x18cd
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x197c
movq %rbx, %rdi
callq 0x1160