Repeating older question

Ive asked this question before although now I know a little more. So to restate what I asked.

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?

Now I know that this is NOT a result of going to an out-of-bounds index in the maze. So can anyone tell me why it fails? Recall it still actually outputs the correct answer it just also crashes.

Once again this site doesnt allow me to format the code so sorry in advance.

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
54
55
56
57
58
59
60
#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]);
}

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}, {1,2,3,4,5}, {1,2,3,4,5,6}};
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
closed account (SECMoG1T)
hello @donda97

1
2
3
4
5
6
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();///here, what happens when i==1? seems like you still out of bounds 
Last edited on
Woops sorry. It can be written like this

1
2
3
4
5
6
for (int i = maze.size(); i >= 2; --i)
{
for (int j = 0; j < (i - 1); ++j)
{
int x = maze[i-1].size();
int y = maze[i-2].size();


But still doesnt work. Well I mean it works it just crashes.
Last edited on
closed account (SECMoG1T)
hey i run your code on my IDE and it works fine c++17, i have also run it on two online compilers and it works fine except for one warning .


warning: reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false [-Wtautological-undefined-compare]
                if (&maze[i-2][j]==nullptr)


check the results here
http://cpp.sh/2prn6
http://coliru.stacked-crooked.com/a/e03495d212838acd
Change
for (int j = 0; j < (i - 1); ++j)
to
for (int j = 0; j < maze[i-1].size()-1; ++j)
or line 33 may crash on maze[i-1][j+1].

It might help if you dealt with i rather than i-1 throughout.

To be honest, I haven't a clue what you are doing with pointers:
1
2
3
4
if (&maze[i-2][j]==nullptr)
{
return 0;
}

and your code can never reach line 37.


Why exactly does this routine need to go in a class?
Last edited on
Thanks Yolanda for the help. I'm still on C++14 so dont know if that was the issue.
And thanks lastchance, you saying "your code can never reach line 37" actually helped me solve it.
Topic archived. No new replies allowed.