{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2 soln - Homework 2 Solution Problem 1 Problem 2 Problem...

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

View Full Document Right Arrow Icon
Homework 2 Solution Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 1: #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; }; 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 2: (1,1) (1,2) (1,3) (1,4) (2,4) (3,4) (3,3) (3,5) (4,5) Problem 3: Make three changes to the Problem 1 solution: Change #include <stack> to #include <queue>
Image of page 1

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

View Full Document Right Arrow Icon
Change stack<Coord> toDo; to queue<Coord> toDo; Change Coord curr = toDo.top(); to Coord curr = toDo.front(); Problem 4: (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
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