Need help determining if vector does not have one more element than previous vector

I have an assignment where I have to write a function that will give me the max amount of points possible, from the top of the pyramid to the bottom. It should return 0 for empty vector arrays, arrays with any empty vectors, and if any row in the pyramid does not have one more element than the row preceding it.

What I have mostly works but I keep getting an error with maze5. { ::operator delete(__p); }

// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <assert.h>

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] += std::max(maze[i-1][j], maze[i-1][j+1]);
                 }

                 else
                    return 0;
             }
                }
           return maze[0][0];

     }

};

int main()
{
    CIS14 cis14;
    vector<vector<int>> maze1 = {{2}, {4,1}, {5,3,8}, {1,6,7,3}};
    vector<vector<int>> maze2 = {{2}, {4,1}, {0,0,0}, {0,0,0,0}};
    vector<vector<int>> maze3 = {};
    vector<vector<int>> maze4 = {{}, {}, {}, {}};
    vector<vector<int>> maze5 = {{2}, {4,1,5}, {5,3}, {1,6,7,3}};
    cout << cis14.getMaxPoints(maze1) << endl;
    cout << cis14.getMaxPoints(maze2) << endl;
    cout << cis14.getMaxPoints(maze3) << endl;
    cout << cis14.getMaxPoints(maze4) << endl;
    cout << cis14.getMaxPoints(maze5) << endl;
    return 0;
}
Last edited on
In several places you access maze[i-2], but i is allowed to take values less than 2, so sometimes you will try to access maze[-1].
Additionally, you access maze[etc][j] without ensuring that j < maze[etc].size().
Topic archived. No new replies allowed.