announcements. backtracking magic number square tentaizu approach

15
Announcements Announcements

Upload: bathsheba-watkins

Post on 12-Jan-2016

297 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Announcements. Backtracking Magic Number Square Tentaizu Approach

AnnouncementsAnnouncements

Page 2: Announcements. Backtracking Magic Number Square Tentaizu Approach

BacktrackingBacktrackingMagic Number SquareTentaizu Approach

Page 3: Announcements. Backtracking Magic Number Square Tentaizu Approach

Magic Number SquaresMagic Number SquaresA 3x3 magic number square has 9

numbers {1,2,… 9} that must be arranged in such a way that every row, column, and diagonal has the same sum.◦For a 3x3 magic number square the sum

is 3*(3*3+1)/2 = 15.

1515

Page 4: Announcements. Backtracking Magic Number Square Tentaizu Approach

Magic Number SquareMagic Number SquareIt is possible to have different sized

magic number squares of size nxn, ◦where numbers {1,2,… n*n} must be

arranged in such a way that every row and column has the same sum.

◦For a nxn magic number square the sum is n*(n*n+1)/2

Page 5: Announcements. Backtracking Magic Number Square Tentaizu Approach

Magic Number SquareMagic Number SquareSay we want to create a class

MagicSquare that takes in an int n and returns all valid Magic Number Squares of size nxn.◦Example, n = 3 returns 8 magic

squares:2 7 69 5 14 3 8

4 9 23 5 78 1 6

2 9 47 5 36 1 8

4 3 89 5 12 7 6

6 7 21 5 98 3 4

6 1 87 5 32 9 4

8 1 63 5 74 9 2

8 3 41 5 96 7 2

Page 6: Announcements. Backtracking Magic Number Square Tentaizu Approach

Magic Number SquareMagic Number SquareHow would we start?

◦The plan like we did with Eight Queens is just to try a number in the first position. Then we can recursively solve the board at

the next position given that the number has been placed.

Then we systematically “throw out” boards that do not meet our constraints.

◦What do we need to do that? First we will want an empty board that we will

fill in one square at a time. Then we will want to mark which numbers in

the set (1, … , n*n) have already been used. We need to calculate the sum that each row,

column, and diagonal must add up to.

Page 7: Announcements. Backtracking Magic Number Square Tentaizu Approach

public class MagicSquare {

private int[][] square;

private boolean[] possible;

private int totalSqs;

private int sum;

private int numsquares;

// Creates an empty MagicSquare object.

public MagicSquare(int n) {

// Fill in an empty square; 0 represents unfilled.

square = new int[n][n];

for (int i=0; i<n; i++)

for (int j=0; j<n; j++)

square[i][j] = 0;

// Start with all numbers being possible.

totalSqs = n*n;

possible = new boolean[totalSqs];

for (int i=0; i<totalSqs; i++)

possible[i] = true;

sum = n*(n*n+1)/2;

numsquares = 0;

}

• square[][] will hold the numbers we place.• possible[] will hold the numbers that have still not bee used yet.• sum will store what value the rows/columns/diagonals must sum to.

square[][] is size nxninitialize all values to 0, it’s empty to start!

There are n*n total numbers,so possible[] is size n*n. At first all of the numbers can be usedso the entire array is set to true.

Page 8: Announcements. Backtracking Magic Number Square Tentaizu Approach

// Recursive method which fills in the square at (row,col).

public void fill(int row, int col) {

// Screen away incorrect squares.

if (!checkRows() || !checkCols() || !checkDiags())

return;

// Filled everything, so print !!

if (row == square.length) {

System.out.println(this);

numsquares++;

return;

}

// Try each possible number for this square.

for (int i=0; i<totalSqs; i++) {

if (possible[i]) {

square[row][col] = i+1;

possible[i] = false;

// Go to the next square.

int newcol = col+1;

int newrow = row;

if (newcol == square.length) {

newrow++;

newcol = 0;

}

// Recursively fill.

fill(newrow, newcol);

// Undo this square.

square[row][col] = 0;

possible[i] = true;

}

}

}

Base Cases:Either we broke the constraints, so we throw that board out.OR We’re DONE!!

Try all possible values for this spot

Find the new row and col, so we can recursively fill the next spot.

Recursively call fill() on our new spot.

Undo this square for future recursive calls, so that we can try ALL valid boards

Page 9: Announcements. Backtracking Magic Number Square Tentaizu Approach

// Recursive method which fills in the square at (row,col).

public void fill(int row, int col) {

// Screen away incorrect squares.

if (!checkRows() || !checkCols() || !checkDiags())

return;

// Filled everything, so print !!

if (row == square.length) {

System.out.println(this);

numsquares++;

return;

}

// Try each possible number for this square.

for (int i=0; i<totalSqs; i++) {

if (possible[i]) {

square[row][col] = i+1;

possible[i] = false;

// Go to the next square.

int newcol = col+1;

int newrow = row;

if (newcol == square.length) {

newrow++;

newcol = 0;

}

// Recursively fill.

fill(newrow, newcol);

// Undo this square.

square[row][col] = 0;

possible[i] = true;

}

}

}

Page 10: Announcements. Backtracking Magic Number Square Tentaizu Approach

// Returns true iff all the rows are okay.

public boolean checkRows() {

// Try each row.

for (int i=0; i<square.length; i++) {

int test = 0;

boolean unFilled = false;

// Add up the values in this row.

for (int j=0; j<square[i].length; j++) {

test += square[i][j];

if (square[i][j] == 0)

unFilled = true;

}

// If the row is filled and adds up to the wrong number,

// this is an invalid square.

if (!unFilled && test != sum)

return false;

}

// Never found proof of an invalid row.

return true;

}

Determine whether each row is filled andthe row’s sum.

If the row is filled and adds up to the wrong numberthen our row constraint is violated, and we return false.

At this point we have checked all rows, and none of them violated our constraint.

Page 11: Announcements. Backtracking Magic Number Square Tentaizu Approach

Magic Number Square Magic Number Square Decision TreeDecision TreeShown on the board…

Page 12: Announcements. Backtracking Magic Number Square Tentaizu Approach

Tentaizu ApproachTentaizu Approach The game is played on a 7x7 board. Exactly 10 of the 49

squares are each hiding a star. Your task is to determine which squares are hiding the stars.

Other squares in the board provide clues: A number in a square indicates how many stars lie next to the square

No square with a number in it contains a star, but a star may appear in a square with no adjacent numbers.

Page 13: Announcements. Backtracking Magic Number Square Tentaizu Approach

Tentaizu ApproachTentaizu ApproachBreak down the problem into smaller steps

1. Read in the board• Determine the squares with numbers• Determine the squares that are blank

2. Place the stars in a backtracking method based on the possible locations

3. Check that the current or finished board meets the requirements of the game• Ten stars are present• Each numbered square has the

correct number of adjacent stars

Page 14: Announcements. Backtracking Magic Number Square Tentaizu Approach

Optional Homework Optional Homework ProblemProblemGet together with someone in the class and

brainstorm how you will solve the Tentaizu assignment.

Turn in a paper describing your strategy.◦ What matrices and/or arrays will you need?◦ What constraints will you check against?

How will you know when these constraints are violated?◦ How will you know when the board is complete?

Show a decision tree diagramming how your Tentaizu algorithm will work. (Like how we did with the Magic Number Square)

Page 15: Announcements. Backtracking Magic Number Square Tentaizu Approach

ReferencesReferencesSlides adapted from Arup Guha’s

Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/

Additional material from the textbook:Data Structures and Algorithm Analysis in

Java (Second Edition) by Mark Allen WeissAdditional images:

www.wikipedia.comxkcd.com