II. stepwise refinement for program design ------------------------------------------ STEPWISE REFINEMENT (WIRTH 1971) Want to be able to justify a design top-down: Abstract Program | Refined Program / | \ SubP 1 SubP 2 SubP 3 ------------------------------------------ How can it be helpful to have a harder problem to solve? A. Example: 8 queens ------------------------------------------ EXAMPLE: THE 8 QUEENS PROBLEM Place 8 queens on a (8x8) chessboard so that none of them attacks any other. [--|--|--|--|--|--|--|--] | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | |--+--+--+--+--+--+--+--| | | | | | | | | | [--|--|--|--|--|--|--|--] ------------------------------------------ How do queens attack in chess? So what does a solution look like? 1. example development ------------------------------------------ START OF THE DEVELOPMENT Start with Example: Let A be the set of all possible placements of 8 queens on the chessboard Let p(b) be true when x is a solution Generate ------------------------------------------ How would you generate all possible placements? Why is that correct? What could be wrong with that? 2. refinements ------------------------------------------ HOW CAN WE DO BETTER? Pre-selection strategy: eliminate Abstractly: Criteria for pre-selection: 1. 2. 3. ------------------------------------------ ------------------------------------------ SECOND STEP, USING PRE-SELECTION generate b in B_r until q(b) or B_r is empty if q(b), then return b Why is this better? ------------------------------------------ ------------------------------------------ APPLYING PRE-SELECTION TO 8 QUEENS What could r be? Then what would q be? ------------------------------------------ So what does the program look like now? How could we continue this? B. Sudoku ------------------------------------------ FOR YOU TO DO Design a program to generate Sudoku puzzles. A Sudoku puzzle is a 9 x 9 grid, consisting of 9 3x3 subgrids. A solution to a puzzle must place in each subgrid, the digits 1-9, and each digit may only appear once per subgrid and once in each column, and once in each row. The puzzle should have some empty grid squares and some numbers filled in. The solver fills in the empty squares. The puzzle should be generated to have exactly one solution (i.e., one way to fill in the values for the empty squares). ------------------------------------------