void checkAdjacentCells(const char* path, matrix *m, cellIndex index, solution *sol, int currentValue) { // The base case - If we go beyond the limits of the matrix if(index.row < 0 || index.row > (m->rowCount - 1) || index.col < 0 || index.col > (m->colCount - 1)) { evaluateSolution(sol, path); return; } // check if the next cell has a lower value than the current cell int val = m->matrix[index.row][index.col]; if(val < currentValue) { char* newPath = malloc(sizeof(char) * (strlen(path) + 4 + 1 + 1)); // the 4-digit value + space + \0 strcpy(newPath, path); char *ch = malloc(sizeof(char) * (5 + 1)); // support upto 4-digit value and \0 (string terminator) if(ch == NULL) { printf("Unable to allocate memory\n"); exit(EXIT_FAILURE); } sprintf(ch, "%d ", val); strcat(newPath, ch); free(ch); int r = index.row, c = index.col; // check east index.row = r; index.col = c + 1; checkAdjacentCells((const char*) newPath, m, index, sol, val); // check west index.row = r; index.col = c - 1; checkAdjacentCells((const char*) newPath, m, index, sol, val); // check north index.row = r - 1; index.col = c; checkAdjacentCells((const char*) newPath, m, index, sol, val); // check south index.row = r + 1; index.col = c; checkAdjacentCells((const char*) newPath, m, index, sol, val); free(newPath); } // all ajacent cells have bigger value then the current cell evaluateSolution(sol, path); return; }
Cost Evaluator::evaluate() { return evaluateSolution(mSchedule.getSolutions().at(0)); }