#include <stdio.h>
#include <stdlib.h>
#define N 10
#define C 10
typedef struct node
{
int n;
int choose;
int cp;
int rp;
int weight;
struct node*formorNode;
struct node *nextNode;
} Node;
void solveProblem(int bestp,Node *pNode,Node *qNode,int *v,int *w,int *a);
int main()
{
int w[N];
int v[N];
int rp;
Node *hNode;
hNode = (Node *)malloc(sizeof(Node));
hNode->choose = -1;
hNode->cp = 0;
hNode->weight = 0;
hNode->nextNode = NULL;
hNode->formorNode = NULL;
hNode->n = 0;
int i;
int a[N];
for(i = 0 ; i < N ; i++)
{
v[i] = rand()%5;
w[i] = rand()%5;
a[i] = 0;
rp += v[i];
}
hNode->rp = rp;
Node *node;
node = hNode;
solveProblem(0,hNode,hNode,v,w,a);
for(i = 0 ; i < N ; i++)
printf("%d ",w[i]);
printf("\n");
for(i = 0 ; i < N ; i++)
{
printf("%d ",a[i]);
}
return 0;
}
void solveProblem(int bestp,Node *pNode,Node *qNode,int *v,int *w,int *a)
{
Node *p = pNode;
Node *q = qNode;
if(p->cp+p->rp > bestp)
{
int n = p->n;
if(n < N)
{
q->nextNode = (Node *)malloc(sizeof(Node));
q = q->nextNode;
q->n = p->n+1;
q->choose = 1;
q->cp = p->cp + v[n];
q->formorNode = p;
q->nextNode = NULL;
q->rp = p->rp - v[n];
q->weight = p->weight + w[n];
if(q->cp > bestp)
{
bestp = q->cp;
Node *node = q;
int i;
for(i = n ; i >= 0 ; i--)
{
a[i] = node->choose;
node = node->formorNode;
}
}
}
q->nextNode = (Node *)malloc(sizeof(Node));
q = q->nextNode;
q->n = n+1;
q->choose = 0;
q->cp = p->cp;
q->formorNode = p;
q->nextNode = NULL;
q->rp = p->rp - v[n];
q->weight = p->weight;
}
if(p->nextNode != NULL)
{
solveProblem(bestp,p->nextNode,q,v,w,a);
}
}
}
#include <stdlib.h>
#define N 10
#define C 10
typedef struct node
{
int n;
int choose;
int cp;
int rp;
int weight;
struct node*formorNode;
struct node *nextNode;
} Node;
void solveProblem(int bestp,Node *pNode,Node *qNode,int *v,int *w,int *a);
int main()
{
int w[N];
int v[N];
int rp;
Node *hNode;
hNode = (Node *)malloc(sizeof(Node));
hNode->choose = -1;
hNode->cp = 0;
hNode->weight = 0;
hNode->nextNode = NULL;
hNode->formorNode = NULL;
hNode->n = 0;
int i;
int a[N];
for(i = 0 ; i < N ; i++)
{
v[i] = rand()%5;
w[i] = rand()%5;
a[i] = 0;
rp += v[i];
}
hNode->rp = rp;
Node *node;
node = hNode;
solveProblem(0,hNode,hNode,v,w,a);
for(i = 0 ; i < N ; i++)
printf("%d ",w[i]);
printf("\n");
for(i = 0 ; i < N ; i++)
{
printf("%d ",a[i]);
}
return 0;
}
void solveProblem(int bestp,Node *pNode,Node *qNode,int *v,int *w,int *a)
{
Node *p = pNode;
Node *q = qNode;
if(p->cp+p->rp > bestp)
{
int n = p->n;
if(n < N)
{
//是否可以将物品放进去
{
q->nextNode = (Node *)malloc(sizeof(Node));
q = q->nextNode;
q->n = p->n+1;
q->choose = 1;
q->cp = p->cp + v[n];
q->formorNode = p;
q->nextNode = NULL;
q->rp = p->rp - v[n];
q->weight = p->weight + w[n];
if(q->cp > bestp)
{
bestp = q->cp;
Node *node = q;
int i;
for(i = n ; i >= 0 ; i--)
{
a[i] = node->choose;
node = node->formorNode;
}
}
}
q->nextNode = (Node *)malloc(sizeof(Node));
q = q->nextNode;
q->n = n+1;
q->choose = 0;
q->cp = p->cp;
q->formorNode = p;
q->nextNode = NULL;
q->rp = p->rp - v[n];
q->weight = p->weight;
}
if(p->nextNode != NULL)
{
solveProblem(bestp,p->nextNode,q,v,w,a);
}
}
}