あなたが何をしようとしているのか完全にはわかりませんが、以下のアルゴリズムはあなたが必要とするすべての価値をあなたに与えるはずです。不要な値は無視したり削除したりできます。おそらく、すべての数値が得られた時点で、すべての配列を適切に埋めることができます。
私が使う言葉:
- 正方形:数値を入れる単一の正方形。
- サブリージョン:正方形のグループ、古典的な数独の3x3グリッド。
- パズル:全体、3x3のサブリージョンと9x9の正方形。
コード:
//You should have these values at this point:
int subRegionWidth = something; //amount of horizontal squares in a subregion
int subRegionHeight = something; //amount of vertical squares in a subregion
int amountOfHorizontalSubRegions = something; //amount of subRegion columns next to each other
int amountOfVerticalSubRegions = something; //amount of subregion rows on top of each other
//Doesn't change, so calculated once in advance:
int squaresPerPuzzleRow = subRegionWidth*amountOfHorizontalSubRegions;
//Variables to use inside the loop:
int subRegionIndex = 0;
int squareColumnInPuzzle;
int squareRowInPuzzle;
int squareIndexInPuzzle;
int squareIndexInSubRegion;
for(int subRegionRow=0; subRegionRow<amountOfVerticalSubRegions;subRegionRow++)
{
for(int subRegionColumn=0; subRegionColumn<amountOfHorizontalSubRegions;subRegionColumn++)
{
for(int squareRowInRegion=0; squareRowInRegion<subRegionHeight; squareRowInRegion++)
{
for(int squareColumnInRegion=0; squareColumnInRegion<subRegionWidth; squareColumnInRegion++)
{
squareColumnInPuzzle = subRegionColumn*subRegionWidth + squareColumnInRegion;
squareRowInPuzzle = subRegionRow*subRegionHeight + squareRowInRegion;
squareIndexInPuzzle = squareRowInPuzzle*squaresPerPuzzleRow + squareColumnInPuzzle;
squareIndexInSubRegion = squareRowInRegion*subRegionWidth + squareColumnInRegion;
//You now have all the information of a square:
//The subregion's row (subRegionRow)
//The subregion's column (subRegionColumn)
//The subregion's index (subRegionIndex)
//The square's row within the puzzle (squareRowInPuzzle)
//The square's column within the puzzle (squareColumnInPuzzle)
//The square's index within the puzzle (squareIndexInPuzzle)
//The square's row within the subregion (squareRowInSubRegion)
//The square's column within the subregion (squareColumnInSubRegion)
//The square's index within the subregion (squareIndexInSubRegion)
//You'll get this once for all squares, add the code to do something with it here.
}
}
subRegionIndex++;
}
}
サブリージョンごとに左上の正方形のみが必要な場合は、内側の2つのループを削除するだけです。
for(int subRegionRow=0; subRegionRow<amountOfVerticalSubRegions;subRegionRow++)
{
for(int subRegionColumn=0; subRegionColumn<amountOfHorizontalSubRegions;subRegionColumn++)
{
squareColumnInPuzzle = subRegionColumn*subRegionWidth;
squareRowInPuzzle = subRegionRow*subRegionHeight;
squareIndexInPuzzle = squareRowInPuzzle*squaresPerPuzzleRow + squareColumnInPuzzle;
//You now have all the information of a top left square:
//The subregion's row (subRegionRow)
//The subregion's column (subRegionColumn)
//The subregion's index (subRegionIndex)
//The square's row within the puzzle (squareRowInPuzzle)
//The square's column within the puzzle (squareColumnInPuzzle)
//The square's index within the puzzle (squareIndexInPuzzle)
//The square's row within the subregion (always 0)
//The square's column within the subregion (always 0)
//The square's index within the subregion (always 0)
//You'll get this once for all squares, add the code to do something with it here.
subRegionIndex++;
}
}