سؤال

I've missed the deadline for this assignment but it is still bugging me that I do not understand what I'm doing for this project. Its part 2 of a sudoku solution checker that needs four methods added to it which are

public boolean checkAndPrintReport( ) {*/return true;}

which should check all and print lines for every failed row or column. The others are

public boolean isGoodRow( int yRowParam ) {return true;}
public boolean isGoodColumn( int xColParam ) {return true;}
public boolean isGoodBlock(int xBlockP, int yBlockP) {return true;} 

Lastly, my checkAll() method is supposed to have three nested loops calling the above three 9 times each.

I don't understand what is needed for this part since I thought I already coded a solution checker here

public int timesRowHas( int yParam, int number ) { 
    int nTimesHas = 0;
    for( int x = 0; x < 9; x++ )
        if( this.getCell(x, yParam) == number )
            nTimesHas = nTimesHas + 1;

    return( nTimesHas );
}

public int timesColHas( int xParam, int number ) {
    int nTimesHas = 0;
    for( int y = 0; y < 9; y++ )
        if( this.getCell(xParam, y) == number )
            nTimesHas = nTimesHas + 1;

    return( nTimesHas );
}

public int timesBlockHas( int xBlockParam, int yBlockParam, int number ) {
    if( xBlockParam < 0 || xBlockParam > 2 || yBlockParam < 0 || yBlockParam > 2 )
        throw new IllegalArgumentException("Bad xBlockParam or bad yBlockParam or both..");

    int nTimesHas = 0; 
    for (int x=0; x<3; x++)
        for (int y=0; y<3;y++)
            nTimesHas = nTimesHas +getCell(xBlockParam+x, yBlockParam+y);

    return(nTimesHas);
 }
هل كانت مفيدة؟

المحلول

The functions you wrote are not exactly what you need. Your functions only check how many times a single number is in a row (column or box).

To tell if a row (column or box) is good, you really need to check all numbers (1-9), not just one of them.

However, the good news is you can implement the needed functions using your functions:

public boolean isGoodRow( int yRowParam ){
    // for number = 1,..,9, ensure the count is not > 1
    for(int i=1; i<=9; i++)
        if(timesRowHas(yRowParam, i) > 1)
            return false;
    return true;
}

(In case you are interested): This is not the most efficient solution, it runs in O(n2) time. isGoodRow() can be found in O(n) time by histograming the #'s in the row.


Once you have implemented the necessary functions:

public boolean isGoodRow( int yRowParam )
public boolean isGoodColumn( int xColParam )
public boolean isGoodBlock(int xBlockP, int yBlockP)

Then you just need to use those to implement checkAndPrintReport():

public boolean checkAndPrintReport(){ 
   for(int i=0; i<9; i++)
        if(!isGoodRow(i) || !isGoodColumn(i) || !isGoodBlock(i/3, i%3)
            return false;
   return true;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top