hw2solution

hw2solution - Winter 2009 CS 32 Homework 2 Solution Problem...

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

View Full Document Right Arrow Icon
Winter 2009 CS 32 Homework 2 Solution Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Problem 1: a. Some number in the range 8821 through 28817, depending on your student id. b. Some hexadecimal number. c. Some hexadecimal number greater than your answer to part b. The difference between those two numbers is a multiple of your answer to part a. d. Some hexdecimal number, one more than your answer to part c. e. Some hexadecimal number greater than your answer to part c. Depending on your processor type and compiler, the difference between these two numbers is, when expressed in decimal, 16 or 23, or 12 or 15 or 13, or (not likely) 11. f. The same hexadecimal number as in part c. [Some people's debuggers seem to give a wrong value here; we'll check that out.] g. Some hexadecimal number greater than your answer to part f. The difference between those two numbers is, when expressed in decimal, 24 if the difference between your answers to parts e and c is 16 or 23; 16 or 14 if that difference is 12; 16 if that difference is 15; 14 if that difference is 13; or (unlikely) 12 if that difference is 11. [Some people's debuggers seem to give the same answer as part f; we'll check that out.] Problem 2: #include <stack> using namespace std; const char WALL = 'X'; const char OPEN = '.'; const char SEEN = 'o'; class Coord { public: Coord(int rr, int cc) : m_r(rr), m_c(cc) {} int r() const { return m_r; } int c() const { return m_c; } private: int m_r; int m_c;
Background image of page 1

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

View Full DocumentRight Arrow Icon
}; bool pathExists(char maze[][10], int sr, int sc, int er, int ec) { if (sr < 0 || sr > 9 || sc < 0 || sc > 9 || er < 0 || er > 9 || ec < 0 || ec > 9 || maze[sr][sc] != OPEN || maze[er][ec] != OPEN) return false; stack<Coord> toDo; toDo.push(Coord(sr,sc)); while ( ! toDo.empty() ) { Coord curr = toDo.top(); toDo.pop(); const int cr = curr.r(); const int cc = curr.c(); if (cr == er && cc == ec) return true; maze[cr][cc] = SEEN; // anything non-OPEN will do if (maze[cr-1][cc] == OPEN) toDo.push(Coord(cr-1,cc)); if (maze[cr+1][cc] == OPEN) toDo.push(Coord(cr+1,cc)); if (maze[cr][cc+1] == OPEN) toDo.push(Coord(cr,cc+1)); if (maze[cr][cc-1] == OPEN) toDo.push(Coord(cr,cc-1)); } return false; } Problem 3: (1,1) (1,2) (1,3) (1,4) (2,4) (3,4) (3,3) (3,5) (4,5) Problem 4: Make three changes to the Problem 2 solution:
Background image of page 2
Change #include <stack> to #include <queue> Change stack<Coord> toDo; to queue<Coord> toDo; Change Coord curr = toDo.top(); to Coord curr = toDo.front(); Problem 5: (1,1) (2,1) (1,2) (3,1) (1,3) (1,4) (2,4) (3,4) (3,5) The stack solution visits the cells in a depth-first order: it continues along a path until it hits a dead end, then backtracks to the most recently visited intersection that has unexplored branches. Because we're using a stack, the next cell to be visited will be a
Background image of page 3

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

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

This note was uploaded on 04/04/2011 for the course CS 32 taught by Professor Davidsmallberg during the Spring '08 term at UCLA.

Page1 / 10

hw2solution - Winter 2009 CS 32 Homework 2 Solution Problem...

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

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