// File: /workspace/github_cpp_makefile_star2_O1/razimantv[P]DancingLinks/dominosa/dominosa.cpp
// Function: main
int main() {
  // Read the height of the board
  // In classic dominosa, the width and domino limit depend on H alone
  //  and need not be input separately
  int H, W, L;
  scanf("%d", &H);
  if (H < 1) {
    printf("Invalid board size\n");
    return 0;
  }
  W = H + 1;
  L = H;

  // Allocate memory for the board
  int **board;
  board = new (nothrow) int *[H];
  if (board == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  for (int i = 0; i < H; i++) {
    board[i] = new (nothrow) int[W];
    if (board[i] == NULL) {
      printf("Memory allocation error\n");
      return 1;
    }
  }

  // Read the board
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      scanf("%d", &board[i][j]);
      if ((board[i][j] < 0) || (board[i][j] >= L)) {
        printf("Invalid entry found in the board\n");
        return 1;
      }
    }

  // Every domino is a subset. Hence R=H*(W-1)+W*(H-1)
  // Every position being filled and every pair of numbers appearing in a domino
  //  are essential constraints. Hence C1=H*W+L*L
  // There are no non-essential constraints. Hence C2=0
  int horiz_dom_count = H * (W - 1), vert_dom_count = W * (H - 1);
  int pos_count = H * W, dom_type_count = L * L;
  int R = horiz_dom_count + vert_dom_count, C1 = pos_count + dom_type_count,
      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);

  // Deal with horizontal dominoes from (i,j) to (i,j+1)
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W - 1; j++) {
      // The domino corresponds to row# i*(W-1)+j of adjacency matrix
      int r = i * (W - 1) + j;

      // Position constraint for (i,j) is column# i*W+j of adjacency matrix
      int c1 = i * W + j, c2 = i * W + j + 1;

      // Domino constraint for (d,e) is column# H*W+d*L+e of adjacency matrix
      int c3 = pos_count + board[i][j] * L + board[i][j + 1];
      int c4 = pos_count + board[i][j + 1] * L + board[i][j];

      mat[r * (H * W + L * L) + c1] = 1;
      mat[r * (H * W + L * L) + c2] = 1;
      mat[r * (H * W + L * L) + c3] = 1;
      mat[r * (H * W + L * L) + c4] = 1;
    }
  }

  // Deal with vertical dominoes from (i,j) to (i+1,j)
  for (int i = 0; i < H - 1; i++) {
    for (int j = 0; j < W; j++) {
      // The domino corresponds to row# horiz_dom_count+i*(W-1)+j
      int r = horiz_dom_count + i * W + j;

      // Columns of constraint are same as earlier
      int c1 = i * W + j, c2 = (i + 1) * W + j;
      int c3 = H * W + board[i][j] * L + board[i + 1][j],
          c4 = H * W + board[i + 1][j] * L + board[i][j];

      mat[r * (H * W + L * L) + c1] = 1;
      mat[r * (H * W + L * L) + c2] = 1;
      mat[r * (H * W + L * L) + c3] = 1;
      mat[r * (H * W + L * L) + 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 a solution exists, print it
  if (all.size() == 1) {
    char *outputboard;
    outputboard = new char[H * W];
    if (outputboard == NULL) {
      printf("Memory allocation error\n");
      return 1;
    }
    for (int i = 0; i < all.size(); i++) {
      for (int j = 0; j < all[i].size(); j++) {
        int id = all[i][j];
        if (id < H * (W - 1)) {
          int r = id / (W - 1), c = id % (W - 1);
          outputboard[r * W + c] = outputboard[r * W + c + 1] = '-';
        } else {
          id -= H * (W - 1);
          int r = id / W, c = id % W;
          outputboard[r * W + c] = outputboard[(r + 1) * W + c] = '|';
        }
      }

      for (int i = 0, k = 0; i < H; i++) {
        for (int j = 0; j < W; j++, k++) printf("%c", outputboard[k]);
        printf("\n");
      }
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
leaq 0x1d50(%rip), %rdi      # 0x3004
xorl %r14d, %r14d
leaq 0xc(%rsp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10d0
movl (%rbx), %r13d
testl %r13d, %r13d
jle 0x134b
leaq (,%r13,8), %rdi
movq 0x3cf3(%rip), %rsi      # 0x4fd0
callq 0x1140
testq %rax, %rax
je 0x190a
movq %rax, %rbp
leaq 0x1(%r13), %rax
movq %rax, 0x20(%rsp)
leaq (,%rax,4), %r14
movq 0x3cca(%rip), %rsi      # 0x4fd0
movq %r14, %rdi
callq 0x1140
movq %rax, (%rbp)
testq %rax, %rax
je 0x135c
movl $0x1, %ebx
movq 0x3cad(%rip), %r15      # 0x4fd0
cmpq %rbx, %r13
je 0x1372
movq %r14, %rdi
movq %r15, %rsi
callq 0x1140
movq %rax, (%rbp,%rbx,8)
incq %rbx
testq %rax, %rax
jne 0x1323
decq %rbx
cmpq %r13, %rbx
setb %bl
jmp 0x135e
leaq 0x1cee(%rip), %rdi      # 0x3040
callq 0x1110
jmp 0x191c
movb $0x1, %bl
leaq 0x1cc3(%rip), %rdi      # 0x3028
callq 0x1110
movl $0x1, %r14d
jmp 0x1378
seta %bl
xorl %r14d, %r14d
testb %bl, %bl
jne 0x191c
movl %r14d, 0x18(%rsp)
cmpl $0x0, 0xc(%rsp)
setg 0x10(%rsp)
movq %rbp, 0x28(%rsp)
jle 0x1444
leal 0x1(%r13), %eax
movl $0x1, %r15d
subq %rax, %r15
leaq 0x1c56(%rip), %r14      # 0x3004
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %r12
xorl %ebp, %ebp
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x10d0
cmpl %r13d, (%r12)
jae 0x13fd
addq $0x4, %r12
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x1413
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x10d0
decq %rbp
leaq 0x4(%r12), %rax
cmpl %r13d, (%r12)
movq %rax, %r12
jb 0x13d0
notq %rbp
cmpq %r13, %rbp
setae %bpl
leaq 0x1c03(%rip), %rdi      # 0x3007
callq 0x1110
movl $0x1, 0x18(%rsp)
jmp 0x141d
negq %rbp
cmpq %r13, %rbp
setae %bpl
testb %bpl, %bpl
je 0x143f
incq %rbx
movslq 0xc(%rsp), %rax
cmpq %rax, %rbx
setl 0x10(%rsp)
movq 0x28(%rsp), %rbp
jl 0x13b0
jmp 0x1444
movq 0x28(%rsp), %rbp
testb $0x1, 0x10(%rsp)
movl 0x18(%rsp), %r14d
jne 0x191c
movl 0xc(%rsp), %ebx
movl %ebx, %ecx
imull %r13d, %ecx
leal -0x1(%rbx), %r14d
movl %r14d, 0x74(%rsp)
movq 0x20(%rsp), %rdx
imull %edx, %r14d
movl %ebx, %eax
imull %edx, %eax
movl %r13d, %edx
imull %r13d, %edx
movl %ecx, 0x78(%rsp)
addl %ecx, %r14d
movq %rax, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
leal (%rax,%rdx), %r12d
movl %r14d, %eax
imull %r12d, %eax
movslq %eax, %rdi
movq 0x3b28(%rip), %rsi      # 0x4fd0
movq %rdi, 0x18(%rsp)
callq 0x1140
testq %rax, %rax
je 0x190a
movq %rax, %r15
movl %r14d, 0x70(%rsp)
movq %r13, 0x10(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x1080
movl %r12d, 0x18(%rsp)
movl %r12d, %eax
movq %rax, 0x98(%rsp)
movl 0x20(%rsp), %eax
movq %rax, 0x68(%rsp)
testl %ebx, %ebx
jle 0x1610
movq 0x10(%rsp), %rax
cmpl $0x2, %eax
movl $0x1, %r8d
cmovgel %eax, %r8d
movabsq $0x100000000, %r13      # imm = 0x100000000
movq %rax, %rdx
movq 0x98(%rsp), %rcx
imulq %rcx, %rdx
addq 0x68(%rsp), %rdx
movq %rdx, 0x38(%rsp)
shlq $0x20, %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, %r10
shlq $0x20, %r10
addq %r13, %r10
incq %rcx
imull 0x18(%rsp), %eax
movl %eax, 0x7c(%rsp)
xorl %edx, %edx
movb $0x1, %r12b
movq 0xa0(%rsp), %rax
movq %r15, %rsi
movq %rbx, 0x40(%rsp)
movq %rdx, 0x58(%rsp)
movq (%rbp,%rdx,8), %r11
movl %eax, 0x50(%rsp)
movl %eax, %ebp
movq %rsi, 0x48(%rsp)
movq %rsi, %rdi
movq %r13, 0x60(%rsp)
xorl %esi, %esi
movq 0x10(%rsp), %rax
movl (%r11,%rsi,4), %ebx
movq %rcx, %rdx
movl 0x4(%r11,%rsi,4), %ecx
leal (%rbx,%rbp), %r14d
imull %eax, %ebx
movb %r12b, (%rdi)
movq %r8, %r9
movq %r13, %r8
sarq $0x20, %r8
movb %r12b, (%r15,%r8)
addl %ebp, %ebx
addl %ecx, %ebx
movslq %ebx, %r8
movb %r12b, (%r15,%r8)
movq %r9, %r8
imull %eax, %ecx
addl %r14d, %ecx
movslq %ecx, %rcx
movb %r12b, (%r15,%rcx)
movq %rdx, %rcx
incq %rsi
addq %r10, %r13
addq %rdx, %rdi
addl 0x18(%rsp), %ebp
cmpq %rsi, %r9
jne 0x1585
movq 0x58(%rsp), %rdx
incq %rdx
movq 0x60(%rsp), %r13
addq 0x30(%rsp), %r13
movq 0x48(%rsp), %rsi
addq 0x38(%rsp), %rsi
movl 0x50(%rsp), %eax
addl 0x7c(%rsp), %eax
movq 0x40(%rsp), %rbx
cmpq %rbx, %rdx
movq 0x28(%rsp), %rbp
jne 0x1561
cmpl $0x2, %ebx
movq 0x10(%rsp), %r13
movl 0x18(%rsp), %eax
movq 0x98(%rsp), %r11
movq 0x68(%rsp), %rsi
jl 0x1733
movl 0x78(%rsp), %edx
movl 0x74(%rsp), %r8d
movq %r8, 0x38(%rsp)
imulq %r11, %rdx
movq %r15, %r9
addq %rsi, %r9
incq %r11
movq %r11, %r8
imulq %rsi, %r8
movq %r8, 0x30(%rsp)
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %r8
addl %esi, %r8d
incl %r8d
imull %r13d, %r8d
incl %r8d
imull %ebx, %r8d
movl %eax, %esi
imull 0x20(%rsp), %esi
movl %esi, 0x40(%rsp)
xorl %edi, %edi
movb $0x1, %bl
movq %r15, %r12
movq (%rbp,%rdi,8), %rsi
movq %rdi, 0x50(%rsp)
movq 0x8(%rbp,%rdi,8), %r10
movl %r8d, 0x58(%rsp)
movl %r8d, %ebp
movq %r12, 0x48(%rsp)
movq %r9, 0x60(%rsp)
movq %r9, %rdi
xorl %r9d, %r9d
movl (%rsi,%r9,4), %ecx
movl %ecx, %r14d
imull %r13d, %r14d
movl (%r10,%r9,4), %r13d
addl %ebp, %r14d
addl %r13d, %r14d
movb %bl, (%r12,%rdx)
movb %bl, (%rdi,%rdx)
movslq %r14d, %r8
movb %bl, (%r15,%r8)
imull 0x10(%rsp), %r13d
addl %ebp, %ecx
addl %r13d, %ecx
movq 0x10(%rsp), %r13
movslq %ecx, %rcx
movb %bl, (%r15,%rcx)
incq %r9
addq %r11, %rdi
addq %r11, %r12
addl %eax, %ebp
cmpq %r9, 0x68(%rsp)
jne 0x16b0
movq 0x50(%rsp), %rdi
incq %rdi
movq 0x60(%rsp), %r9
movq 0x30(%rsp), %rsi
addq %rsi, %r9
movq 0x48(%rsp), %r12
addq %rsi, %r12
movl 0x58(%rsp), %r8d
addl 0x40(%rsp), %r8d
cmpq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rbp
jne 0x1689
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movl %eax, %ebp
callq 0x19c8
movq %rbx, %rdi
movl 0x70(%rsp), %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x1ac0
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0xb0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0xd0(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x1dfe
movq 0x20(%rsp), %r14
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
cmpq $0x18, %rax
jne 0x18ce
movslq 0xc(%rsp), %rax
movslq %r14d, %rdi
imulq %rax, %rdi
callq 0x1030
movq %rax, %rbx
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0x18ce
xorl %edx, %edx
movb $0x7c, %r12b
movq %rdx, 0x10(%rsp)
leaq (%rdx,%rdx,2), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x8(%rax,%rcx,8), %r8
subq %rdi, %r8
je 0x185f
sarq $0x2, %r8
movl 0xc(%rsp), %r9d
imull %r13d, %r9d
cmpq $0x1, %r8
adcq $0x0, %r8
xorl %r10d, %r10d
movl (%rdi,%r10,4), %ecx
movl %ecx, %eax
subl %r9d, %eax
jge 0x1837
movl %ecx, %eax
cltd
idivl %r13d
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %r13d
imull %r14d, %esi
addl %edx, %esi
movslq %esi, %rax
movw $0x2d2d, (%rbx,%rax)    # imm = 0x2D2D
jmp 0x1857
cltd
idivl %r14d
leal 0x1(%rax), %ecx
imull %r14d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movb %r12b, (%rbx,%rcx)
imull %r14d, %eax
addl %edx, %eax
cltq
movb %r12b, (%rbx,%rax)
incq %r10
cmpq %r10, %r8
jne 0x180d
cmpl $0x0, 0xc(%rsp)
jle 0x1898
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq %ebp, %rbp
movsbl (%rbx,%rbp), %edi
callq 0x10f0
incq %rbp
decl %r14d
jne 0x186e
movl $0xa, %edi
callq 0x10f0
incl %r15d
cmpl 0xc(%rsp), %r15d
movq 0x20(%rsp), %r14
jl 0x186b
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rcx
cmpq %rdx, %rcx
ja 0x17de
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x18eb
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10c0
leaq 0x80(%rsp), %rdi
callq 0x197a
leaq 0xd0(%rsp), %rdi
callq 0x1a14
xorl %r14d, %r14d
jmp 0x191c
leaq 0x1717(%rip), %rdi      # 0x3028
callq 0x1110
movl $0x1, %r14d
movl %r14d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1933
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x1953
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10c0
leaq 0x80(%rsp), %rdi
callq 0x197a
jmp 0x1965
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x1a14
movq %rbx, %rdi
callq 0x1150