Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program called coin_collection.cpp to collect maximum number of coins on an n x m board with inaccessible cells . For details, read

Write a C++ program called coin_collection.cpp to collect maximum number of coins on an n x m board with inaccessible cells. For details, read the question 5 of the textbook page 291.

Your program should ask a user for a text filename to read a 2-D array of a sequence of numbers. In the array, 1 indicates that theres a coin on the cell, while 0 means no coin. A cell with the number 2 indicates that the cell is not accessible. Your program should display maximum possible coins and path to collect it. If you have more than one path, you only need to display one. For the homework, you can assume that the board size is less than or equal to 25 x 25. When you write the program, dont forget to include Title, Abstract, ID (A four-digit number), Name, and Date.

This is a sample run of the program on the cloud9.

$ g++ -o coin_collection coin_collection.cpp

$ ./coin_collection

Enter a file name: ./t1.txt

Max coins: 3

Path:(1,1)->(2,1)->(2,2)->(3,2)->(3,3)->(4,3)->(4,4)

For the sample run, t1.txt has the following context:

4 4

0 2 1 1

0 1 2 1

0 0 1 2

1 2 0 1

The first line (= 4 and 4 in the example) indicates the numbers of rows and columns in the file. One blank space is used to delimiter the data. Note that theres no blank space at the end of each line. If your program does not read the file properly, your program will get no credit. In the sample execution, the starting indexes of the path are 1, not 0.

This is another sample run.

$ g++ -o coin_collection coin_collection.cpp

$ ./coin_collection

Enter a file name: ./t2.txt

Max coins: 1

Path:(1,1)->(1,2)->(1,3)->(1,4)->(2,4)

For the sample run, t2.txt has the following context:

2 4

0 1 0 0

1 1 2 0

How would you modify the dynamic programming algorithm for the coincollecting problem if some cells on the board are inaccessible for the robot? Apply your algorithm to the board below, where the inaccessible cells are shown by 2s. How many optimal paths are there for this board?

int F[n][m];

F[1][1] = board[1][1];

for (int j = 2; j <= m; j++) {

F[1][j] = F[1][j-1] + board[1][j];

}

for (int i = 2; i <= n; i++) {

F[i][1] = F[i-1][1] + board[i][1];

for (int j = 2; j <= m; j++) {

F[i][j] = max(F[i-1][j], F[i][j-1]) + board[i][j];

}

}

return F[n][m];

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

2. What additional techniques might be used to assess talent?

Answered: 1 week ago