Checking in a collection?


#1

I have a Sudoku grid that is stored in a 1D vector. Here is an example of the board
foto_no_exif
Say, given a row and column, I want to find all values that are in its row, column, and 3x3 grid.
Using a for loop, I can do the following:

val row = 0
val col = 0  // We want to check for [0, 0]
for(i <- 0 to 8) {
     if(myVector(i + row * 9) > 0) // do something
     if(myVector(col + i * 9) > 0) // do something
}

This allows me to check for values in the row and column of [0, 0]. However, I cannot think of a formula to check the 3x3 grid for [0, 0]. Using that same loop with i valued 0 through 8, can anyone think of a way to check all values in its grid?
Thanks!


#2

You can calculate indices like this:

for(iSquare <- 0 until 9) {
val squareBegin = 3iSquare
for(x <- 0 until 3; y <- 0 until 3) {
val index = squareBegin + x + 9
y
println(index) // or whatever
}

}


#3

I think Markdown ate some of your code


#4

Worst forum software ever.