Homework 7: Sudoku. Preliminaries. Overview

Similar documents
Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

: Principles of Imperative Computation. Fall Assignment 5: Interfaces, Backtracking Search, Hash Tables

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Implementation of a Sudoku Solver Using Reduction to SAT

Project 4 (8 Puzzle) Clarifications and Hints 1 / 9

CSCI 3155: Homework Assignment 3

EECE.2160: ECE Application Programming

In either case, remember to delete each array that you allocate.

15100 Fall 2005 Final Project

15-122: Principles of Imperative Computation, Spring 2013

Parallelizing SAT Solver With specific application on solving Sudoku Puzzles

15-780: Problem Set #2

Repetition Through Recursion

This class simulates the Sudoku board. Its important methods are setcell, dorow, docolumn and doblock.

Lecture 1. 1 Introduction

EXAM Computer Science 1 Part 1

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Informatics 2D. Coursework 1: Search and Games

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

CS 177 Recitation. Week 8 Methods

CMPUT 396 Sliding Tile Puzzle

CPSC 217 Assignment 3

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

CS108, Stanford Handout #3. HW1 CodeCamp

COMPSCI 220 Programming Methodology Project Assignment 06: Expression Evaluator

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Homework Assignment #3

You ve encountered other ways of signalling errors. For example, if you lookup an unbound key in a hashtable, Java (and Scala) produce nulls:

CS 2110 Summer 2011: Assignment 2 Boggle

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Sudoku-4: Logical Structure

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

CS2 Practical 1 CS2A 22/09/2004

CMPSCI 187 / Spring 2015 Hanoi

Using Euler s Theorem

Constraint Satisfaction Problems (CSPs)

Homework 4: due by 11:59pm on Wednesday, April 2, 2014 (Total: 100 points)

CSC 427: Data Structures and Algorithm Analysis. Fall 2006

CmpSci 187: Programming with Data Structures Spring 2015

CSCI 3155: Lab Assignment 2

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

This homework has an opportunity for substantial extra credit, which is described at the end of this document.

CMSC 201 Spring 2018 Project 3 Minesweeper

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

Practical Questions CSCA48 Week 6

Self assessment due: Monday 10/29/2018 at 11:59pm (submit via Gradescope)

Homework 5: Spatial Games : Programming for Scientists Due: Thursday, March 3, 2016 at 11:59 PM

Tips from the experts: How to waste a lot of time on this assignment

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS446: Machine Learning Fall Problem Set 4. Handed Out: October 17, 2013 Due: October 31 th, w T x i w

CIS 121 Data Structures and Algorithms with Java Spring 2018

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

CS W4701 Artificial Intelligence

CSCI 3155: Homework Assignment 2

CS70 - Lecture 6. Graphs: Coloring; Special Graphs. 1. Review of L5 2. Planar Five Color Theorem 3. Special Graphs:

Exploring Performance Tradeoffs in a Sudoku SAT Solver CS242 Project Report

Due Friday, March 20 at 11:59 p.m. Write and submit one Java program, Sequence.java, as described on the next page.

Backtracking. See Section 7.7 p of Weiss

CMPSCI 187 / Spring 2015 Sorting Kata

CSE 130, Fall 2006: Final Examination

Homework 2. Due 4:00pm, Wednesday, February 13, Installation and Handin 2

Practice Midterm Exam #2

CHAPTER 3. Register allocation

CSCI 3155: Homework Assignment 4

CS 314 Midterm 2 Fall 2012

CMSC 201 Spring 2019 Lab 06 Lists

Programming Project: Game of Life

Spring 2018 June 20 th, 2018 Extra Practice Problems. Short Problems

CompSci 220. Programming Methodology 12: Functional Data Structures

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

CHAPTER 3. Register allocation

Part I: Written Problems

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Supplement: Case Study: Sudoku. For Introduction to Java Programming By Y. Daniel Liang

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

You must bring your ID to the exam.

cc -o build/test_translate build/test_translate.o b... $

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

CS61BL: Data Structures & Programming Methodology Summer Project 1: Dots!

Part I: Written Problems

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

CIS 110: Introduction to Computer Programming

Exercise 1: Tail Sum of Squares (2 points) Homework 3 READ THIS BEFORE YOU BEGIN:

CS 1110: Introduction to Computing Using Python Loop Invariants

Finally, all fields are public by default, so you can easily write simple functions, such as this one, without writing getters:

Question 1: knn classification [100 points]

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CS 2316 Homework 9b GT Thrift Shop Due: Wednesday, April 20 th, before 11:55 PM Out of 100 points. Premise

CSCI 3155: Lab Assignment 2

Instructions for Crossword Assignment CS130

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

10/11/2017. Constraint Satisfaction Problems II. Review: CSP Representations. Heuristic 1: Most constrained variable

2018 Pummill Relay problem statement

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Transcription:

Homework 7: Sudoku For this assignment, you will write a Sudoku solver. To do so, you will (1) use Scala collections extensively, (2) implement a backtracking search algorithm, and (3) implement constraint propagation. Preliminaries We assume you know how to play Sudoku. If you don t, you should play a few games by hand, before attempting this assignment. The support code for this assignment is in the hw.sudoku package. You should create a directory-tree that looks like this:./sudoku project plugins.sbt src main scala... Your solution goes here test scala...yours tests go here The project/plugins.sbt file must have exactly this line: addsbtplugin (" edu. umass. cs" % " compsci220 " % " 1.0.2 ") Overview A Sudoku board is a 9-by-9 grid, where each cell is either blank or has a value in the range 1 9. For this assignment, we ll use strings of length 81 to represent Sudoku boards, where each block of nine characters represents a successive row. For example, the following string: val puzzle = "...8.3...6..7..84.3.5..2.9...1.54.8...4.27.6...3.1..7.4.72..4..6...4.1...3 " Represents the following board: 8 3 6 7 8 4 3 5 2 9 1 5 4 8 Here are some more examples: 4 2 7 6 3 1 7 4 7 2 4 6 4 1 3 ".43.8.25.6...1.949...4.7...6.8...1.2...382.5...5.34.9.71. " " 2...8.3...6..7..84.3.5..2.9...1.54.8...4.27.6...3.1..7.4.72..4..6...4.1...3 " "..3.2.6..9..3.5..1..18.64...81.29..7...8..67.82...26.95..8..2.3..9..5.1.3.. " " 1..92...524.1...7..5...81.2...4.27...9..6...3.945...71..6 " Your first task is to parse strings that represent Sudoku boards. You may assume that all strings represent solvable boards and that the string has exactly 81 characters. This part of the assignment should be trivial. 129

Solving Sudoku puzzles is much harder, but we ll walk you through it. Backtracking search is a recursive algorithm that operates as follows. Given a a Sudoku board, B: If B is already filled completely and a solution, return the solution. If B is an invalid board (e.g., two 2s in a row), abort. Otherwise, generate a list of all boards that fill in one more square of B. For each generated board, recursively apply the search function: If any board produces a valid solution, return that board If no board produces a solution, abort and return This approach will work in principle. But, in practice there are too many boards to search: there are 81 squares, and each square can hold 10 values (a digit or blank). Therefore, there are 10 81 possible boards, which exceeds the number of atoms in the observable universe. To actually solve Sudoku problems, we need to combine backtracking search with constraint propogation. When you play Sudoku yourself, every time you fill a digit into a cell, you can eliminate that digit from several other cells. For example, if you fill 2 into the top-left corner of the board above, you can eliminate 2 from the first row, first column, and first box. i.e., there is no point even trying to place 2 in those spots, since the 2 in the corner constrains those cells. We ll augment backtracking search with constraint propagation, which implements this intuition. The key idea is to store not the value at a cell, but the set of values that may be placed in a cell. For example, on the empty board the values 1 9 may be placed at any cell: With this representation, when we place a value at a cell, we eliminate it from the other cells in the same row, column, and box (collectively known as the peers of a cell). For example, if we place 5 at the top- left corner of the empty board, we can eliminate 5 from the peers of the top-left corner: 5 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 123456789 123456789 123456789 123456789 123456789 123456789 1234 6789 1234 6789 1234 6789 123456789 123456789 123456789 123456789 123456789 123456789 This procedure will significantly reduce the number of boards that need to be visited. Programming Task Your task is to implement the SolutionLike and BoardLike traits. You can use the template in fig. 27.1 to do so. We recommend proceeding in this order and testing as you go along: 1. Implement Solution.peers. peers(r, c) produces the coordinates of all cells in the same row as r, same column as c, and same block as (r,c). Do not include (r, c) in the set of its own peers. i.e., the value of the expression peers(r,c).contains((r, c)) should be false. 130

import hw. sudoku._ object Solution extends SudokuLike { type T = Board def parse ( str : String ): Board =??? // You can use a Set instead of a List (or, any Iterable ) def peers ( row : Int, col : Int ): List [( Int, Int )] =??? // Top - left corner is (0,0). Bottom - right corner is (8,8). Feel free to // change the fields of this class. class Board ( val available : Map [( Int, Int ), List [ Int ]]) extends BoardLike [ Board ] { def availablevaluesat ( row : Int, col : Int ): List [ Int ] = { // Assumes that a missing value means all values are available. Feel // free to change this. available. getorelse (( row, col ), 1. to (9). tolist ) def valueat ( row : Int, col : Int ): Option [ Int ] =??? def issolved (): Boolean =??? def isunsolvable (): Boolean =??? def place ( row : Int, col : Int, value : Int ): Board = { require ( availablevaluesat (row, col ). contains ( value ))??? // You can return any Iterable (e.g., Stream ) def nextstates (): List [ Board ] = { if ( isunsolvable ()) { List () else {??? def solve (): Option [ Board ] =??? Figure 27.1: Template for Sudoku 131

2. Implement Solution.parse You should assume that the input string has exactly 81 characters, where each character is a digit or a period. Each successively block of nine characters represents a row (i.e., row-major order). As the template suggests, you need to store the set of available values at each cell instead of the value at the cell. You ll need to define the empty board as the map with all values available at each cell and implement parse using peers as a helper function. 3. Implement Board.valueAt. You should produce the digit stored at the given row and column or None if it is blank. 4. Implement Board.isSolved and Board.isUnsolvable. You may assume that the constraints represented by available are valid. Therefore, a board is solved if every cell is constrained to exactly one value. Similarly, a board is unsolvable if any cell is constrained to the empty set of values (i.e., nothing can be placed in that cell). 5. Implement Board.place. The application place(row, col, value) produces a new board with value placed at (row, col) of this board. You may assume that the predicate availablevaluesat(row, col).contains(value) is true. For the new board to be valid, you ll have to: (a) Remove value from set of available values of each peer (i.e., peers(row, col)). (b) While doing (1), if a peer is constrained to exactly 1 value, remove that value from its peers. 6. Implement nextstates, which returns the list of all boards that have exactly one additional value placed on the board. You should sort the returned list: ensure that boards with fewer available values occur earlier in the list. 7. Implement solve. If this.issolved is true, then return Some(this). If not, iterate through the list of nextstates, applying solve to board. Return the first solution that you find. If no solution is found, return None. Solvable Boards Your solver will not be able to solve arbitrary Sudoku boards. But, here are some boards that should work: val fromcs121_1 = " 85...4.1...67...21...3..85...7...982...3...15..5...43...37...2.9...58 " val fromcs121_2 = ".1...2..3..9..1656..7...33.7..8...89...6...6.254..9.5..1..7..3...2 " val puz1 = ".43.8.25.6...1.949...4.7...6.8...1.2...382.5...5.34.9.71. " val puz2 = " 2...8.3...6..7..84.3.5..2.9...1.54.8...4.27.6...3.1..7.4.72..4..6...4.1...3 " There are several sources of Sudoku puzzles on the Web. There are 50 purportedly easy puzzles on Peter Norvig s webpage: http://norvig.com/easy50.txt You can use this terminal command to translate them into the format for this assignment: curl -s http :// norvig. com / easy50. txt \ tr \n \ tr 0. \ sed s/ // \ sed s/ ======== /\ $ \n/g In addition, we encourage you to share solvable puzzles with each other on Piazza. Check Your Work Here is a trivial test suite that simply checks to see if you ve defined the Solution object with the right type: 132

class TrivialTestSuite extends org. scalatest. FunSuite { test (" The solution object must be defined ") { val obj : hw. sudoku. SudokuLike = Solution You should place this test suite in src/test/scala/trivialtestsuite.scala. This test suite must run as-is, or you may receive no credit for your solution. Hand In From the sbt console, run the command submit. The command will create a file called submission.tar.gz in your assignment directory. Submit this file using Moodle. For example, if the command runs successfully, you will see output similar to this: Created submission. tar. gz. Upload this file to Moodle. [ success ] Total time : 0 s, completed Jan 17, 2016 12: 55: 55 PM Note: The command will not allow you to submit code that does not compile. If your code doesn t compile, you will receive no credit for the assignment. 133

134