This game works very similar to plinko. Except every time the ball/disk falls it picks up points. (meaning points are picked up along the path not just at the end.) Anyway, the maze it goes through is a pyramid and for my assignment the the number of rows in the maze can be as high as INT_MAX. My question is the code crashes once I go over 4 rows. Side note, it actually still calculates the right answer with over 4 rows but it crashes. Any help?
Also another question but I dont know if this is the place for it. I crashed my code too many times trying to fix this code and now my Fault tolerant heap shim always keeps it from crashing but i have to know if it crashes. Anyone know how to disable it on windows 10?
#include <iostream>
#include <vector>
using namespace std;
class CIS14
{
public:
int getMaxPoints(vector<vector<int>> &maze)
{
if (maze.empty()){
return 0;
}
for (int i = maze.size(); i > 0; --i)
{
for (int j = 0; j < (i - 1); ++j)
{
int x = maze[i-1].size();
int y = maze[i-2].size();
if (&maze[i-2][j]==nullptr)
{
return 0;
}
if (y >= x)
{
return 0;
}
else if (x > y)
{
maze[i-2][j] = maze[i-2][j] + std::max(maze[i-1][j], maze[i-1][j+1]);
}
If you're getting crashes while working with vectors/arrays, then chances are you are going to an out-of-bounds index.
If you use my_vector.at(index) instead of my_vector[index], C++ will do bounds checking for you, similar to languages like Java. This will allow you to locate exactly when the out-of-bounds indexing happens.
if (&maze[i-2][j]==nullptr)
What are you trying to do here...? I can almost guarantee this check is not doing what you think it's doing.
So for example, if you were to change your std::max line to maze.at(i-2].at(j) = maze.at(i-2).at(j) + std::max(maze.at(i-1).at(j), maze.at(i-1).at(j+1)); along with every other place you have []'s, the program will at least crash at a specific instant it tries to call out-of-bound indices. You also can capture this exception by wrapping the whole code in a try-catch block. http://www.cplusplus.com/doc/tutorial/exceptions/
Your first exception seems to be happening in maze4's call to the function.