typedef struct statu_node
{
int x, y; // x is the number of trader, and y is #servant
int dir; // dir = 1 express the boat in here coast. dir = -1 is in opposition
int tst_cnt; // count number of tested in every statu.
struct statu_node *pre; // mark the previous statu.
}
s_node;
struct vector
{
int v, w; // the vector transmition of statu, that is the boat can manned.
}; // v is the manned #trader, and w is the manned #servant
/***************************************************************
* this is a problem of which describe the businessman cross river
*
****************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "sv.h"
#define PERSONS 3 // the number of traders(also servant)
#define N 5 // the number of all probably state of boat manned,
// if boat can manned two person, #state = 5
bool test(int, int, int);
void delete(void);
void addNew(int, int, int);
void FindOut(void);
s_node s_first =
{
.x = 3, .y = 3,
.tst_cnt = 0,
.dir = 1,
.pre = NULL,
};
s_node *final = NULL;
struct vector u[N] ={
{0, 1},
{0, 2},
{1, 0},
{1, 1},
{2, 0},
};
int main(void)
{
final = &s_first;
while (true)
{
/*** stop part **/
// stop and return if findout a method
if (final->x == 0 && final->y == 0)
{
FindOut();
break;
}
// all condition have be tested, but not find a good ways
else if (final->pre == NULL && final->tst_cnt == N)
{
printf("No method, well, that is you failed!!!!!!!!!!\n");
break;
}
/* continue part **/
// if a statu have not effective, then delete, and continue test the last
if (final->tst_cnt == N)
{
delete();
continue;
}
// if a new statu if effectively, then add it to list. if not, excute the next test
int trader = final->x - (final->dir) * u[final->tst_cnt].v;
int servant = final->y - (final->dir) * u[final->tst_cnt].w;
int director = -(final->dir);
if (test(trader, servant, director))
{
addNew(trader, servant, director);
continue;
}
}
return 0;
}
// print the a effective method if find it
void FindOut(void)
{
s_node *iterate = final;
while (iterate != NULL)
{
printf("(%d, %d)<-", iterate->x, iterate->y);
iterate = iterate->pre;
}
printf("Start\n");
printf("Yes, you succeed!!!!\n");
}
bool test(int t, int s, int d)
{
final->tst_cnt++; // every test, the test count plus 1;
// illegal scope, return false
if (t < 0 || t > PERSONS || s < 0 || s > PERSONS)
return false;
// legal scop.
if (t == 0 || t == s || t == PERSONS)
{
// test this statu if exit yet at forward, if it is, then jump it
s_node *iterate = final;
while (iterate != NULL)
{
if (iterate->x == t && iterate->y == s && iterate->dir == d)
return false;
iterate = iterate->pre;
}
// if not, return true
return true;
}
else
return false;
}
// delete the statu of which no effectively
void delete(void)
{
s_node *newptr = final;
final = final->pre;
free(newptr);
}
// add new statu if test return true
void addNew(int t, int s, int d)
{
s_node *newptr = malloc(sizeof(s_node));
newptr->x = t;
newptr->y = s;
newptr->dir = d;
newptr->tst_cnt = 0; // initialize the tst_cnt of new statu
newptr->pre = final;
final = newptr; // reset the final pointer to
}