Question: struct path findMinEnergyPath ( Wall w , int reach, int energyCosts [ NUM _ COLOURS ] ) { struct path p = { 0 ,

struct path findMinEnergyPath(Wall w, int reach, int energyCosts[NUM_COLOURS]){
struct path p ={0, NULL};
int height = WallHeight(w);
int width = WallWidth(w);
struct rock *rocks = calloc(height * width, sizeof(struct rock));
int numOfRocks = WallGetAllRocks(w, rocks);
bool **explored = calloc(height, sizeof(bool*));
for (int r =0; r < height; r++)
explored[r]= calloc(width, sizeof(bool));
struct rock ***prevRock = calloc(height, sizeof(struct rock**));
for (int r =0; r < height; r++)
prevRock[r]= calloc(width, sizeof(struct rock*));
PQ pq = PQNew();
for (int i =0; i < numOfRocks; i++){
if (rocks[i].row <= reach){
Edge e ={rocks[i].row, rocks[i].col, energyCosts[rocks[i].colour], rocks[i].colour};
PQInsert(pq, e);
explored[rocks[i].row][rocks[i].col]= true;
}
}
while (!PQIsEmpty(pq)){
Edge top = PQExtract(pq);
struct rock *cur = &rocks[top.row * width + top.col];
if (height <= cur->row + reach){
struct rock **revPath = calloc(height * width, sizeof(struct rock*));
while (cur){
*revPath[p.numRocks++]=*cur;
cur = prevRock[cur->row][cur->col];
}
p.rocks = calloc(p.numRocks, sizeof(struct rock));
for (int i =0, j = p.numRocks -1; i < p.numRocks; i++, j--){
p.rocks[i]=*revPath[j];
}
free(revPath);
break;
}
for (int i =0; i < numOfRocks; i++){
int r = rocks[i].row, c = rocks[i].col, colour = rocks[i].colour;
if (explored[r][c])
continue;
int dr = abs(r - cur->row), dc = abs(c - cur->col);
if (dr > reach || dc > reach)
continue;
Edge newEdge;
newEdge.weight = top.weight + energyCosts[colour]; // Store energy as weight
newEdge.row = i; // Use row to store the index to rocks
newEdge.col = c; // Optionally, store actual column for reference
newEdge.colour = colour; // Store colour for additional data
PQInsert(pq, newEdge);
explored[r][c]= true;
prevRock[r][c]= cur;
}
}
// Clean up memory
free(rocks);
for (int r =0; r < height; r++){
free(explored[r]);
free(prevRock[r]);
}
free(explored);
free(prevRock);
PQFree(pq);
return p;
}
//////////////////////////////////////////////////////////////////
// Interface to the Wall ADT
//!!! DO NOT MODIFY THIS FILE !!!
#ifndef WALL_H
#define WALL_H
typedef struct wall *Wall;
// Rock colours
#define NUM_COLOURS 4
// enums are used to create groups of related constants. It is like
// creating multiple #defines, except you can also provide a type name.
typedef enum {
NONE =-1,
GREEN =0,
TEAL =1,
PINK =2,
RED =3,
} Colour;
// Terminal output colours
#define GRN "\x1B[32m"
#define CYN "\x1B[96m"
#define MAG "\x1B[95m"
#define RD "\x1B[91m"
#define RESET "\x1B[0m"
struct rock {
int row;
int col;
Colour colour;
};
Wall WallNew(int height, int width);
void WallFree(Wall w);
int WallHeight(Wall w);
int WallWidth(Wall w);
void WallAddRock(Wall w, struct rock rock);
int WallNumRocks(Wall w);
Colour WallGetRockColour(Wall w, int row, int col);
int WallGetAllRocks(Wall w, struct rock rocks[]);
int WallGetRocksInRange(Wall w, int row, int col, int dist,
struct rock rocks[]);
int WallGetColouredRocksInRange(Wall w, int row, int col, int dist,
Colour colour, struct rock rocks[]);
void WallPrint(Wall w);
#endif
/////////////////////////////////////////////
// Priority queue of edges
// Edges with smaller weight have higher priority
//!!! DO NOT MODIFY THIS FILE !!!
#ifndef PQ_H
#define PQ_H
#include
#include "Wall.h"
typedef struct PQRep *PQ;
typedef struct Edge {
int row;
int col;
int weight;
Colour colour;
} Edge;
PQ PQNew(void);
void PQFree(PQ pq);
void PQInsert(PQ pq, Edge e);
Edge PQExtract(PQ pq);
bool PQIsEmpty(PQ pq);
#endif
please give the IMPLEMENTATION of the fixed findMinEnergyPath, currently it returns "no path found!"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!