3

Basically I have a 3 x 3 grid that is filled with two digit numbers 00 - 99. Some of the numbers are given as input the rest are unknown. What are some suggestions on how to solve such a problem with brute force in C?

EDIT: Sorry I forgot part of the problem. Every row and column and diagonal must add up to the same number. I don't want any code just some ideas to get started with algorithm

false
  • 10,264
  • 13
  • 101
  • 209
foo
  • 2,241
  • 6
  • 21
  • 26

5 Answers5

4

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

The recursive function (say, fill_next) finds the next cell with unknown value. If there is no such cell, it checks the square to see if it fits the requirements (the sums are correct) and if so prints the square as a solution; it then returns. If there is a cell with an unknown value, it loops, trying each of the values 0 to 99 in turn for that cell then calling itself recursively to fill in the next unknown cell.

How to get to the next cell with unknown value: you can simply pass to find_next the number of the next cell to start looking at; you would start the whole thing off by calling fill_next(0). The cell number is 0 through 8 since you have 9 cells. If you are storing the square in a 2D array, just use num%3 and num/3 as the indices.

This would be much easier to describe by just giving you the few lines of code it takes, but you said you don't want that.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
4

Magic squares are really a system of (simple) simultaneous equations. You solve those by converting into a matrix and using Gaussian elimination, which is brute force but reasonably elegant at the same time. If the solution is not unique, you'll at least a reduced set of constraints on what the solution can be, which should make doing the solution much simpler.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • That's not "brute force" as the term is commonly (or even uncommonly) understood. It is certainly a more efficient and effective way to solve the problem, but it isn't what the OP asked for. – Jim Balter Feb 09 '11 at 02:39
  • @Jim: You don't want to apply "true" brute force to magic squares (i.e., scanning through all possibilities and seeing, one-by-one, if they are the solution) as the search space is 100^9 (== 10^18; many many CPU years with current technology) and so wholly impractically large. – Donal Fellows Feb 17 '11 at 00:55
  • To expand on "many CPU years", one modern CPU can't search one combination in 1ns. You need a cluster, possibly a large one, to get that rate of searching, and even then you're expected time to solution is about 16 years. (Upper bound – 1Gs – is just a bit under 32 years; expected search time is about half that, assuming the solution is unique.) – Donal Fellows Feb 17 '11 at 00:59
  • Please read the problem statement -- some of the values are already filled in. And what "you" want to do is irrelevant -- that's what the OP (or the OP's instructor) asked for. – Jim Balter Feb 17 '11 at 06:33
1

What is your problem? Are you trying to find out what each number is? is their any criteria that the numbers have to meet? If so, just guess each possible number in each ?? spot until a combination fits the criteria.

invisible bob
  • 864
  • 1
  • 9
  • 24
  • Sorry I forgot part of the problem. Every row and column and diagonal must add up to the same number. – foo Feb 07 '11 at 23:17
  • As I understand it, "just guess each possible number in each ??" IS the problem the OP is asking for a solution to. Checking whether the resulting square is a magic square is more self-evident. – Jim Balter Feb 08 '11 at 05:32
0

A 3x3 grid sounds like a 2d array.

Some example code in 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 - the 3x3 grid array (array of arrays) r - current row iteration c - current column iteration

Optionally, a.length and a[r].length can both be constants of 3 (in your case).

Christian
  • 27,509
  • 17
  • 111
  • 155
  • This prints out a grid, but that's not the OP's request. – Jim Balter Feb 08 '11 at 05:29
  • @Jim - That's "brute forcing" a grid, exactly what the OP wanted, with the difference that the OP didn't say what he wanted to do with it, so I opted to printing it out. – Christian Feb 08 '11 at 10:55
  • @Christian No, it's not exactly what the OP wanted; what the OP wanted is to know how to solve magic squares where some of the values are known and some are unknown. – Jim Balter Feb 08 '11 at 11:03
  • @Jim - If he OP was clear enough in his question, willytate wouldn't have asked so... – Christian Feb 08 '11 at 21:52
  • @Christian I didn't say he was clear, but you're the one using the word "exactly", and you definitely missed a few things. – Jim Balter Feb 09 '11 at 00:56
  • @Jim - That sounds awfully subjective. Too bad for me someone else hit the "understanding the OP" jackpot... – Christian Feb 09 '11 at 23:03
  • @Christian Yeah, too bad for you that someone else is better at understanding than you are. – Jim Balter Feb 10 '11 at 05:29
0

Brute force for magic square problem is pretty straightforward.

  1. Iterate over each row and compute sum for each row (keep i constant, j++) (3 sums)
  2. Iterate over each column and compute sum for each column(keep j constant, i++) (3 sums)
  3. Iterate over both diagonals and compute sum (i++, j++, such that i equals j) (2 sums)

If sum is not the same as the first sum you compute at any point, then you're done. If all sums are equal, then you've found the magic square.

Girish Rao
  • 2,609
  • 1
  • 20
  • 24
  • This determines whether a filled in square is a magic square, but the OP is asking how to fill in the square to find all the solutions. – Jim Balter Feb 08 '11 at 05:27
  • Oh yeah! Well, brute force would be to try every combination of numbers in the missing spots. If there were 3 missing cells, try 0-0-0, and the run the algorithm I describe above. Then try 0-0-1, and run the algorithm again, etc, etc until 99-99-99. This is assuming that the magic number is unknown. – Girish Rao Feb 08 '11 at 13:17
  • When you say "try 0-0-0 ... then try 0-0-1 etc. etc." you are just repeating the question, not answering it. See my answer above, which provides an algorithm for doing just that. – Jim Balter Feb 08 '11 at 13:20