project 3 solution

project 3 solution - #include"grid.h#include...

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

View Full Document Right Arrow Icon
#include "grid.h" #include <string> #include <cctype> using namespace std; bool getDeltas(char dir, int& dr, int& dc); //************************************* // isPathWellFormed //************************************* bool isPathWellFormed(string path) { // Each iteration of the loop will check one path segment size_t k = 0; while (k != path.size()) { // Path segment must start with a valid direction char dir = tolower(path[k]); if (dir != 'n' && dir != 'e' && dir != 'w' && dir != 's') return false; // Next must come a digit k++; if (k == path.size() || !isdigit(path[k])) return false; // Optionally, there may be another digit k++; if (k == path.size()) return true; if (isdigit(path[k])) k++; } // We get here if we got through the path without a problem return true; } //************************************* // followSegment //************************************* int followSegment(int r, int c, char dir, int maxSteps)
Image of page 1

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

View Full Document Right Arrow Icon
{ // Make sure (r,c) is a valid empty grid cell int nr = getRows(); int nc = getCols(); if (r < 1 || r > nr || c < 1 || c > nc || isWall(r,c)) return -1; // Make sure maxSteps is nonnegative if (maxSteps < 0) return -1; // Make sure the direction is OK, and for a given direction // determine dr and dc such that moving from (r,c) to (r+dr,c+dc) // is a move in that direction.
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