You are on page 1of 4

/*

* INSTRUCTION:
* This is a C++ starting code for hw6_1.
* When you finish the development, download this file.
* Note that the current filename is "main.cpp".
* But rename it to "main_hw6_1.cpp".
* After that, upload the renamed file on Canvas.
*/

// Finish the head comment with Abstract, Name, and Date.


/*
* Title: main_hw6_1.cpp
* Abstract: This program traverses a 2D matrix to collect coins
* and returns the most optimum path from (1,1) to (i,j);
* Name: Nina Khuu
* Date: 02/21/2023
*/

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

//Function to print matrix


void displayMatrix(vector<vector<int> > v);

int main()
{
int rows, columns;

//Receives number of rows and columns from the user


cin >> rows >> columns;

vector<vector<int> > inputBoard; //Matrix to keep track of input from


user
vector<vector<int> > maxCoinBoard; //Matrix to keep track of maximum
coins per square

//Receives input board from the user


for(int i = 0; i < rows; i++)
{
vector<int> rowInput;

for(int j = 0; j < columns; j++)


{
int n;
cin >> n;
rowInput.push_back(n);

}
inputBoard.push_back(rowInput);
}

//Keep track of coins from first row


vector<int> firstRowCoins;

//Check coins on first row


for(int i = 0; i < columns; i++)
{
if(i==0)
{
//Index[0][0] on max coin board should contain same value from
input board
firstRowCoins.push_back(inputBoard[0][0]);
}
else
{
//Check each index after and add value from left index and current
index of input board
firstRowCoins.push_back( firstRowCoins.back() + inputBoard[0][i]);
}
}

//Push the first row to max coin board


maxCoinBoard.push_back(firstRowCoins);

//Check remaining rows


for(int i = 1; i < inputBoard.size(); i++)
{
vector<int> rowValues;

for(int j = 0; j < inputBoard[i].size(); j++)


{
if(j==0)
{
//To check values in first column, compare top to value of
0 and add coin if there is one.
rowValues.push_back(max(maxCoinBoard[i-1][j], 0) +
inputBoard[i][j]);
}
else
{
//Check remaining columns
rowValues.push_back(max(maxCoinBoard[i-1][j],
rowValues.back())+ inputBoard[i][j]);

}
}
//Push each row to max coin board
maxCoinBoard.push_back(rowValues);
}

//Max coin is the number in destination index


int maxCoins = maxCoinBoard[rows-1][columns-1];

//Print out max coins


cout << "Max coins:"<<maxCoins << endl;

//Set current index pair as destination index


pair<int, int> currentIndex(rows-1, columns-1);

//Create vector to keep track of optimal path


vector<pair<int, int> > optimalPath;

//Add destination index for optimal path


optimalPath.push_back(currentIndex);

do{
//If the row is 0 then set the next index to the left square
if(currentIndex.first == 0)
{
currentIndex.first = currentIndex.first;
currentIndex.second = currentIndex.second-1;
}
//If the column is 0 or if top index is greater than left index, set next
index to top square
else if(currentIndex.second == 0 || maxCoinBoard[currentIndex.first-1]
[currentIndex.second] > maxCoinBoard[currentIndex.first][currentIndex.second-1] )
{
currentIndex.first = currentIndex.first-1;
currentIndex.second = currentIndex.second;
}
else
{
currentIndex.first = currentIndex.first;
currentIndex.second = currentIndex.second-1;
}

//Insert the index pair into beginning of optimal path vector


optimalPath.insert(optimalPath.begin(), currentIndex);

}while(currentIndex.first != 0 || currentIndex.second != 0); //Do until index


is [0][0]

//Print out optimal path


cout << "Path:";
for(int i = 0; i < optimalPath.size(); i++)
{
if(i==optimalPath.size()-1)
{
cout << "("<<optimalPath[i].first + 1 << "," << optimalPath[i].second + 1
<<")";
}
else
{
cout << "("<<optimalPath[i].first + 1 << "," << optimalPath[i].second + 1
<<")";
cout << "->";
}
}

cout << endl;


return 0;
}

void displayMatrix(vector<vector<int> > v)


{
cout << endl;
for(int i = 0; i < v.size(); i++)
{
for(int j =0; j < v[i].size(); j++)
{
cout << v[i][j] << " ";
}
cout << endl;
}
}

You might also like