Brute Force Magic Squares

In principle, I have a 3 x 3 grid, which is filled with two digits 00 - 99. Some numbers are indicated as input, others are unknown. What are some suggestions on how to solve such a problem using brute force in C?

EDIT: Sorry, I forgot part of the problem. Each row, column and diagonal must contain the same number. I don't want any code to start with some algorithms using an algorithm

+3
source share
5 answers

There is a simple recursive solution to your problem, which is an example of a type of brute force called backtracking (google that).

(, fill_next) . , , , ( ), ; . , , 0 99, , , , .

: find_next , ; , fill_next (0). 0 8, 9 . 2D-, num% 3 num/3 .

, , , , .

+4

- () . , , . , , , , , .

+4

? , ? , ? , ? .

+1

3x3 2d-.

JS:

var a=[
    [ 11, 12, 13 ],
    [ 21, 22, 23 ],
    [ 31, 32, 33 ]
];
for(var r=0; r<a.length; r++)
    for(var c=0; c<a[r].length; c++)
        console.log(r+','+c+' = '+a[r]+','+a[r][c]);

a - 3x3 ( ) r - c -

, a.length a[r].length 3 ( ).

0

.

  • ( constant, j ++) (3 )
  • (keep j constant, ++) (3 )
  • (i ++, j ++, , j) (2 )

, , , . , .

0

Source: https://habr.com/ru/post/1790574/


All Articles