// File: /workspace/github_cpp_makefile_star2_O3/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), %rbp
movl $0xb64, %edx            # imm = 0xB64
movq %rbp, %rdi
movl $0x1, %esi
callq 0x1090
leaq 0x1e0(%rsp), %rdi
movl $0x510, %edx            # imm = 0x510
movl $0x1, %esi
callq 0x1090
xorl %r12d, %r12d
movl $0xaaaaaaab, %r13d      # imm = 0xAAAAAAAB
movq $0x0, (%rsp)
xorl %ecx, %ecx
movl %ecx, %eax
imulq %r13, %rax
shrq $0x21, %rax
imull $0xf3, %eax, %edx
movq %rdx, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
leaq (%rcx,%rcx,8), %rcx
leal (%rax,%rax,8), %eax
leal 0xf2(%rax,%rax,2), %eax
movl %eax, 0x1c(%rsp)
leal 0x50(%rcx), %eax
movq %rax, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
leaq (%rsp,%rcx,4), %rax
addq $0x90, %rax
movq %rax, 0x10(%rsp)
movq %rbp, 0x28(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
leaq (%rax,%rbx,4), %r15
leaq 0x1c81(%rip), %rdi      # 0x3004
movq %r15, %rsi
xorl %eax, %eax
callq 0x10e0
movl (%r15), %eax
cmpl $0x9, %eax
ja 0x17ea
testl %eax, %eax
je 0x1474
movl %ebx, %ecx
imulq %r13, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,8), %ecx
leal (%rcx,%rcx,2), %esi
leaq (%rbx,%rbx,8), %rdx
movl %ebx, %ecx
imulq %r13, %rcx
shrq $0x21, %rcx
movq 0x8(%rsp), %rdi
addl %eax, %edi
addl %edi, %esi
leal (%rax,%r14), %edi
movq (%rsp), %r8
addl %eax, %r8d
xorl %r9d, %r9d
movl %r9d, %r10d
imulq %r13, %r10
shrq $0x21, %r10
imull $0x36, %r10d, %r10d
movl %r12d, (%rbp,%r9,4)
leal -0x1(%r8), %r11d
movslq %r11d, %r11
movl %r12d, 0x6f0(%rsp,%r11,4)
leal -0x1(%rdi), %r11d
movslq %r11d, %r11
movl %r12d, 0x6f0(%rsp,%r11,4)
addl %esi, %r10d
decl %r10d
movslq %r10d, %r10
movl %r12d, 0x6f0(%rsp,%r10,4)
incq %r9
addl $0x9, %esi
addl $0x51, %edi
addl $0x9, %r8d
cmpq $0x9, %r9
jne 0x13d6
movq 0x38(%rsp), %rsi
addq %rbx, %rsi
movl %r12d, 0x1e0(%rsp,%rsi,4)
movq 0x30(%rsp), %rsi
addl %eax, %esi
movslq %esi, %rsi
movl %r12d, 0x1e0(%rsp,%rsi,4)
leal 0xa1(%rdx,%rax), %edx
movslq %edx, %rdx
movl %r12d, 0x1e0(%rsp,%rdx,4)
leal (%rcx,%rcx,8), %ecx
addl 0x1c(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movl %r12d, 0x1e0(%rsp,%rax,4)
incq %rbx
addl $0x9, %r14d
addq $0x24, %rbp
cmpq $0x9, %rbx
jne 0x1373
movq 0x20(%rsp), %rcx
incq %rcx
movq (%rsp), %rax
addl $0x51, %eax
movq %rax, (%rsp)
movq 0x28(%rsp), %rbp
addq $0x144, %rbp            # imm = 0x144
cmpq $0x9, %rcx
jne 0x1321
xorl %eax, %eax
xorl %ebp, %ebp
cmpl $0x0, 0x6f0(%rsp,%rax,4)
je 0x14d2
movl %ebp, %ecx
movslq %ebp, %rdx
movl %eax, 0x1260(%rsp,%rdx,4)
incl %edx
movl %edx, %ebp
jmp 0x14d7
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x6f0(%rsp,%rax,4)
incq %rax
cmpq $0x2d9, %rax            # imm = 0x2D9
jne 0x14b6
xorl %eax, %eax
xorl %r14d, %r14d
movl 0x1e0(%rsp,%rax,4), %ecx
cmpl $0x1, %ecx
movl $0x0, %edx
sbbl %edx, %edx
movl %r14d, %esi
cmpl $0x1, %ecx
sbbl $-0x1, %r14d
orl %esi, %edx
movl %edx, 0x1e0(%rsp,%rax,4)
incq %rax
cmpq $0x144, %rax            # imm = 0x144
jne 0x14ee
movl %r14d, %eax
imull %ebp, %eax
movslq %eax, %r15
movq 0x3aa3(%rip), %rsi      # 0x4fd0
movq %r15, %rdi
callq 0x1150
testq %rax, %rax
je 0x180d
movq %rax, %rbx
movl %ebp, 0x8(%rsp)
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1090
leaq 0x6f0(%rsp), %r9
leaq 0x324(%rsp), %rdx
movl $0xaaaaaaab, %esi       # imm = 0xAAAAAAAB
movl %r12d, %eax
imulq %rsi, %rax
shrq $0x21, %rax
leal (%rax,%rax,8), %eax
movq %r12, 0x10(%rsp)
leaq (%r12,%r12,8), %rdi
leal (%rax,%rax,2), %r8d
addl $0xf3, %r8d
movq %r9, (%rsp)
leaq 0x468(%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 $0x1e0, %r15            # imm = 0x1E0
leaq (%r11,%rdi), %r13
xorl %eax, %eax
movl (%r9,%rax), %r12d
cmpl $-0x1, %r12d
je 0x162c
movl 0x1e0(%rsp,%r13,4), %ecx
cmpl $-0x1, %ecx
je 0x15e4
movl %r12d, %ebp
imull %r14d, %ebp
addl %ebp, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%rdx,%rax), %ecx
cmpl $-0x1, %ecx
je 0x15fc
movl %r12d, %ebp
imull %r14d, %ebp
addl %ebp, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%r10,%rax), %ecx
cmpl $-0x1, %ecx
je 0x1615
movl %r12d, %ebp
imull %r14d, %ebp
addl %ebp, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
movl (%r15,%rax), %ecx
cmpl $-0x1, %ecx
je 0x162c
imull %r14d, %r12d
addl %r12d, %ecx
movslq %ecx, %rcx
movb $0x1, (%rbx,%rcx)
addq $0x4, %rax
cmpq $0x24, %rax
jne 0x15bd
incq %r11
addq $0x24, %r10
addq $0x24, %r9
cmpq $0x9, %r11
jne 0x159b
movq 0x10(%rsp), %r12
incq %r12
addq $0x24, %rdx
movq (%rsp), %r9
addq $0x144, %r9             # imm = 0x144
cmpq $0x9, %r12
jne 0x156a
leaq 0x78(%rsp), %r15
movq %r15, %rdi
callq 0x18a8
movq %r15, %rdi
movl 0x8(%rsp), %esi
movl %r14d, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x199e
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0x40(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0x78(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x1cc8
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %rax
subq %rcx, %rax
cmpq $0x18, %rax
jne 0x17b3
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0x1770
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movslq (%rax,%rdx,4), %rsi
movslq 0x1260(%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 $0x90, %r8
movl %esi, (%r8,%rdi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x16f1
leaq 0x90(%rsp), %r15
leaq 0x1888(%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 0x1785
movl $0xa, %edi
callq 0x1100
incq %r12
addq $0x24, %r15
cmpq $0x9, %r12
jne 0x1782
movq %rbx, %rdi
callq 0x10f0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x17d2
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x60(%rsp), %rdi
callq 0x1852
leaq 0x78(%rsp), %rdi
callq 0x18f4
xorl %eax, %eax
jmp 0x17fb
leaq 0x181a(%rip), %rdi      # 0x300b
callq 0x1120
movl $0x1, %eax
addq $0x1dc8, %rsp           # imm = 0x1DC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x180b(%rip), %rdi      # 0x301f
jmp 0x17f1
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x1830
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x60(%rsp), %rdi
callq 0x1852
jmp 0x183f
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x18f4
movq %rbx, %rdi
callq 0x1160
nop