Question: PLease rewrite this graph.h and graph.c and make sure it works and rewrite the findminturns ( ) and findMinEnergyPath ( ) functions so that it
PLease rewrite this graph.h and graph.c and make sure it works and rewrite the findminturns and findMinEnergyPath functions so that it works with graph.h and graph.c
Graph.h:
#ifndef GRAPHH
#define GRAPHH
#include "Wall.h
#include
typedef struct
int from;
int to;
int weight;
Edge;
typedef struct
struct rock rock;
Edge edges;
int numedges;
Node;
typedef struct
Node nodes;
int numnodes;
Graph;
Graph createGraphWall w int reach;
void freeGraphGraph g;
#endif
graph.c:
#include "graph.h
#include
@brief Creates and constructs a graph representation from a given Wall.
This function builds a graph where nodes represent rocks on the wall, and
edges represent potential connections between rocks within a specified reach.
@param w The Wall structure containing the rock information.
@param reach The maximum distance Manhattan distance between rocks to be considered connected.
@return A pointer to the newly created Graph structure, or NULL if allocation fails.
Graph createGraphWall w int reach
int numRocks WallNumRocksw;
Allocate memory for the graph
Graph g mallocsizeofGraph;
if g NULL return NULL; Handle allocation failure
Allocate memory for the nodes and rocks arrays
gnodes mallocnumRocks sizeofNode;
if gnodes NULL
freeg;
return NULL;
Handle allocation failure
struct rock rocks mallocnumRocks sizeofstruct rock;
if rocks NULL
freegnodes;
freeg;
return NULL;
Handle allocation failure
gnumnodes numRocks;
Populate the rocks array
WallGetAllRocksw rocks;
Initialize nodes and build edges
for int i ; i numRocks; i
gnodesirock rocksi;
gnodesinumedges ;
Allocate space for potential edges may be optimized later
gnodesiedges mallocnumRocks sizeofint;
if gnodesiedges NULL
Handle allocation failure include a loop to free previously allocated memory
for int j ; j i; j
freegnodesjedges;
freegnodes;
freeg;
return NULL;
Add edges based on distance criteria
for int j ; j numRocks; j
if i j && absrocksirow rocksjrow absrocksicol rocksjcol reach
gnodesiedgesgnodesinumedges j;
return g;
@brief Deallocates memory associated with a Graph structure.
@param g The Graph structure to be freed.
void freeGraphGraph g
Free edges for each node
for int i ; i gnumnodes; i
freegnodesiedges;
Free the nodes array and the graph itself
freegnodes;
freeg;
climber.h:
Interface to boulder climbing algorithms
DO NOT MODIFY THIS FILE
#ifndef CLIMBERH
#define CLIMBERH
#include "Wall.h
struct path
int numRocks;
struct rock rocks;
;
struct path findShortestPathWall w int reach, Colour colour;
struct path findMinEnergyPathWall w int reach, int energyCostsNUMCOLOURS;
struct path findMinTurnsPathWall w int reach, int energyCostsNUMCOLOURS
int maxEnergy;
#endif
climber.c:
Implementation of boulder climbing algorithms
#include
#include
#include
#include
#include
#include
#include "climber.h
#include "Wall.h
#include "graph.h
#include "dijkstra.h
struct path findShortestPathWall w int reach, Colour colour
int height WallHeightw;
int width WallWidthw;
Initialise an array to store the shortest distance to reach each rock
int dist intmallocheight sizeofint;
for int i ; i height; i
distiintmallocwidth sizeofint;
for int j ; j width; j
distij INTMAX; Initialise distances to infinity
Initialise a queue for BFS
int queueSize height width;
int queue intmallocqueueSize sizeofint;
int front rear ;
Find all starting rocks at most 'reach' units above the ground
for int j ; j width; j
if WallGetRockColourw height j colour
queuerearheight width j;
distheight j; distance from the bottom row is
Define possible movements
int dx; up right left
int dy;
while front rear
int curr queuefront;
int row curr width;
int col curr width;
check adjacent rocks
for int i ; i ; i
for int j ; j ; j
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
