#K58957. Mini Sudoku Solver

    ID: 30757 Type: Default 1000ms 256MiB

Mini Sudoku Solver

Mini Sudoku Solver

You are given a 4x4 Mini Sudoku puzzle. In this puzzle, the grid is partially filled with integers from 1 to 4 while empty cells are represented by 0. The goal is to decide whether there exists at least one valid solution that completes the grid without violating the following conditions:

  • Each row must contain the numbers 1 to 4 without repetition.
  • Each column must contain the numbers 1 to 4 without repetition.
  • Each 2x2 sub-grid must contain the numbers 1 to 4 without repetition.

In mathematical terms, if we denote the grid by \(G\) where \(G[i][j]\) represents the value in the \(i^{th}\) row and \(j^{th}\) column (with \(0 \le i,j < 4\)), for each row \(i\) and column \(j\), it must hold that:

\(\{ G[i][0], G[i][1], G[i][2], G[i][3] \} = \{1, 2, 3, 4\}\) and \(\{ G[0][j], G[1][j], G[2][j], G[3][j] \} = \{1, 2, 3, 4\}\). Also for each sub-grid defined by \(start\_row = 2\lfloor i/2 \rfloor\) and \(start\_col = 2\lfloor j/2 \rfloor\), the set of numbers must equal \(\{1, 2, 3, 4\}\).

Output YES if there is at least one valid way to fill the grid; otherwise, output NO.

inputFormat

The input consists of exactly 4 lines. Each line contains 4 space-separated integers representing a row of the Mini Sudoku grid. The number 0 represents an empty cell. It is guaranteed that the grid will have exactly 4 rows and 4 columns.

outputFormat

Output a single line containing either YES if the puzzle can be solved validly, or NO otherwise.

## sample
0 0 3 4
3 4 0 0
0 3 4 0
4 0 0 3
YES