0

この課題の締め切りに間に合いませんでしたが、このプロジェクトで何をしているのか理解できずにいまだに悩まされています。4 つのメソッドを追加する必要がある数独ソリューション チェッカーのパート 2

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

すべてをチェックし、失敗した行または列ごとに行を出力する必要があります。他は

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

最後に、私の checkAll() メソッドには、上記の 3 つをそれぞれ 9 回呼び出す 3 つの入れ子になったループがあるはずです。

ここでソリューションチェッカーをすでにコーディングしていると思っていたので、この部分に何が必要なのかわかりません

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);
 }
4

1 に答える 1

0

あなたが書いた関数は、まさにあなたが必要としているものではありません。関数は、単一の数値が行 (列またはボックス) に何回あるかをチェックするだけです。

行 (列またはボックス) が適切かどうかを判断するには、1 つの数字だけでなく、すべての数字 (1 ~ 9) を確認する必要があります。

ただし、良いニュースは、関数を使用して必要な関数を実装できることです。

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;
}

(興味がある場合):これは最も効率的なソリューションではありません。O(n 2 ) 時間で実行されます。isGoodRow() は、行の # をヒストグラム化することで O(n) 時間で見つけることができます。


必要な機能を実装したら:

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

次に、それらを使用して実装する必要があります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;
}
于 2013-12-04T05:11:25.350 に答える