Я не совсем уверен, что вы пытаетесь сделать, но приведенный ниже алгоритм должен дать вам все необходимое вам значение.Вы можете просто игнорировать и / или удалять значения, которые вам не нужны.Вы могли бы, вероятно, заполнить все свои массивы соответствующим образом в той точке, где у вас есть все числа.
Слова, которые я использую:
- квадрат: один квадрат, чтобы вставить число.
- субрегион: группа квадратов, сетка 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++;
}
}
Если вам нужны только левые верхние квадраты на субрегион, просто удалите две внутренние петли:
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++;
}
}