Download presentation
Presentation is loading. Please wait.
Published byNelson Alvin Berry Modified over 6 years ago
1
EGR 115 Introduction to Computing for Engineers
Designing The Sudoku Game in MATLAB – Part 2 lecture 13 EGR 115 Introduction to Computing for Engineers
2
Designing The Sudoku Game in MATLAB
Rules of the game: It is our job to complete the Sudoku array by filling-in the empty array entries Every row must contain all of the numbers from 1 to 9 Every col must contain all of the numbers from 1 to 9 Every sub-array must contain all of the numbers from 1 to 9 No number can be repeated in a row, a col, or a sub array Many strategies exist, however, we will be developing our own strategies!! lecture 13 EGR 115 Introduction to Computing for Engineers
3
Designing The Sudoku Game in MATLAB Approach #1
Consider the empty element at row 3 and col 6 Sudoku_array(3, 6) belongs to: Row 3, col 6, and sub-matrix (1:3, 4:6) Based only on row 3, what is the set of possible choices? Based only on col 6, what is the set of possible choices? Based only on sub-matrix (1:3, 4:6) what is the set of possible choices? Allowed by all three? Sudoku_array(3, 6) = 1 2 3 4 5 6 7 8 9 1,3,4,8,9 1, 3, 5, 6, 9 ? 2, 3, 4, 7, 9 3, 9 lecture 13 EGR 115 Introduction to Computing for Engineers
4
Designing The Sudoku Game in MATLAB Approach #1
Algorithm: Identify an empty element in the table Determine the row, col, and sub-matrix to which this element belongs Determine element choices based only on: Row -> row_set Col -> col_set Sub-matrix -> sub_matrix_set Compute the intersection of the three sets Return to step 1. until all empty elements have been considered lecture 13 EGR 115 Introduction to Computing for Engineers
5
Designing The Sudoku Game in MATLAB Approach #1
We will need to run this algorithm up to 81 times Very painful!! Could start with row #1 and “loop” through all 9 cols then row #2 and “loop” through all 9 cols , … We do not know how to “code” that yet!! Need to be able to determine “if” a particular number is contained in an array Logical operations? What type of variable (or data structure) can we use to store the possible choices for each empty element? Three dimensional array? lecture 13 EGR 115 Introduction to Computing for Engineers
6
Designing The Sudoku Game in MATLAB Approach #2
Consider the empty element at row 3 and col 6 Sudoku_array(3, 6) belongs to: Row 3, col 6, and sub-matrix (1:3, 4:6) With NO constraints could be: Considering row 3: Next, considering col 6: Next, considering sub-mat (1:3, 4:6): Conclusion: Sudoku_array(3, 6) = 1 2 3 4 5 6 7 8 9 ? 3, 9 lecture 13 EGR 115 Introduction to Computing for Engineers
7
Designing The Sudoku Game in MATLAB Approach #2
Algorithm: Identify an empty element in the table Determine the row, col, and sub-matrix to which this element belongs Assume that the element can be any of the set 1, …, 9 Reduce the set choices based on: Row: Remove any numbers present in the row, then Col: Remove any numbers present in the col, then Sub-matrix: Remove any numbers present in the sub-matrix Return to step 1. until all empty elements have been considered lecture 13 EGR 115 Introduction to Computing for Engineers
8
Designing The Sudoku Game in MATLAB Approach #2
We will need to run this algorithm up to 81 times Very painful!! Could start with row #1 and “loop” through all 9 cols then row #2 and “loop” through all 9 cols , … A loop within a loop? We do not know how to “code” that yet!! Need to be able to determine “if” a particular number is contained in an array Logical operations? We do not know how to “code” that yet!! What type of variable (or data structure) can we use to store the possible choices for each empty element? Could create a unique file for each empty cell For example: row2_col5.mat lecture 13 EGR 115 Introduction to Computing for Engineers
9
EGR 115 Introduction to Computing for Engineers
Next Lecture Mid-Term Exam#1 lecture 13 EGR 115 Introduction to Computing for Engineers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.