0% found this document useful (0 votes)
4 views3 pages

CS3353 4

Uploaded by

engrkumailabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

CS3353 4

Uploaded by

engrkumailabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS3353: Data Structures and Algorithm Analysis I Fall 2024

Homework #4

Code

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

void findPath(vector<vector<int>>& matrix, int i, int j, vector<int>& path)


{

int M = matrix.size();
int N = matrix[0].size();
path.push_back(matrix[i][j]);

if(i == M-1 && j == N-1 ){


int k = 0;
cout<< "[";
while(k < path.size() - 1){
cout << path[k] << ", ";
k++;
}
cout << path[k] ;
cout<< "]";
cout << endl;
path.pop_back();
return;
}

if(j < N - 1){


findPath(matrix, i, j+1, path); //move right
}
if(i < M - 1 && j < N - 1){
findPath(matrix, i+1, j+1, path); //move down right
}
if(i < M - 1){
findPath(matrix, i+1, j, path); //move down
}
path.pop_back();
}
int main()
{
int x_axis = 3, y_axis = 3;
int opt;

while (true) {

cout << "M E N U\n";


cout << "Horizontal Axis (0), Vertical Axis (1), Start Discovery (2), Exit
Program (3)\nChoose? ";

cin >> opt;

switch (opt) {
case 0:
cout << "Enter new Horizontal Axis size: ";
cin >> x_axis;
break;
case 1:
cout << "Enter new Vertical Axis size: ";
cin >> y_axis
;
break;
case 2: {

vector<vector<int>> matrix(x_axis, vector<int>(y_axis));


int count = 1;
vector<int> path;
int i = 0;
while( i < x_axis) {
int j=0;
while (j < y_axis) {
matrix[i][j] = count++;
++j;
}
++i;
}

cout << "Paths from (0,0) to (" << x_axis - 1 << "," << y_axis -
1 << "):\n";
findPath(matrix, 0, 0, path);
break;
}
case 3:
cout << "Quitting Program...\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

SELF TESTING

You might also like