필터 지우기
필터 지우기

Matlab Sudoku Row and Column Check

조회 수: 2 (최근 30일)
Katrina
Katrina 2015년 10월 28일
답변: Kirby Fears 2015년 10월 28일
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
Kirby Fears
Kirby Fears 2015년 10월 28일
편집: Kirby Fears 2015년 10월 28일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kirby Fears
Kirby Fears 2015년 10월 28일
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.

카테고리

Help CenterFile Exchange에서 Sudoku에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by