Created
August 11, 2012 02:29
-
-
Save jbush001/3320068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
static long long int positionsEvaluated = 0; | |
int inOrder(const int rack[10]) | |
{ | |
int i; | |
int last = 0; | |
for (i = 0; i < 10; i++) | |
{ | |
if (rack[i] < last) | |
return 0; | |
last = rack[i]; | |
} | |
return 1; | |
} | |
int evaluate(int deck[10], int rack[10], int depth) | |
{ | |
int index; | |
int oldCard; | |
int newScore; | |
int bestScore = 1000; | |
positionsEvaluated++; | |
if ((positionsEvaluated & 0xffffff) == 0) | |
printf("%llu\n", positionsEvaluated); | |
if (inOrder(rack)) | |
{ | |
printf("found solution, depth %d: ", depth); | |
for (index = 0; index < 10; index++) | |
printf("%d ", rack[index]); | |
printf("\n"); | |
return depth; | |
} | |
if (depth > 9) | |
return 1000; | |
for (index = 0; index < 10; index++) | |
{ | |
oldCard = rack[index]; | |
rack[index] = deck[depth]; | |
newScore = evaluate(deck, rack, depth + 1); | |
if (newScore < bestScore) | |
bestScore = newScore; | |
rack[index] = oldCard; | |
} | |
return bestScore; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
int deck[10] = { 26, 50, 58, 3, 30, 53, 43, 33, 24, 56 }; | |
int rack[10] = { 60, 32, 48, 16, 10, 20, 46, 11, 22, 8 }; | |
evaluate(deck, rack, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment