// File: 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:
subq $0x1e48, %rsp           # imm = 0x1E48
movl $0x0, 0x1e44(%rsp)
movl $0x2d9, 0x1e40(%rsp) # imm = 0x2D9
movl $0x144, 0x1e3c(%rsp) # imm = 0x144
leaq 0x12d0(%rsp), %rdi
movl $0x1, %esi
movl $0xb64, %edx            # imm = 0xB64
callq 0x1090
leaq 0xdc0(%rsp), %rdi
movl $0x1, %esi
movl $0x510, %edx            # imm = 0x510
callq 0x1090
movl $0x0, 0xc6c(%rsp)
cmpl $0x9, 0xc6c(%rsp)
jge 0x164b
movl $0x0, 0xc68(%rsp)
cmpl $0x9, 0xc68(%rsp)
jge 0x1633
movslq 0xc6c(%rsp), %rax
leaq 0xc70(%rsp), %rsi
imulq $0x24, %rax, %rax
addq %rax, %rsi
movslq 0xc68(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rsi
leaq 0x3cef(%rip), %rdi      # 0x5004
movb $0x0, %al
callq 0x10e0
movslq 0xc6c(%rsp), %rcx
leaq 0xc70(%rsp), %rax
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movslq 0xc68(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x1366
movslq 0xc6c(%rsp), %rcx
leaq 0xc70(%rsp), %rax
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movslq 0xc68(%rsp), %rcx
cmpl $0x9, (%rax,%rcx,4)
jle 0x1384
leaq 0x3c9a(%rip), %rdi      # 0x5007
movb $0x0, %al
callq 0x1040
movl $0x1, 0x1e44(%rsp)
jmp 0x1cfc
movslq 0xc6c(%rsp), %rcx
leaq 0xc70(%rsp), %rax
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movslq 0xc68(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x161b
movl $0x0, 0xc64(%rsp)
cmpl $0x9, 0xc64(%rsp)
jge 0x151c
imull $0x51, 0xc6c(%rsp), %eax
imull $0x9, 0xc68(%rsp), %ecx
addl %ecx, %eax
addl 0xc64(%rsp), %eax
cltq
movl $0x0, 0x12d0(%rsp,%rax,4)
imull $0x51, 0xc6c(%rsp), %eax
imull $0x9, 0xc64(%rsp), %ecx
addl %ecx, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0x12d0(%rsp,%rax,4)
imull $0x51, 0xc64(%rsp), %eax
imull $0x9, 0xc68(%rsp), %ecx
addl %ecx, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0x12d0(%rsp,%rax,4)
movl 0xc6c(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
imull $0x3, %eax, %eax
movl %eax, 0x48(%rsp)
movl 0xc64(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x48(%rsp), %eax
addl %ecx, %eax
imull $0x51, %eax, %eax
movl %eax, 0x4c(%rsp)
movl 0xc68(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
imull $0x3, %eax, %ecx
movl 0xc64(%rsp), %eax
movl $0x3, %esi
cltd
idivl %esi
movl 0x4c(%rsp), %eax
addl %edx, %ecx
imull $0x9, %ecx, %ecx
addl %ecx, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0x12d0(%rsp,%rax,4)
movl 0xc64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc64(%rsp)
jmp 0x13b8
imull $0x9, 0xc6c(%rsp), %eax
addl 0xc68(%rsp), %eax
cltq
movl $0x0, 0xdc0(%rsp,%rax,4)
imull $0x9, 0xc6c(%rsp), %eax
addl $0x51, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0xdc0(%rsp,%rax,4)
imull $0x9, 0xc68(%rsp), %eax
addl $0xa2, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0xdc0(%rsp,%rax,4)
movl 0xc6c(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
imull $0x1b, %eax, %eax
addl $0xf3, %eax
movl %eax, 0x44(%rsp)
movl 0xc68(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x44(%rsp), %eax
imull $0x9, %ecx, %ecx
addl %ecx, %eax
movslq 0xc6c(%rsp), %rdx
leaq 0xc70(%rsp), %rcx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movslq 0xc68(%rsp), %rdx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
cltq
movl $0x0, 0xdc0(%rsp,%rax,4)
jmp 0x161d
movl 0xc68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc68(%rsp)
jmp 0x12da
jmp 0x1635
movl 0xc6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc6c(%rsp)
jmp 0x12c1
movl $0x0, 0xfc(%rsp)
movl $0x0, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
cmpl $0x2d9, 0xf4(%rsp) # imm = 0x2D9
jge 0x16f5
movslq 0xf4(%rsp), %rax
cmpl $0x0, 0x12d0(%rsp,%rax,4)
je 0x16ca
movl 0xfc(%rsp), %ecx
movslq 0xf4(%rsp), %rax
movl %ecx, 0x12d0(%rsp,%rax,4)
movl 0xf4(%rsp), %ecx
movslq 0xfc(%rsp), %rax
movl %ecx, 0x100(%rsp,%rax,4)
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x16dd
movslq 0xf4(%rsp), %rax
movl $0xffffffff, 0x12d0(%rsp,%rax,4) # imm = 0xFFFFFFFF
jmp 0x16df
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x166c
movl $0x0, 0xf0(%rsp)
cmpl $0x144, 0xf0(%rsp) # imm = 0x144
jge 0x176b
movslq 0xf0(%rsp), %rax
cmpl $0x0, 0xdc0(%rsp,%rax,4)
je 0x1743
movl 0xf8(%rsp), %ecx
movl %ecx, %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
movslq 0xf0(%rsp), %rax
movl %ecx, 0xdc0(%rsp,%rax,4)
jmp 0x1756
movslq 0xf0(%rsp), %rax
movl $0xffffffff, 0xdc0(%rsp,%rax,4) # imm = 0xFFFFFFFF
jmp 0x1758
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x1700
movl 0xfc(%rsp), %eax
movl %eax, 0xec(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movl 0xe8(%rsp), %eax
addl 0xe4(%rsp), %eax
movl %eax, 0xe0(%rsp)
movl 0xec(%rsp), %eax
imull 0xe0(%rsp), %eax
movslq %eax, %rdi
movq 0x6810(%rip), %rsi      # 0x7fd0
callq 0x1130
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x17f6
leaq 0x383d(%rip), %rdi      # 0x501c
movb $0x0, %al
callq 0x1040
movl $0x1, 0x1e44(%rsp)
jmp 0x1cfc
movq 0xd8(%rsp), %rdi
movl 0xec(%rsp), %eax
imull 0xe0(%rsp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1090
movl $0x0, 0xd4(%rsp)
cmpl $0x9, 0xd4(%rsp)
jge 0x1a8a
movl $0x0, 0xd0(%rsp)
cmpl $0x9, 0xd0(%rsp)
jge 0x1a72
movl $0x0, 0xcc(%rsp)
cmpl $0x9, 0xcc(%rsp)
jge 0x1a5a
imull $0x51, 0xd4(%rsp), %eax
imull $0x9, 0xd0(%rsp), %ecx
addl %ecx, %eax
addl 0xcc(%rsp), %eax
movl %eax, 0xc8(%rsp)
movslq 0xc8(%rsp), %rax
cmpl $-0x1, 0x12d0(%rsp,%rax,4)
jne 0x1899
jmp 0x1a44
imull $0x9, 0xd4(%rsp), %eax
addl 0xd0(%rsp), %eax
movl %eax, 0xc4(%rsp)
imull $0x9, 0xd4(%rsp), %eax
addl $0x51, %eax
addl 0xcc(%rsp), %eax
movl %eax, 0xc0(%rsp)
imull $0x9, 0xd0(%rsp), %eax
addl $0xa2, %eax
addl 0xcc(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl 0xd4(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
imull $0x1b, %eax, %eax
addl $0xf3, %eax
movl %eax, 0x40(%rsp)
movl 0xd0(%rsp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x40(%rsp), %eax
imull $0x9, %ecx, %ecx
addl %ecx, %eax
addl 0xcc(%rsp), %eax
movl %eax, 0xb8(%rsp)
movslq 0xc4(%rsp), %rax
cmpl $-0x1, 0xdc0(%rsp,%rax,4)
je 0x196d
movq 0xd8(%rsp), %rax
movslq 0xc8(%rsp), %rcx
movl 0x12d0(%rsp,%rcx,4), %ecx
imull 0xe0(%rsp), %ecx
movslq 0xc4(%rsp), %rdx
addl 0xdc0(%rsp,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movslq 0xc0(%rsp), %rax
cmpl $-0x1, 0xdc0(%rsp,%rax,4)
je 0x19b4
movq 0xd8(%rsp), %rax
movslq 0xc8(%rsp), %rcx
movl 0x12d0(%rsp,%rcx,4), %ecx
imull 0xe0(%rsp), %ecx
movslq 0xc0(%rsp), %rdx
addl 0xdc0(%rsp,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movslq 0xbc(%rsp), %rax
cmpl $-0x1, 0xdc0(%rsp,%rax,4)
je 0x19fb
movq 0xd8(%rsp), %rax
movslq 0xc8(%rsp), %rcx
movl 0x12d0(%rsp,%rcx,4), %ecx
imull 0xe0(%rsp), %ecx
movslq 0xbc(%rsp), %rdx
addl 0xdc0(%rsp,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movslq 0xb8(%rsp), %rax
cmpl $-0x1, 0xdc0(%rsp,%rax,4)
je 0x1a42
movq 0xd8(%rsp), %rax
movslq 0xc8(%rsp), %rcx
movl 0x12d0(%rsp,%rcx,4), %ecx
imull 0xe0(%rsp), %ecx
movslq 0xb8(%rsp), %rdx
addl 0xdc0(%rsp,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
jmp 0x1a44
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x1854
jmp 0x1a5c
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x183b
jmp 0x1a74
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x1822
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x2320
movq 0x38(%rsp), %rdi
movl 0xec(%rsp), %esi
movl 0xe8(%rsp), %edx
movl 0xe4(%rsp), %ecx
movq 0xd8(%rsp), %r8
callq 0x2510
jmp 0x1ac5
leaq 0x78(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1d20
leaq 0x60(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x1d30
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xa0(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x2e00
jmp 0x1b04
leaq 0x78(%rsp), %rdi
callq 0x1d40
cmpq $0x1, %rax
jne 0x1ca2
movl $0x0, 0x5c(%rsp)
movslq 0x5c(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1d60
movq %rax, %rdi
callq 0x1d80
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x1c2b
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1d60
movq %rax, %rdi
movslq 0x5c(%rsp), %rsi
callq 0x1da0
movslq (%rax), %rax
movl 0x100(%rsp,%rax,4), %eax
movl %eax, 0x58(%rsp)
movl 0x58(%rsp), %eax
movl $0x9, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movl %edx, 0x1c(%rsp)
movl 0x58(%rsp), %eax
movl $0x51, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0xc70(%rsp), %rax
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0x58(%rsp), %eax
movl $0x9, %ecx
cltd
idivl %ecx
movl $0x9, %ecx
cltd
idivl %ecx
movq 0x10(%rsp), %rax
movl %edx, %ecx
movl 0x1c(%rsp), %edx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1b20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x1ced
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1dc0
leaq 0x78(%rsp), %rdi
callq 0x1e20
jmp 0x1ced
movl $0x0, 0x54(%rsp)
cmpl $0x9, 0x54(%rsp)
jge 0x1ca0
movl $0x0, 0x50(%rsp)
cmpl $0x9, 0x50(%rsp)
jge 0x1c81
movslq 0x54(%rsp), %rax
leaq (%rax,%rax,8), %rax
leaq 0xc70(%rsp,%rax,4), %rax
movslq 0x50(%rsp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x33cc(%rip), %rdi      # 0x5035
xorl %eax, %eax
callq 0x1040
jmp 0x1c72
jmp 0x1c74
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x1c42
leaq 0x3392(%rip), %rdi      # 0x501a
xorl %eax, %eax
callq 0x1040
jmp 0x1c91
jmp 0x1c93
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x1c33
jmp 0x1ca2
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x1cbf
movq 0x8(%rsp), %rdi
callq 0x10f0
movl $0x0, 0x1e44(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1dc0
leaq 0x78(%rsp), %rdi
callq 0x1e20
leaq 0xa0(%rsp), %rdi
callq 0x23b0
jmp 0x1cfc
leaq 0xa0(%rsp), %rdi
callq 0x23b0
jmp 0x1d0b
movl 0x1e44(%rsp), %eax
addq $0x1e48, %rsp           # imm = 0x1E48
retq
movq 0x98(%rsp), %rdi
callq 0x1140
nopl (%rax,%rax)