34S-CS106X-Practice-Key

34S-CS106X-Practice-Key - CS106X Handout 34S Autumn 2009...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106X Handout 34S Autumn 2009 December 2 nd , 2009 CS106X Practice Final Solution Solution 1: Recursive Backtracking and the 70 2 Puzzle bool Solve(Grid<bool>& board) // assume board is initially cleared { return Solve(board, 1); } bool Solve(Grid<int>& board, int dimension) { if (dimension == 25) return true; for (int row = 0; row < board.numRows() - dimension; row++) { for (int col = 0; col < board.numCols() - dimension; col++) { if (CanPlaceSquare(board, row, col, dimension)) { PlaceSquare(board, dimension, row, col); if (Solve(board, dimension + 1)) return true; LiftSquare(board, dimension, row, col); } } } return false; } Solution 2: Braided Lists struct node { int value; node *next; }; The recursive approach is easier to code up, but requires more a much more clever algorithm. void Braid(node *list) { Queue<int> numbers; BraidRec(list, numbers); } void BraidRec(node *list, Queue<int>& numbers) { if (list == NULL) return; numbers.enqueue(list->value); BraidRec(list->next, numbers); node *newNode = new node; newNode->value = numbers.dequeue(); newNode->next = list->next; list->next = newNode; } A fully iterative approach is best handled in two passes: void Build(node *list) { node *reverse = NULL; for (node *curr = list; curr != NULL; curr = curr->next) { node *newNode = new node; newNode->value = curr->value; newNode->next = reverse; reverse = newNode; } // reverse now addresses a memory-independent version of the original list, // where all of the nodes are in reverse order. node *rest = reverse; // rest addresses part that has yet to be braided in for (node *curr = list; curr != NULL; curr = curr->next->next) { node *next = rest->next; rest->next = curr->next; curr->next = rest; rest = next; } } Solution 3: Quadtrees quadtree *gridToQuadtree(Grid<bool>& image) { return gridToQuadtree(image, 0, image.numCols(), 0, image.numRows()); } quadtree *gridToQuadtree(Grid<bool>& image, int lowx, int highx, int lowy, int highy) { quadtree *qt = new quadtree; qt->lowx = lowx; qt->highx = highx - 1; qt->lowy = lowy; qt->highy = highy - 1; if (allPixelsAreTheSameColor(image, lowx, highx, lowy, highy)) { qt->isBlack = image[lowx][lowy]; for (int i = 0; i < 4; i++) qt->children[i] = NULL; } else { int midx = (highx - lowx) / 2; int midy = (highy - lowy) / 2; qt->children[NW] = gridToQuadTree(image, lowx, midx, midy, highy); qt->children[NE] = gridToQuadtree(image, midx, highx, midy, highy); qt->children[SE] = gridToQuadTree(image, midx, highx, lowy, midy);...
View Full Document

This note was uploaded on 03/07/2010 for the course CS 201 taught by Professor Selimaksoy during the Spring '09 term at Bilkent University.

Page1 / 6

34S-CS106X-Practice-Key - CS106X Handout 34S Autumn 2009...

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

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