started migration stack->tree
This commit is contained in:
100
best.c
Normal file
100
best.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <values.h>
|
||||
#include "lceb.h"
|
||||
|
||||
static int target=0;
|
||||
//static int best=MAXINT;
|
||||
static int bestdiff=MAXINT;
|
||||
static int bestops=MAXINT;
|
||||
|
||||
static BEST bests[1024]; /* TODO: should be dynamic */
|
||||
static int nbests=0;
|
||||
|
||||
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
|
||||
|
||||
void set_target(n)
|
||||
int n;
|
||||
{
|
||||
target=n;
|
||||
}
|
||||
|
||||
int check_best(res, nops, node, values, ops)
|
||||
int res;
|
||||
int nops;
|
||||
NODE *node;
|
||||
int *values;
|
||||
char *ops;
|
||||
{
|
||||
int diff, found=0;
|
||||
|
||||
diff=DIFF(target, res);
|
||||
# ifdef DEBUG
|
||||
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||
# endif
|
||||
if (diff < bestdiff || (diff == bestdiff && nops < bestops)) {
|
||||
# ifdef DEBUG
|
||||
printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||
printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
|
||||
# endif
|
||||
//best=res;
|
||||
bestdiff=diff;
|
||||
bestops=nops;
|
||||
nbests=0;
|
||||
found=1;
|
||||
//return 1;
|
||||
} else if (diff == bestdiff && nops == bestops) {
|
||||
# ifdef DEBUG
|
||||
printf("NEW BEST SOLUTION (%d): res=%d diff=%d nops=%d\n", nbests+1, res, diff, nops);
|
||||
# endif
|
||||
found=1;
|
||||
}
|
||||
if (found) {
|
||||
bests[nbests].res=res;
|
||||
bests[nbests].diff=diff;
|
||||
bests[nbests].oper=ops;
|
||||
bests[nbests].root=node;
|
||||
bests[nbests].values=values;
|
||||
nbests++;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_best(node, values, pops, depth)
|
||||
NODE *node;
|
||||
int *values;
|
||||
char *pops;
|
||||
int depth;
|
||||
{
|
||||
static int *vals;
|
||||
static char *ops;
|
||||
|
||||
if (depth == 0) {
|
||||
vals=values;
|
||||
ops=pops;
|
||||
}
|
||||
if (node->type == TREE_LEAF) {
|
||||
printf("%d ", *vals);
|
||||
vals++;
|
||||
} else {
|
||||
print_best(node->left, vals, ops, depth+1);
|
||||
print_best(node->right, vals, ops, depth+1);
|
||||
printf(" %c ", *ops);
|
||||
ops++;
|
||||
}
|
||||
if (depth == 0)
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void print_bests()
|
||||
{
|
||||
int i;
|
||||
printf("BESTS: diff=%d solutions=%d nops=%d\n", bestdiff, nbests, bestops);
|
||||
for (i=0; i<nbests; ++i) {
|
||||
printf("res = %d : ", bests[i].res);
|
||||
print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user