18-recursive-backtracking-2 - CSE 143 Lecture 18 More...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Exercise: Dominoes • The game of dominoes is played with small black tiles, each having 2 numbers of dots from 0-6. Players line up tiles to match dots. • Given a class Domino with the following public methods: int first() // first dots value int second() // second dots value void flip() // inverts 1st/2nd boolean contains(int n) // true if 1st/2nd == n String toString() // e.g. "(3|5)" • Write a method hasChain that takes a List of dominoes and a starting/ending dot value, and returns whether the dominoes can be made into a chain that starts/ends with those values.
Background image of page 2
3 Domino chains • Suppose we have the following dominoes: • We can link them into a chain from 1 to 3 as follows: – Notice that the 3|5 domino had to be flipped. • We can "link" one domino into a "chain" from 6 to 2 as follows:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Exercise client code import java.util.*; // for ArrayList public class SolveDominoes { public static void main(String[] args) { // [(1|4), (2|6), (4|5), (1|5), (3|5)] List<Domino> dominoes = new ArrayList<Domino>(); dominoes.add(new Domino(1, 4)); dominoes.add(new Domino(2, 6)); dominoes.add(new Domino(4, 5));
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

18-recursive-backtracking-2 - CSE 143 Lecture 18 More...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online