商人过河, 事实表明无论有多少商人, 只要船能载4人就够了, 且用的步骤数为 2 * n + 5

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
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值