// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1dc8, %rsp           # imm = 0x1DC8
leaq 0x6f0(%rsp), %rdi
pushq $0x1
popq %rbx
movl $0xb64, %edx            # imm = 0xB64
movl %ebx, %esi
callq 0x1090
leaq 0x1e0(%rsp), %rdi
movl $0x510, %edx            # imm = 0x510
movl %ebx, %esi
callq 0x1090
pushq $-0x1
popq %r15
pushq $0x3
popq %rbp
movq %r15, (%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
cmpq $0x9, %rcx
je 0x1475
imull $0xf3, %eax, %esi
leaq (%rcx,%rcx,8), %rdi
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
imull $0x1b, %eax, %eax
addl $0xf2, %eax
movl %eax, 0x1c(%rsp)
movq %rdi, 0x38(%rsp)
leal 0x50(%rdi), %eax
movq %rax, 0x30(%rsp)
decl %esi
movq %rsi, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
imulq $0x24, %rcx, %rax
addq %rsp, %rax
addq $0x90, %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x28(%rsp)
xorl %r12d, %r12d
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
cmpq $0x9, %r12
je 0x1432
movl %eax, %r13d
movq 0x10(%rsp), %rax
leaq (%rax,%r12,4), %rbx
leaq 0x1cb9(%rip), %rdi      # 0x3004
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10e0
movl (%rbx), %edi
cmpq $0x9, %rdi
ja 0x1454
testq %rdi, %rdi
je 0x1422
imull $0x1b, %r13d, %r9d
leaq (%r12,%r12,8), %r8
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
movl %eax, %ecx
movq 0x8(%rsp), %rax
addl %edi, %eax
addl %eax, %r9d
movq (%rsp), %rax
leaq (%rax,%rdi), %r10
leaq (%r15,%rdi), %r11
movq %r14, %rbx
xorl %esi, %esi
movl %esi, %eax
xorl %edx, %edx
divl %ebp
cmpq $0x9, %rsi
je 0x13e1
imull $0x36, %eax, %eax
andl $0x0, 0x6f0(%rsp,%rbx)
addl %r9d, %eax
andl $0x0, 0x6f0(%rsp,%r10,4)
andl $0x0, 0x6f0(%rsp,%r11,4)
cltq
andl $0x0, 0x6f0(%rsp,%rax,4)
incq %rsi
addl $0x9, %r9d
addq $0x4, %rbx
addq $0x9, %r10
addq $0x51, %r11
jmp 0x1396
movq 0x38(%rsp), %rax
addq %r12, %rax
andl $0x0, 0x1e0(%rsp,%rax,4)
movq 0x30(%rsp), %rax
addl %edi, %eax
andl $0x0, 0x1e0(%rsp,%rax,4)
leal (%r8,%rdi), %eax
addl $0xa1, %eax
andl $0x0, 0x1e0(%rsp,%rax,4)
leal (%rcx,%rcx,8), %eax
addl 0x1c(%rsp), %eax
addl %edi, %eax
andl $0x0, 0x1e0(%rsp,%rax,4)
incq %r12
addq $0x24, %r14
addq $0x9, %r15
jmp 0x1327
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r14
addq $0x144, %r14            # imm = 0x144
addq $0x51, (%rsp)
pushq $-0x1
popq %r15
jmp 0x12c8
leaq 0x1bb0(%rip), %rdi      # 0x300b
callq 0x1120
pushq $0x1
popq %rax
addq $0x1dc8, %rsp           # imm = 0x1DC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %esi, %esi
cmpq $0x2d9, %rax            # imm = 0x2D9
je 0x14ac
cmpl $0x0, 0x6f0(%rsp,%rax,4)
je 0x149e
movl %esi, %edx
movslq %esi, %rsi
movl %eax, 0x1260(%rsp,%rsi,4)
incl %esi
jmp 0x14a0
movl %ecx, %edx
movl %edx, 0x6f0(%rsp,%rax,4)
incq %rax
jmp 0x147c
xorl %eax, %eax
xorl %r15d, %r15d
cmpq $0x144, %rax            # imm = 0x144
je 0x14e5
movl 0x1e0(%rsp,%rax,4), %ecx
cmpl $0x1, %ecx
movl $0x0, %edx
sbbl %edx, %edx
cmpl $0x1, %ecx
movl %r15d, %ecx
sbbl $-0x1, %ecx
orl %r15d, %edx
movl %edx, 0x1e0(%rsp,%rax,4)
incq %rax
movl %ecx, %r15d
jmp 0x14b1
movl %r15d, %eax
movl %esi, 0x8(%rsp)
imull %esi, %eax
movslq %eax, %r13
movq 0x3ad7(%rip), %rsi      # 0x4fd0
movq %r13, %rdi
callq 0x1150
testq %rax, %rax
je 0x16f0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1090
xorl %ecx, %ecx
leaq 0x324(%rsp), %rsi
leaq 0x6f0(%rsp), %r11
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
cmpq $0x9, %rcx
je 0x1630
imull $0x1b, %eax, %r8d
movq %rcx, (%rsp)
leaq (%rcx,%rcx,8), %r9
addl $0xf3, %r8d
leaq 0x468(%rsp), %r10
movq %r11, 0x10(%rsp)
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
cmpq $0x9, %rcx
je 0x1614
leal (%rax,%rax,8), %eax
addl %r8d, %eax
leaq (%rsp,%rax,4), %rax
addq $0x1e0, %rax            # imm = 0x1E0
leaq (%rcx,%r9), %rdx
xorl %r13d, %r13d
cmpq $0x24, %r13
je 0x1604
movl (%r11,%r13), %r12d
cmpl $-0x1, %r12d
je 0x15fe
movl 0x1e0(%rsp,%rdx,4), %ebx
cmpl $-0x1, %ebx
je 0x15b2
movl %r12d, %edi
imull %r15d, %edi
addl %edi, %ebx
movslq %ebx, %rdi
movb $0x1, (%r14,%rdi)
movl (%rsi,%r13), %ebx
cmpl $-0x1, %ebx
je 0x15cc
movl %r12d, %edi
imull %r15d, %edi
addl %edi, %ebx
movslq %ebx, %rdi
movb $0x1, (%r14,%rdi)
movl (%r10,%r13), %ebx
cmpl $-0x1, %ebx
je 0x15e6
movl %r12d, %edi
imull %r15d, %edi
addl %edi, %ebx
movslq %ebx, %rdi
movb $0x1, (%r14,%rdi)
movl (%rax,%r13), %ebx
cmpl $-0x1, %ebx
je 0x15fe
imull %r15d, %r12d
addl %r12d, %ebx
movslq %ebx, %rdi
movb $0x1, (%r14,%rdi)
addq $0x4, %r13
jmp 0x1585
incq %rcx
addq $0x24, %r11
addq $0x24, %r10
jmp 0x155e
movq (%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %r11
addq $0x144, %r11            # imm = 0x144
addq $0x24, %rsi
jmp 0x152c
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x1860
movq %r12, %rdi
movl 0x8(%rsp), %esi
movl %r15d, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x194a
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x70(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0x58(%rsp), %rdi
pushq $-0x1
popq %r8
xorl %ecx, %ecx
callq 0x1c5c
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
cmpq $0x18, %rcx
jne 0x1742
movq (%rax), %r8
movq 0x8(%rax), %r9
subq %r8, %r9
sarq $0x2, %r9
xorl %r10d, %r10d
pushq $0x9
popq %r11
pushq $0x51
popq %rbx
cmpq %r10, %r9
je 0x16fc
movslq (%r8,%r10,4), %rax
movl 0x1260(%rsp,%rax,4), %ecx
movl %ecx, %eax
cltd
idivl %r11d
movl %eax, %esi
movl %edx, %edi
incl %edi
movl %ecx, %eax
cltd
idivl %ebx
movslq %eax, %rcx
movl %esi, %eax
cltd
idivl %r11d
movslq %edx, %rax
imulq $0x24, %rcx, %rcx
addq %rsp, %rcx
addq $0x90, %rcx
movl %edi, (%rcx,%rax,4)
incq %r10
jmp 0x16ad
leaq 0x1928(%rip), %rdi      # 0x301f
jmp 0x145b
leaq 0x90(%rsp), %r12
pushq $0xa
popq %rbx
leaq 0x18f9(%rip), %r15      # 0x3007
xorl %r13d, %r13d
cmpq $0x9, %r13
je 0x1742
xorl %ebp, %ebp
cmpq $0x9, %rbp
je 0x1732
movl (%r12,%rbp,4), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %rbp
jmp 0x1719
movl %ebx, %edi
callq 0x1100
incq %r13
addq $0x24, %r12
jmp 0x1711
movq %r14, %rdi
callq 0x10f0
leaq 0x70(%rsp), %rdi
callq 0x182c
leaq 0x40(%rsp), %rdi
callq 0x179e
leaq 0x58(%rsp), %rdi
callq 0x18a8
xorl %eax, %eax
jmp 0x1463
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x182c
leaq 0x40(%rsp), %rdi
callq 0x179e
jmp 0x178b
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x18a8
movq %rbx, %rdi
callq 0x1160
nop