Matlab Sudoku Row and Column Check
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to solve a Sudoku puzzle with Matlab and I'm not sure how to check the rows and columns in order to solve the puzzle. I am creating a "Can't be" 9x9x9 matrix to fill in what can't go in each square. So in the 3D section a 0 placeholder indicates the number can be used. My thought is to sum each row and column for 1-9 (so 45) and check for unique values, but I don't know how to do that. I've tried looking at code online, but I don't understand their process. Any help or ideas to checking the rows and columns is greatly appreciated! Thank you!
1 Commento
Kirby Fears
il 28 Ott 2015
Modificato: Kirby Fears
il 28 Ott 2015
Hi Katrina,
If this is an assignment for an academic course, please tag your post as "homework" so users know to respond with general tips instead of full solutions.
Risposte (1)
Kirby Fears
il 28 Ott 2015
Katrina,
You could approach this several ways, so the first big step is to decide on a framework that makes sense to you. It sounds like your current framework has an integer array (9x9) representing the puzzle and a complementary boolean array (9x9x9) representing available values to fill each position with.
You can initialize the arrays as follows:
puzzle = int8(zeros(9));
possible = true([9,9,9]);
The puzzle should start off with certain values pre-filled of course. You'd need to fill those into "puzzle" and switch certain "possible" booleans to false.
You could write a function called "puzzleFill" which will take arguments such as "row", "column" and "value" to place a value into the puzzle. You can add values in Matlab like this:
puzzle(row,col) = value;
Now puzzle(row,col) should no long have "possible" filling values since a value has been determined (this can be adjusted in puzzleFill). You would also need to remove "value" from the "possible" list for other positions in that row or column. You could do this by using "value" as an index. For example, to mark "value" as used along a specific "row", you can set "possible" to false along the row.
possible(row,:,value) = false; % mark value as used along "row"
When determining how to fill the empty spaces in the puzzle, you might find the below functionality helpful:
% number of possible fill values for position (row,col) of puzzle:
sum_possible = sum(possible,3); % 9x9 array
disp(sum_possible(row,col));
% possible fill values for position (row,col):
disp(find(possible(row,col,:)));
You may also find the built-in intersect() function helpful when comparing lists of possible values across different puzzle positions.
Hope this helps.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!