// File: /workspace/github_cpp_makefile_star2_O2/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 0x1d80(%rip), %rdi      # 0x3004
xorl %r13d, %r13d
leaq 0x1c(%rsp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10d0
movl (%rbx), %ebx
testl %ebx, %ebx
jle 0x1302
leaq (,%rbx,8), %rdi
movq 0x3d25(%rip), %rsi      # 0x4fd0
callq 0x1140
pushq $0x1
popq %r13
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x12f9
leaq 0x1(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq (,%rax,4), %r14
xorl %ebp, %ebp
movq 0x3cf8(%rip), %r15      # 0x4fd0
cmpq %rbp, %rbx
je 0x130b
movq %r14, %rdi
movq %r15, %rsi
callq 0x1140
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
testq %rax, %rax
jne 0x12d8
leaq 0x1d28(%rip), %rdi      # 0x3028
jmp 0x135d
leaq 0x1d37(%rip), %rdi      # 0x3040
jmp 0x135d
leaq 0x1cf2(%rip), %r14      # 0x3004
movl %ebx, %ecx
xorl %ebp, %ebp
movslq %ecx, %rax
cmpq %rax, %rbp
jge 0x1377
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %r15
movq 0x8(%rsp), %r12
subq $0x1, %r12
jb 0x134d
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
leaq 0x4(%r15), %rax
cmpl %ebx, (%r15)
movq %rax, %r15
jb 0x132c
jmp 0x1356
incq %rbp
movl 0x1c(%rsp), %ecx
jmp 0x1316
leaq 0x1caa(%rip), %rdi      # 0x3007
callq 0x1110
movl %r13d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %edx
imull %ebx, %edx
leal -0x1(%rcx), %eax
movq %rax, 0x48(%rsp)
movq 0x8(%rsp), %rsi
imull %esi, %eax
movl %ecx, %ebp
imull %esi, %ebp
movl %ebx, %r12d
imull %r12d, %r12d
movl %edx, 0x60(%rsp)
addl %edx, %eax
leal (%r12,%rbp), %r14d
movl %eax, 0x64(%rsp)
imull %r14d, %eax
movslq %eax, %rdi
movq 0x3c1c(%rip), %rsi      # 0x4fd0
movq %rdi, 0x10(%rsp)
movq %rcx, %r13
callq 0x1140
testq %rax, %rax
je 0x17c2
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1080
xorl %esi, %esi
movslq %r14d, %rcx
movslq 0x8(%rsp), %rax
testl %r13d, %r13d
movl $0x0, %edx
movq %r13, 0x28(%rsp)
cmovgl %r13d, %edx
movq %rdx, 0x38(%rsp)
leal (%r12,%rbp), %edi
incl %edi
movl %ebx, %edx
imull %edi, %edx
incl %edx
movl %edx, 0x6c(%rsp)
movq %rbx, %rdx
imulq %rcx, %rdx
movq %rax, 0x40(%rsp)
addq %rax, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
incq %rcx
movl %ebx, %eax
imull %r14d, %eax
movl %eax, 0x68(%rsp)
pushq $0x1
popq %r9
movb $0x1, %dl
movl %ebp, %r10d
movq %r15, %r11
movq %rbp, 0xa0(%rsp)
movq %r12, 0x98(%rsp)
cmpq 0x38(%rsp), %rsi
je 0x150a
movq 0x20(%rsp), %rax
movq %rsi, 0x10(%rsp)
movq (%rax,%rsi,8), %r8
movl %r10d, 0x58(%rsp)
movq %r11, 0x50(%rsp)
movq %r11, %rax
movl %r9d, 0x30(%rsp)
movl %r9d, %r11d
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x14cc
movl (%r8,%rsi,4), %r13d
movl 0x4(%r8,%rsi,4), %ebp
leal (%r10,%r13), %r9d
imull %ebx, %r13d
incq %rsi
movb %dl, (%rax)
movslq %r11d, %r11
movb %dl, (%r15,%r11)
addl %r10d, %r13d
addl %ebp, %r13d
imull %ebx, %ebp
movslq %r13d, %r12
movb %dl, (%r15,%r12)
addl %r9d, %ebp
movslq %ebp, %r9
movb %dl, (%r15,%r9)
addl %edi, %r11d
addq %rcx, %rax
addl %r14d, %r10d
jmp 0x1485
movq 0x10(%rsp), %rsi
incq %rsi
movl 0x30(%rsp), %r9d
addl 0x6c(%rsp), %r9d
movq 0x50(%rsp), %r11
addq 0x90(%rsp), %r11
movl 0x58(%rsp), %r10d
addl 0x68(%rsp), %r10d
movq 0xa0(%rsp), %rbp
movq 0x98(%rsp), %r12
jmp 0x1455
movslq 0x60(%rsp), %rsi
xorl %eax, %eax
movq 0x48(%rsp), %rdx
testl %edx, %edx
cmovlel %eax, %edx
movq %rdx, 0x48(%rsp)
imulq 0x88(%rsp), %rsi
movq %r15, %rdi
movq 0x40(%rsp), %rdx
addq %rdx, %rdi
movq %rdi, 0x30(%rsp)
imulq %rcx, %rdx
movq %rdx, 0x40(%rsp)
leal (%r12,%rbp), %edx
incl %edx
imull %ebx, %edx
incl %edx
movq 0x28(%rsp), %rdi
imull %edx, %edi
movq %rdi, 0x28(%rsp)
movq 0x8(%rsp), %rdx
imull %r14d, %edx
movl %edx, 0x38(%rsp)
movb $0x1, %r8b
movq %r15, %r9
movq %rbx, 0x10(%rsp)
cmpq 0x48(%rsp), %rax
je 0x161c
leaq 0x1(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq 0x20(%rsp), %rdx
movq (%rdx,%rax,8), %r10
movq 0x8(%rdx,%rax,8), %r11
movq 0x28(%rsp), %r12
movq %r9, 0x58(%rsp)
movq 0x30(%rsp), %rdx
xorl %eax, %eax
cmpq %rax, 0x8(%rsp)
je 0x15f2
movl (%r10,%rax,4), %ebp
movl %ebp, %r13d
imull %ebx, %r13d
movl (%r11,%rax,4), %edi
addl %r12d, %r13d
addl %edi, %r13d
movb %r8b, (%r9,%rsi)
movb %r8b, (%rdx,%rsi)
movslq %r13d, %rbx
movb %r8b, (%r15,%rbx)
movq 0x10(%rsp), %rbx
imull %ebx, %edi
addl %r12d, %ebp
addl %edi, %ebp
movslq %ebp, %rdi
movb %r8b, (%r15,%rdi)
incq %rax
addq %rcx, %rdx
addq %rcx, %r9
addl %r14d, %r12d
jmp 0x15a5
movq 0x40(%rsp), %rax
addq %rax, 0x30(%rsp)
movq 0x58(%rsp), %r9
addq %rax, %r9
movq 0x28(%rsp), %rax
addl 0x38(%rsp), %eax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
jmp 0x1572
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
callq 0x18d0
movq %rbx, %rdi
movl 0x64(%rsp), %esi
movl %r14d, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x19ba
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xc0(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0xa8(%rsp), %rdi
pushq $-0x1
popq %r8
xorl %ecx, %ecx
callq 0x1ccc
movq 0x8(%rsp), %r15
movq 0x70(%rsp), %r12
movq 0x78(%rsp), %r13
movq %r13, %rax
subq %r12, %rax
cmpq $0x18, %rax
jne 0x1796
movl 0x1c(%rsp), %r14d
movl %r14d, %eax
imull %r15d, %eax
movslq %eax, %rdi
callq 0x1030
movq %rax, %rbx
xorl %ecx, %ecx
movb $0x7c, %r10b
subq %r12, %r13
movq %r13, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rcx, %rax
jbe 0x1796
movq %rcx, 0x20(%rsp)
imulq $0x18, %rcx, %rax
movq (%r12,%rax), %rsi
movq 0x8(%r12,%rax), %rdi
subq %rsi, %rdi
sarq $0x2, %rdi
movl %r14d, %r8d
imull 0x10(%rsp), %r8d
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0x1736
movl (%rsi,%r9,4), %eax
movl %eax, %ecx
subl %r8d, %ecx
jge 0x170f
cltd
idivl 0x10(%rsp)
imull %r15d, %eax
addl %edx, %eax
cltq
movw $0x2d2d, (%rbx,%rax)    # imm = 0x2D2D
jmp 0x1731
movl %ecx, %eax
cltd
idivl %r15d
leal 0x1(%rax), %ecx
imull %r15d, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
movb %r10b, (%rbx,%rcx)
imull %r15d, %eax
addl %edx, %eax
cltq
movb %r10b, (%rbx,%rax)
incq %r9
jmp 0x16ea
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpl %r14d, %r12d
jge 0x177a
movslq %r15d, %r14
movslq %ebp, %r13
movq 0x8(%rsp), %rax
leal (%rax,%r13), %ebp
cmpq %r13, %r14
je 0x1763
movsbl (%rbx,%r13), %edi
callq 0x10f0
incq %r13
jmp 0x174f
pushq $0xa
popq %rdi
callq 0x10f0
incl %r12d
movl 0x1c(%rsp), %r14d
addl 0x8(%rsp), %r15d
jmp 0x173b
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x70(%rsp), %r12
movq 0x78(%rsp), %r13
movq 0x8(%rsp), %r15
jmp 0x16ab
leaq 0xc0(%rsp), %rdi
callq 0x189c
leaq 0x70(%rsp), %rdi
callq 0x180e
leaq 0xa8(%rsp), %rdi
callq 0x1918
xorl %r13d, %r13d
jmp 0x1362
leaq 0x185f(%rip), %rdi      # 0x3028
callq 0x1110
pushq $0x1
popq %r13
jmp 0x1362
jmp 0x17d9
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x189c
leaq 0x70(%rsp), %rdi
callq 0x180e
jmp 0x17f8
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x1918
movq %rbx, %rdi
callq 0x1150
nop