Set initial bestSolution to all INT_MAX and if all INT_MAX set

bestSolutionLength to 17
This commit is contained in:
zenon 2018-06-08 11:31:31 +02:00
parent 5398b9dccd
commit 029ce6264f

View File

@ -6,6 +6,7 @@
*/ */
#include "common.h" #include "common.h"
#include <limits.h>
static const char *pname; static const char *pname;
@ -67,7 +68,7 @@ void initCircBuf()
} }
for (int i = 0; i < MAX_ITEMS; i++) { for (int i = 0; i < MAX_ITEMS; i++) {
bestSolution[i] = -1; bestSolution[i] = INT_MAX;
} }
return; return;
} }
@ -107,20 +108,24 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace)
int newSolutionLength = 0; int newSolutionLength = 0;
// Compute length of current bestSolution // Compute length of current bestSolution
if (bestSolution[0] != -1) { if ((bestSolution[0] != -1) && (bestSolution[0] != INT_MAX)) {
for (int i = 0; i < MAX_ITEMS; i++) { for (int i = 0; i < MAX_ITEMS; i++) {
if (bestSolution[i] == -1) { if (bestSolution[i] == -1) {
bestSolutionLength = i; bestSolutionLength = i;
break; break;
} }
} }
} else if (bestSolution[0] == INT_MAX) {
bestSolutionLength = 17;
} }
// Compute length of newSolution // Compute length of newSolution
for (int i = 0; i < MAX_ITEMS; i++) { if (solution[0] != -1) {
if ((solution[i] == -1) && (i != 0)) { for (int i = 0; i < MAX_ITEMS; i++) {
newSolutionLength = i+1; if (solution[i] == -1) {
break; newSolutionLength = i;
break;
}
} }
} }