34S-CS106X-Practice-Key

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

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

View Full Document Right Arrow Icon
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; }
Image of page 1

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

View Full Document Right Arrow Icon
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];
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern