Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I just need a pathfinder function that will work in this code below: #include #include #include #include std::vector > readIn(std::string file){ std::ifstream fin; fin.open(file); int

image text in transcribedimage text in transcribed

I just need a pathfinder function that will work in this code below:

#include

#include

#include

#include

std::vector > readIn(std::string file){

std::ifstream fin;

fin.open(file);

int row,col;

fin >> row;

fin >> col;

std::vector<:vector> > m(row, std::vector(col));

for(int i = 0; i

for(int j = 0 ; j

fin >> m[i][j];

}

}

return m;

}

std::vector > path_finder(const std::vector > & m, int row, int col, int dir, int ones, int negones) {

std::vector > path(m.size(), std::vector(m[0].size(), '-'));

if (row == m.size() - 1 && col == m[0].size() - 1) {

path[row][col] = 'x';

return path;

}

if (row >= 0 && row = 0 && col = 0 && negones >= 0)) {

if (m[row][col] == 1) {

path[row][col] = 'V';

ones--;

}

else if (m[row][col] == -1) {

path[row][col] = '^';

negones--;

}

if (dir != 1 && ones >= 0 && negones >= 0) {

auto down = path_finder(m, row + 1, col, 0, ones, negones);

if (down[m.size() - 1][m[0].size() - 1] == 'x') {

path = down;

path[row][col] = 'V';

return path;

}

}

if (dir != 0 && ones >= 0 && negones >= 0) {

auto up = path_finder(m, row - 1, col, 1, ones, negones);

if (up[m.size() - 1][m[0].size() - 1] == 'x') {

path = up;

path[row][col] = '^';

return path;

}

}

if (dir != 3 && ones >= 0 && negones >= 0) {

auto right = path_finder(m, row, col + 1, 2, ones, negones);

if (right[m.size() - 1][m[0].size() - 1] == 'x') {

path = right;

path[row][col] = '>';

return path;

}

}

if (dir != 2 && ones >= 0 && negones >= 0) {

auto left = path_finder(m, row, col - 1, 3, ones, negones);

if (left[m.size() - 1][m[0].size() - 1] == 'x') {

path = left;

path[row][col] = '

return path;

}

}

}

return path;

}

std::vector > path_finder(const std::vector > & m) {

int ones = 0;

int negones = 0;

for(int i = 0; i

for(int j = 0; j

if(m[i][j] == 1) {

ones++;

}

if(m[i][j] == -1) {

negones++;

}

}

}

path_finder(m, 0, 0, 0, ones, negones);

}

// Prints a matrix

// m: A matrix

template

void print(const std::vector > & m) {

for(int i = 0; i

for(int j = 0; j

std::cout

}

std::cout

}

std::cout

}

int main() {

std::string fileName;

std::cout

std::cin >> fileName;

std::cout

std::vector > m = readIn(fileName);

std::cout

print(m);

std::vector > path = path_finder(m);

std::cout

// print(path);

return 0;

}

- Write a program that ask the user far a file - That file contains a matrix of numbers (only 1,0 and 1 ) - The first two lines of the file will be the size of the rows and columns - You can assume the data in the file is correct - Your program should stare the values of the matrix af a vectors of int vectors. - Your program should then find a cantinuous path thought the matrix starting at the top left and moving to the botton right 0 The path must have the same number of 1 's and - 1 's. The number of zeros do nat matter - Your program should only move left (col -1), righticol +1 ), up (row -1) and down (row +1). - Your path can only use a pasitian once - If there is such a path in the matrix, that path should be printed to console, using ' A ' for down, 'n' for up, ' > ' for right, ' ' for left, ' x ' where the path ends and '-' for all other positions in the matrix. - You must write a function called path_finder to find the path thought the matrix this function needs be recursive

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions

Question

Ensure continued excellence in people management.

Answered: 1 week ago

Question

Enhance the international team by recruiting the best people.

Answered: 1 week ago