Files
ProjectEulerCPP/Source/Problem15.cpp

100 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//ProjectEuler/C++/Source/Problem15.hpp
//Matthew Ellison
// Created: 09-29-18
//Modified: 07-14-19
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/myClasses
/*
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cinttypes>
#include <string>
#include <sstream>
#include "Stopwatch.hpp"
#include "../Headers/Problem.hpp"
#include "../Headers/Problem15.hpp"
int Problem15::WIDTH = 20;
int Problem15::LENGTH = 20;
void Problem15::move(int currentX, int currentY, uint64_t& numOfRoutes){
//Check if you are at the end
if((currentX == WIDTH) && (currentY == LENGTH)){
++numOfRoutes;
return;
}
//Move right if possible
if(currentX < WIDTH){
move(currentX + 1, currentY, numOfRoutes);
}
//Move down if possible
if(currentY < LENGTH){
move(currentX, currentY + 1, numOfRoutes);
}
}
Problem15::Problem15() : Problem("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?"), numOfRoutes(0){
}
void Problem15::solve(){
//If the problem has already been solved do nothing and end the function
if(solved){
return;
}
//Setup the variables
int currentX = 0; //The current x location on the grid
int currentY = 0; //The current y location on the grid
//Start the timer
timer.start();
//We write this as a recursive function
//When in a location it always moves right first, then down
move(currentX, currentY, numOfRoutes);
//Stop the timer
timer.stop();
//Throw a flag to show the problem is solved
solved = true;
}
std::string Problem15::getString() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
std::stringstream results;
results << "The number of routes is " << numOfRoutes;
return results.str();
}
uint64_t Problem15::getNumberOfRoutes() const{
//If the problem hasn't been solved throw an exception
if(!solved){
throw unsolved();
}
return numOfRoutes;
}