Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ Q1 Consider the Collecting Apples problem given in class, rewrite the solution such that moving from start to end includes three types of moves:

c++

Q1

Consider the Collecting Apples problem given in class, rewrite the solution such that moving from start to end includes three types of moves:

Right, Down, Right-Down. The Right-down move means that you can move diagonally.

Also, black apples indicate that these are blocked and cannot be crossed as part of the path from start to end. The blocked cells are represented by negative values in the input.

Write the function CountingApples that takes as input a 2D array apples, and the values of two integers NN and MM (2\leq N,\ 2\leq M)2N, 2M). NN represents the number of rows, and MM represents the number of columns in the given apples 2D array.

You will not be asked to read the values, the apples 2D array will be ready automatically and fed to your CountingApples function.

If there is no path from start to end, the maximum number of collected apples will be 0. Also, if the start cell is a blocked one, there is no solution and the maximum number of collected apples will be 0 in this case as well.

Output

Your function should return the maximum number of apples that can be collected.

Note:

The program will be automatically tested by calling your CountingApples(int** apples, int N, int M) function. You are not allowed to change the signature of this function. Inside the function, you can use apples as a 2D array. You can define additional functions if you need. The function should return the maximum number of apples that can be collected.

Example 1:

N=4 M=5 10 20 30 40 50 5 5 5 5 5 1 1 1 1 1 3 3 3 3 3

Result is 159

Explanation: it will select the path of going the first row and last column

Example 2:

N=4 M=5 4 -1 30 40 50 1 -1 5 5 5 1 -1 1 1 1 1 1 1 1 1

Result is 11

Explanation: Since some cells are blocked, it will select the path of going the first column down and then the last row left to right

#include #include using namespace std; void Create2DArray(int** &data, int R, int C) { data = new int* [R]; for (int i = 0; i < R; i++) data[i] = new int[C]; }

int CountingApples(int ** apples, int N, int M){ }

#include using namespace std; #include "Q1.cpp"

int main(){

int N, M;

int** apples=NULL; // Dynamic 2D array

int example[4][5] = {{1, 10, 3 ,1 ,1}, {2, 1, 7 ,2 ,3}, {22 ,11 ,11, 5, 4},{3, 50, 8, 9, 1}}; // this example was covered in class N=4; M=5; Create2DArray(apples, N , M ); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) apples[i][j]=example[i][j];

cout <

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

What is the relationship between humans?

Answered: 1 week ago

Question

What is the orientation toward time?

Answered: 1 week ago