Question

I need help fixing a very simple brute force attack on a set of numbers 1=9. My goal is to solve a sudoko board where a row is filled with 8 numbers so that there can only be one number left on the row. The only way that I thought of to find this was to assign a number to the missing cell and then check it against every other number on the row. I'm really close to the solution but the return is always a 1. Here's the snippet of code that I'm having problems with: I updated the code so that it now adds all the numbers and subtracts them from 45 to find the right number. It still doesn't return the right number. It returns 423 (With 6 as the missing number).

int radd1=deduct[0][0]+deduct[0][1]+deduct[0][2]+deduct[0][3]+deduct[0][4]+deduct[0] int test=0;
if (radd1==8) {
    for (int control=0; control<9; control++) {
        if (dash[0][control]=='_') {
            empty=control;
        }
    }
    for (int control2=0; control2<9; control2++) {
        if (control2!=empty) {
            test=test+dash[0][control2];
        }
    }
    cout << test << endl;
}

Some more info:
The entire solve is based off of one 9x9 char filled with numbers to go in their appropriate spots. This char is called dash[9][9].
The deduct[9][9] char is a duplicate of dash[9][9] except that instead of the actual number that goes there, there is a 1. This is so I can add up a whole row and if it is equal to 8 perform my simple brute force (if that's the right term) to find the final ninth number. empty is an int. It stores (in this case) the column number that has no number.
Spot any problems?

Was it helpful?

Solution

  1. It's a mistake to be using deduct[0][9] in the addition of radd1, right?
  2. The comparison with dash[0][control3] is inside a cycle so that the value of test is always dependant in the last iteration of the cycle. (In this case control2+1 vs dash[0][8]. I suppose you'd have to break the cycle if there is an equality.
  3. You assign control2+'1' to dash[0][empty] but later use control+1. (Not the same thing)
  4. The whole assignment and comparison inside the condition if (test==1) doesn't feel right but we can't be sure if it's correct just from the included code.

There is a better way of finding the number that has not been used. We know the sum of 1+2+..+9 = 45, so you'd only have to sum the appearing numbers and substract from 45 to obtain the number that's missing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top