Back
Close

Takuzu Solver (Easy mode)

Statement

 Goal

Takuzu (aka 'Binary Sudoku' or 'Binairo') is a variant of Sudoku that only allows the numbers 0 and 1. The following three rules must be met for a valid solution:
1. Each row and column must contain an equal number of 0s and 1s (e.g. 5 of each for a 10x10 grid).
2. No row or column may contain a sequence of three or more repeating digits (e.g. 1 1 0 is valid but 1 1 1 is invalid) (Hint: you can solve this 'Easy mode' puzzle with just this rule)
3. No rows (or columns) can be duplicates of other rows (or columns).

Given an integer n and an incomplete nxn binary grid of 0s and 1s, with . symbols for unknown values, complete the grid by replacing all the unknown values with 0s and 1s, obeying the required rules.

There will only be one valid solution.

Hard version: https://www.codingame.com/training/hard/takuzu-solver (Hint: you will need to include a backtracking algorithm and use all three rules)
Input
Line 1 : An integer n, the size of the grid (always even)
Next n lines : A row of n characters: 0, 1 or . (for unknown values)
Output
n lines : The completed board, with all . replaced with valid 0s and 1s, following the rules.
Constraints
4 < n < 16
n is even
All boards have a single valid solution
All boards can be solved using rule 2 alone (easy mode).
Example
Input
6
.0...1
0.11..
..1..0
.1...0
....1.
11.0.0
Output
100101
001101
011010
110100
001011
110010

Tags
LoopsSudoku2D array

Difficulty
Easy

Test cases
Test 1 Test
Input
6 .0...1 0.11.. ..1..0 .1...0 ....1. 11.0.0
Output
100101 001101 011010 110100 001011 110010

Validator 1 Validator
Input
6 0....1 0.0.11 ..00.. 1....0 ....0. .0.1..
Output
001101 010011 110010 101100 011001 100110

Test 2 Test
Input
8 .....00. .1...... 11.0..0. ..0....1 .1...0.. 0.0..0.. ....1..0 0.11.11.
Output
10011001 01100110 11001001 10010101 01101010 01010011 10101100 00110110

Validator 2 Validator
Input
8 .....1.1 .11.0..1 .1...... ..0.0.1. 1.0....0 ........ ..1..00. 0..11..0
Output
10010101 01100101 01101010 10010011 11001100 00110110 10101001 01011010

Test 3 Test
Input
10 .......1.. .00..0..1. .0..1..0.0 ..1...1... 1.1......1 .......1.. .0..1...0. ....11...0 .0.0..1..0 0...0...1.
Output
0110010101 1001100110 1001101010 0110011001 1010100101 0101010110 1001101001 0110110100 1010011010 0101001011

Validator 3 Validator
Input
10 .00.00...1 ....0.11.1 .11.....0. .11......0 ......0.0. ....1...01 1.00.0.... 11.....0.. ...1....1. .11...00.0
Output
1001001011 1001001101 0110110100 0110101010 1001010101 0010110101 1100101010 1101001001 0011010110 0110110010

Test 4 Test
Input
12 0.0.0..1...0 ....1...0... .0.......1.. 10.01.1...0. ..1.....0... ....1.0....0 .1....0...1. .....1...0.. ..1...0....1 1......1..0. .00..00.0... .1.1..1..1.0
Output
010101011010 011010100101 100101010110 101010101001 011001100101 100110011010 011010010110 100101101001 011001001011 101010110100 100110010011 010101101100

Validator 4 Validator
Input
12 ..0...0.0..1 0.00.0.11.1. .1.......0.0 ..1....0.... 0...0....00. .......11..1 11..1.....0. 1..0.....1.1 ..1...1.0... .0...11.0... 1..........0 ..0..1.00.1.
Output
100101010101 010010011011 011010101010 101101100100 010101011001 001010011011 110110100100 101001001101 011010110010 100101100101 101010011010 010101100110

Test 5 Test
Input
14 01.1.10.....1. 1.......1.1.10 ..11..1...1..0 1...1..0...... ...1.0..1....0 11....0....0.. ....0.....1..1 1.1...0.00...1 1...0..1....0. .1..0...0.0... ......1.0..1.. ..1....1.....1 00.0.0....1..1 .0..1...11..0.
Output
01010101010011 11001001101010 00110110101100 10101010010011 01011001101100 11010100110010 00100110011011 10101001001101 11010101100100 01100110010011 10011011001100 01100101100101 00101010011011 10011010110100

Validator 5 Validator
Input
14 .....11.1.1.10 .0.1...0..1..0 .00..1.....0.. .......1...... .11.1....1..0. .....00.0.0... 1..1..0.....1. .......1.1.0.0 ..1.0......... 00.1...1.00... 0....1.11.0..1 ....1.......0. .1.0.......0.. 1.00...00....1
Output
01100110101010 10011010101100 10010101010011 01100101100110 01101010011001 10011001010101 10110100100110 01001011011010 10110010011001 00110101100110 01001101100101 10011010011001 01100101101010 11001010010101

Solution language

Solution

Stub generator input