-3

I have found a function on internet here for solving my problem on Sudoku written in C language.I have almost everything done but I am stuck here, I can't check the 3x3 box if any value is duplicated:

/**
 * Check if a value contains in its 3x3 box for a cell.
 * @param row current row index.
 * @param col current column index.
 * @return true if this cell is incorrect or duplicated in its 3x3 box.
 */
private boolean containedIn3x3Box(int row, int col, int value) {
    // Find the top left of its 3x3 box to start validating from
    int startRow = row / 3 * 3;
    int startCol = col / 3 * 3;

    // Check within its 3x3 box except its cell
    for (int i = startRow; i < startRow + 3; i++)
        for (int j = startCol; j < startCol + 3; j++) {
            if (!(i == row && j == col)) {
                if (cells[i][j] == value){
                    return true;
                }
            }
        }

    return false;
}

Up here is the function and I put it in my program here:

int valid(int k, int ii, int jj)
{
    int i,start,final,j;
    start=ii/3*3;
    final=jj/3*3;
    for(i = 1; i <= 9; ++i) {
        if (i != ii && v[i][jj] == k)
            return 0;
        if (i != jj && v[ii][i] == k)
            return 0;
    }
    for(i=start;i<=start+3;i++)
        for(j=final;j<=final+3;j++)
        {
            if(!(i==ii && j==jj))
            {
                if(v[i][j]==k)
                return 0;
            }
        }
    return 1;
}

I have read on the website where I took this function and tried to understand the program. The program compares values with value part. My loop is not working well.

SaurabhJinturkar
  • 554
  • 7
  • 20
Marian Pavel
  • 31
  • 2
  • 6
  • Why you are using two loops in valid function? I think `if (i != jj && v[ii][i] == k)` here you are comparing value instead of row. – SaurabhJinturkar Jan 08 '14 at 11:05
  • Please make your question explicit. It seems that you need explanations on some code, but it's not clear which code you are confused about. – anatolyg Jan 08 '14 at 11:19
  • First Loop from For it's for Row/Collumn check the next for in for it's for Box 3x3 check and it's translated from first function. – Marian Pavel Jan 08 '14 at 11:25

1 Answers1

0

Your problem is with the loop start and end value.

The example uses 0-based counts. The three triplets have indexes (0,1,2) (3,4,5) (6,7,8). You want to use 1-based loops.

You have missed to change the lower limit on the double loop:

int valid(int k, int ii, int jj)
{
    int i,start,final,j;
    start=ii/3*3;
    final=jj/3*3;
    for(i = 1; i <= 9; ++i) {
        if (i != ii && v[i][jj] == k)
            return 0;
        if (i != jj && v[ii][i] == k)
            return 0;
    }
    for(i=start+1;i<=start+3;i++)              // Changed lower limit
        for(j=final+1;j<=final+3;j++)          // Changed lower limit
        {
            if(!(i==ii && j==jj))
            {
                if(v[i][j]==k)
                return 0;
            }
        }
    return 1;
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82