// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
leaq 0x1d30(%rip), %rdi      # 0x3004
xorl %ebx, %ebx
leaq 0x14(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x10d0
movl (%r14), %r13d
testl %r13d, %r13d
jle 0x181c
leaq (,%r13,8), %rdi
movq 0x3cd0(%rip), %rsi      # 0x4fd0
callq 0x1140
testq %rax, %rax
je 0x1813
movq %rax, %rbx
leaq 0x1(%r13), %rbp
leaq (,%rbp,4), %r14
xorl %r12d, %r12d
movq 0x3ca9(%rip), %r15      # 0x4fd0
movq %r14, %rdi
movq %r15, %rsi
callq 0x1140
movq %rax, (%rbx,%r12,8)
testq %rax, %rax
je 0x1813
incq %r12
cmpq %r12, %r13
jne 0x1327
movq %rbp, 0x18(%rsp)
movl %ebp, %eax
leaq 0x1caf(%rip), %r14      # 0x3004
xorl %ebp, %ebp
movq %rax, (%rsp)
movq (%rbx,%rbp,8), %r15
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
cmpl %r13d, (%r15)
jae 0x182a
addq $0x4, %r15
decq %r12
jne 0x1362
movslq 0x14(%rsp), %r12
incq %rbp
cmpq %r12, %rbp
movq (%rsp), %rax
jl 0x135b
movl %r13d, %ecx
imull %r12d, %ecx
leal -0x1(%r12), %r14d
movl %r14d, 0x58(%rsp)
movq 0x18(%rsp), %rbp
imull %ebp, %r14d
movl %r12d, %eax
imull %ebp, %eax
movl %r13d, %edx
imull %edx, %edx
movl %ecx, 0x5c(%rsp)
addl %ecx, %r14d
movq %rax, 0x68(%rsp)
movq %rdx, 0x90(%rsp)
leal (%rax,%rdx), %ecx
movl %r14d, %eax
movl %ecx, (%rsp)
imull %ecx, %eax
movslq %eax, %rdi
movq 0x3bee(%rip), %rsi      # 0x4fd0
movq %rdi, 0x8(%rsp)
callq 0x1140
testq %rax, %rax
je 0x1813
movq %rax, %r15
movl %r14d, 0x60(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x1080
testl %r12d, %r12d
movl (%rsp), %r14d
jle 0x1650
movabsq $0x100000000, %rsi      # imm = 0x100000000
movslq %r14d, %rax
movslq %ebp, %rcx
movq %r13, %rdx
imulq %rax, %rdx
movq %rcx, 0x20(%rsp)
addq %rcx, %rdx
movq %rdx, 0x38(%rsp)
shlq $0x20, %rdx
movq %rdx, 0x30(%rsp)
movq %rax, %r8
shlq $0x20, %r8
addq %rsi, %r8
movq %rax, 0x28(%rsp)
leaq 0x1(%rax), %rdx
movl %r13d, %eax
imull %r14d, %eax
movl %eax, 0x64(%rsp)
xorl %edi, %edi
movb $0x1, %cl
movq 0x68(%rsp), %rax
movq %r15, %r9
movq %r12, 0x40(%rsp)
movq %rbx, 0x98(%rsp)
movq %rdi, 0x50(%rsp)
movq (%rbx,%rdi,8), %r10
movl %eax, 0x10(%rsp)
movl %eax, %r12d
movq %r9, 0x48(%rsp)
movq %r9, %rdi
movq %rsi, 0x8(%rsp)
movq %rsi, %r14
xorl %esi, %esi
movl (%rsp), %r9d
movl (%r10,%rsi,4), %ebp
movl 0x4(%r10,%rsi,4), %r11d
leal (%r12,%rbp), %eax
imull %r13d, %ebp
incq %rsi
movb %cl, (%rdi)
movq %rdx, %rbx
movq %r8, %rdx
movq %r14, %r8
sarq $0x20, %r8
movb %cl, (%r15,%r8)
addl %r12d, %ebp
addl %r11d, %ebp
imull %r13d, %r11d
movslq %ebp, %r8
movb %cl, (%r15,%r8)
movq %rdx, %r8
movq %rbx, %rdx
addl %eax, %r11d
movslq %r11d, %rax
movb %cl, (%r15,%rax)
addq %r8, %r14
addq %rbx, %rdi
addl %r9d, %r12d
cmpq %rsi, %r13
jne 0x14a3
movq 0x50(%rsp), %rdi
incq %rdi
movq 0x8(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x48(%rsp), %r9
addq 0x38(%rsp), %r9
movl 0x10(%rsp), %eax
addl 0x64(%rsp), %eax
movq 0x40(%rsp), %r12
cmpq %r12, %rdi
movq 0x98(%rsp), %rbx
jne 0x147d
cmpl $0x1, %r12d
movq 0x18(%rsp), %rbp
movl (%rsp), %r14d
je 0x1650
movl 0x5c(%rsp), %ecx
movl 0x58(%rsp), %eax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
imulq %rcx, %rax
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq 0x20(%rsp), %rax
addq %rax, %rdi
imulq %rdx, %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rax
movq 0x90(%rsp), %rcx
addl %eax, %ecx
incl %ecx
imull %r13d, %ecx
incl %ecx
imull %r12d, %ecx
movl %ebp, %eax
imull %r14d, %eax
movl %eax, 0x30(%rsp)
xorl %eax, %eax
movb $0x1, %r10b
movq %r15, %r11
movq 0x28(%rsp), %r12
movq (%rbx,%rax,8), %rsi
movq %rsi, 0x8(%rsp)
movq 0x8(%rbx,%rax,8), %rsi
incq %rax
movq %rax, 0x40(%rsp)
movl %ecx, 0x10(%rsp)
movl %ecx, %r9d
movq %r11, 0x48(%rsp)
movq %rdi, 0x50(%rsp)
xorl %r8d, %r8d
movq 0x8(%rsp), %rax
movl (%rax,%r8,4), %eax
movl %eax, %ebp
imull %r13d, %ebp
movl (%rsi,%r8,4), %r14d
addl %r9d, %ebp
addl %r14d, %ebp
movb %r10b, (%r11,%r12)
movb %r10b, (%rdi,%r12)
movslq %ebp, %rcx
movq 0x18(%rsp), %rbp
movb %r10b, (%r15,%rcx)
imull %r13d, %r14d
addl %r9d, %eax
addl %r14d, %eax
movl (%rsp), %r14d
cltq
movb %r10b, (%r15,%rax)
incq %r8
addq %rdx, %rdi
addq %rdx, %r11
addl %r14d, %r9d
cmpq %r8, %rbp
jne 0x15d1
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %rdi
addq %rax, %rdi
movq 0x48(%rsp), %r11
addq %rax, %r11
movl 0x10(%rsp), %ecx
addl 0x30(%rsp), %ecx
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x15a7
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0x18ec
movq %rbx, %rdi
movl 0x60(%rsp), %esi
movl %r14d, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x19e2
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0xa0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0xc0(%rsp), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x1d0c
movq 0x70(%rsp), %r12
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
subq %r12, %rax
cmpq $0x18, %rax
jne 0x17db
movl 0x14(%rsp), %r15d
movl %r15d, %eax
imull %ebp, %eax
movslq %eax, %rdi
callq 0x1030
cmpq %r12, (%rsp)
je 0x17db
movq %rax, %rbx
xorl %r11d, %r11d
movb $0x7c, %r14b
leaq (%r11,%r11,2), %rax
movq (%r12,%rax,8), %rdi
movq 0x8(%r12,%rax,8), %r8
subq %rdi, %r8
je 0x1760
sarq $0x2, %r8
movl %r15d, %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 0x173b
movl %ecx, %eax
cltd
idivl %r13d
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %r13d
imull %ebp, %esi
addl %edx, %esi
movslq %esi, %rax
movw $0x2d2d, (%rbx,%rax)    # imm = 0x2D2D
jmp 0x1758
cltd
idivl %ebp
leal 0x1(%rax), %ecx
imull %ebp, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movb %r14b, (%rbx,%rcx)
imull %ebp, %eax
addl %edx, %eax
cltq
movb %r14b, (%rbx,%rax)
incq %r10
cmpq %r10, %r8
jne 0x1712
testl %r15d, %r15d
jle 0x17b6
movq %r11, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq %r14d, %r14
movsbl (%rbx,%r14), %edi
callq 0x10f0
incq %r14
decl %ebp
jne 0x1773
movl $0xa, %edi
callq 0x10f0
incl %r12d
movl 0x14(%rsp), %r15d
cmpl %r15d, %r12d
movq 0x18(%rsp), %rbp
jl 0x1770
movq 0x70(%rsp), %r12
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %r11
movb $0x7c, %r14b
incq %r11
movq (%rsp), %rax
subq %r12, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq %r11, %rax
ja 0x16ea
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x17f8
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10c0
leaq 0x70(%rsp), %rdi
callq 0x1896
leaq 0xc0(%rsp), %rdi
callq 0x1938
xorl %ebx, %ebx
jmp 0x183b
leaq 0x180e(%rip), %rdi      # 0x3028
jmp 0x1831
leaq 0x181d(%rip), %rdi      # 0x3040
callq 0x1110
jmp 0x183b
leaq 0x17d6(%rip), %rdi      # 0x3007
callq 0x1110
movl $0x1, %ebx
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1851
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1871
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10c0
leaq 0x70(%rsp), %rdi
callq 0x1896
jmp 0x1880
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1938
movq %rbx, %rdi
callq 0x1150
nop