Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP! I have a problem with a program I m working on . My program outputs incorrect paths and it doesn t output the expected

HELP! I have a problem with a program Im working on. My program outputs incorrect paths and it doesnt output the expected result. For example, when I input ./a.out 21235 it should output:
NNENE
NNEEN
NENNE
NENEN
NEENN
ENNEN
ENENN
Number of paths: 7
But instead it outputs:
Paths:
Number of paths: 0
Heres my code:
greedy_robot.h
#ifndef GREEDY_ROBOT_H
#define GREEDY_ROBOT_H
#include
#include
// Function to generate paths from current position to treasure
void generatePaths(int max_distance, int robot_x, int robot_y, int treasure_x, int treasure_y,
std::string pathSoFar, std::vector& result);
// Function to find paths from starting position to treasure
std::vector findPaths(int max_distance, int robot_x, int robot_y, int treasure_x, int treasure_y);
#endif // GREEDY_ROBOT_H
greedy_robot.cpp
#include
#include "greedy_robot.h"
using namespace std;
void generatePaths(int max_distance, int robot_x, int robot_y, int treasure_x, int treasure_y,
std::string pathSoFar, std::vector& result){
// Base case: you've reached the treasure with the required distance
if (robot_x == treasure_x && robot_y == treasure_y && pathSoFar.length()==2* max_distance){
result.push_back(pathSoFar);
return;
}
// Move East if robot's x-coordinate is less than treasure's x-coordinate
if (robot_x < treasure_x && pathSoFar.length()<2* max_distance){
generatePaths(max_distance, robot_x +1, robot_y, treasure_x, treasure_y, pathSoFar +'E', result);
}
// Move West if robot's x-coordinate is greater than treasure's x-coordinate
if (robot_x > treasure_x && pathSoFar.length()<2* max_distance){
generatePaths(max_distance, robot_x -1, robot_y, treasure_x, treasure_y, pathSoFar +'W', result);
}
// Move North if robot's y-coordinate is less than treasure's y-coordinate
if (robot_y < treasure_y && pathSoFar.length()<2* max_distance){
generatePaths(max_distance, robot_x, robot_y +1, treasure_x, treasure_y, pathSoFar +'N', result);
}
// Move South if robot's y-coordinate is greater than treasure's y-coordinate
if (robot_y > treasure_y && pathSoFar.length()<2* max_distance){
generatePaths(max_distance, robot_x, robot_y -1, treasure_x, treasure_y, pathSoFar +'S', result);
}
}
std::vector findPaths(int max_distance, int robot_x, int robot_y, int treasure_x, int treasure_y){
std::vector result;
generatePaths(max_distance, robot_x, robot_y, treasure_x, treasure_y,"", result);
return result;
}
main.cpp
#include
#include "greedy_robot.h"
using namespace std;
int main(int argc, char* argv[]){
// Check if correct number of command-line arguments is provided
if (argc !=6){
cerr << "Usage: "<< argv[0]<<" max_distance robot_x robot_y treasure_x treasure_y
";
return 1;
}
// Convert command-line arguments to integers
int max_distance = stoi(argv[1]);
int robot_x = stoi(argv[2]);
int robot_y = stoi(argv[3]);
int treasure_x = stoi(argv[4]);
int treasure_y = stoi(argv[5]);
// Find paths from starting position to treasure
vector paths = findPaths(max_distance, robot_x, robot_y, treasure_x, treasure_y);
// Print the paths and number of paths
cout << "Paths:
";
for (const auto& path : paths){
cout << path <<'
';
}
cout << "Number of paths: "<< paths.size()<<'
';
return 0;
}

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

Students also viewed these Databases questions

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

What attracts you about this role?

Answered: 1 week ago