Back
Close

Neighbor-Sum Grids

Statement

 Goal

A (2-)neighbor-sum grid is a 5×5 matrix containing each number from 1 to 25 exactly once and where each value that is at least 3 can be obtained as the sum of two distinct values among its direct neighbors (horizontally, vertically and diagonally, so that an inner cell has 8 neighbors, a border cell has 5 neighbors and a corner cell has 3 neighbors).

Example: (21 = 4+17, 14 = 4+10, 10 = 4+6, 16 = 6+10, 22 = 6+16, etc for every value >2 of the grid)
21 14 10 16 22
17 4 1 6 19
12 3 5 11 13
15 8 2 7 18
23 24 9 20 25

It can be proven that there are 56816 such grids (or 7102 up to the 8 symmetries of the square).

In this problem, you are given a partially completed grid (in which unknown values are indicated by a 0 in the input) and you are asked to complete it. It is guaranteed for each given testcase that there exists a unique solution.
Input
5 lines of 5 space-separated numbers between 0 and 25, 0 indicating an unknown value.
Output
5 lines of 5 space-separated numbers between 1 and 25 corresponding to the unique complete solution.
Constraints
Example
Input
21 14 10 0 0
0 4 1 6 0
0 3 5 11 13
15 0 0 0 0
23 24 0 20 25
Output
21 14 10 16 22
17 4 1 6 19
12 3 5 11 13
15 8 2 7 18
23 24 9 20 25

Tags
Backtracking

Difficulty
Hard

Test cases
10 missing Test
Input
21 14 10 0 0 0 4 1 6 0 0 3 5 11 13 15 0 0 0 0 23 24 0 20 25
Output
21 14 10 16 22 17 4 1 6 19 12 3 5 11 13 15 8 2 7 18 23 24 9 20 25

Validator 1 Validator
Input
0 0 0 20 21 0 6 5 4 0 23 7 1 3 0 0 9 8 2 0 25 24 0 0 22
Output
19 11 15 20 21 13 6 5 4 17 23 7 1 3 14 16 9 8 2 12 25 24 18 10 22

13 missing Test
Input
19 15 11 0 23 0 0 0 0 13 0 0 1 0 22 0 0 0 0 16 14 12 21 0 25
Output
19 15 11 17 23 18 4 5 6 13 20 3 1 7 22 2 10 8 9 16 14 12 21 24 25

Validator 2 Validator
Input
21 13 0 0 23 10 0 0 0 0 12 2 3 6 15 0 0 4 0 0 0 0 11 0 17
Output
21 13 22 14 23 10 8 5 9 24 12 2 3 6 15 19 7 4 1 16 25 18 11 20 17

16 missing Test
Input
23 22 8 0 18 0 0 0 5 0 0 0 12 0 0 0 10 0 11 9 0 0 0 0 0
Output
23 22 8 13 18 19 1 3 5 25 15 4 12 2 7 14 10 6 11 9 24 16 21 17 20

Validator 3 Validator
Input
0 11 8 0 0 0 0 3 0 0 18 0 23 2 0 0 0 0 0 0 24 0 21 15 0
Output
20 11 8 25 17 19 1 3 5 12 18 4 23 2 7 14 10 6 13 9 24 16 21 15 22

19 missing Test
Input
0 12 0 0 0 0 0 17 0 7 0 4 3 0 18 0 0 0 0 0 0 0 0 0 0
Output
23 12 13 20 25 11 1 17 5 7 10 4 3 2 18 19 9 6 8 16 24 15 21 14 22

Validator 4 Validator
Input
0 0 0 16 0 10 0 0 0 0 0 0 2 0 0 21 0 0 0 24 0 0 0 11 0
Output
25 15 13 16 23 10 6 9 7 12 22 4 2 5 20 21 1 3 8 24 18 17 14 11 19

22 missing Test
Input
0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 5 0 0 0 0 13 0
Output
24 16 21 15 20 14 10 6 11 9 18 4 17 2 7 19 1 3 5 12 23 22 8 13 25

Validator 5 Validator
Input
0 0 0 0 0 9 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 11 0
Output
25 15 21 20 24 9 16 6 10 14 7 2 19 4 18 12 5 3 1 22 17 13 8 11 23

22 missing Test
Input
0 0 8 13 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0
Output
23 22 8 13 25 19 1 3 5 12 18 4 15 2 7 14 10 6 11 9 24 16 21 17 20

Validator 6 Validator
Input
0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 19 0 0 0 5 0 0 0 0 0
Output
22 14 20 16 25 24 8 6 10 15 17 2 7 4 11 19 9 3 1 5 21 12 13 18 23

23 missing Test
Input
0 0 0 0 0 0 0 0 0 0 0 0 18 0 0 23 0 0 0 0 0 0 0 0 0
Output
19 12 13 16 17 7 5 3 1 20 9 2 18 4 11 23 8 6 10 15 22 14 24 21 25

Validator 7 Validator
Input
0 0 0 7 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0
Output
22 23 9 7 19 14 8 2 5 12 24 6 18 3 13 21 10 4 1 16 25 15 11 20 17

23 missing Test
Input
21 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Output
21 11 1 13 18 10 4 3 5 12 20 6 2 7 19 14 8 15 9 16 22 23 17 24 25

Validator 8 Validator
Input
18 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Output
18 13 1 11 21 12 5 3 4 10 19 7 2 6 20 16 9 15 8 14 25 24 17 23 22

Solution language

Solution

Stub generator input