农夫过河问题

题目描述
有一个农夫带一只羊、一筐菜和一只狼过河。如果没有农夫看管,则狼要吃羊,羊要吃菜。但是船很小,只够农夫带一样东西过河。问农夫该如何解此难题?

我的代码:

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<vector>
#include<queue>
#include<set>


using namespace std;

typedef struct state{
	int a[4];			//河的本岸 a[0],a[1],a[2].a[3]分别代表本岸人、羊、菜、狼的
	int b[4];			//河的对岸
	int last_state;
}State;

queue<State> states;
bool is_used[16];
State history[16];
int state_num(State a)
{
	int num = a.a[0] * 8 + a.a[1] * 4 + a.a[2] * 2 + a.a[3] * 1;
	return num;
}

bool check_feasiblity(State a)
{
	if (a.a[0] == 1)			//人在河的本岸
	{
		if ((a.a[1] == 0 && a.a[2] == 0) || (a.a[1] == 0 && a.a[3] == 0))
		{
			return false;
		}

	}
	else     //人在河的对岸
	{
		if ((a.a[1] == 1 && a.a[2] == 1) || (a.a[1] == 1 && a.a[3] == 1))
		{
			return false;
		}
	}
	return true;
}

void print_routine(int state_num)
{
	int last_state_num = history[state_num].last_state;
	if (last_state_num!=-1)
	{
		print_routine(last_state_num);
		State last_state = history[last_state_num];
		State current_state = history[state_num];
		if (current_state.a[0] == 1)
		{
			if (current_state.a[1] == 1 && last_state.a[1] == 0)
			{
				cout << "sheep_come" << endl;
			}
			else if (current_state.a[2] == 1 && last_state.a[2] == 0)
			{
				cout << "vegetable_come" << endl;
			}
			else if (current_state.a[3] == 1 && last_state.a[3] == 0)
			{
				cout << "wolf_come" << endl;
			}
			else
			{
				cout << "nothing_come" << endl;
			}
		}
		else		
		{
			if (current_state.a[1] == 0 && last_state.a[1] == 1)
			{
				cout << "sheep_go" << endl;
			}
			else if (current_state.a[2] == 0 && last_state.a[2] == 1)
			{
				cout << "vegetable_go" << endl;
			}
			else if (current_state.a[3] == 0 && last_state.a[3] == 1)
			{
				cout << "wolf_go" << endl;
			}
			else
			{
				cout << "nothing_go" << endl;
			}
		}
	}
}
int main()
{
	memset(is_used, false, sizeof(is_used));
	State initial_state;		
	initial_state.a[0] = initial_state.a[1] = initial_state.a[2] = initial_state.a[3] = 1;
	initial_state.b[0] = initial_state.b[1] = initial_state.b[2] = initial_state.b[3] = 0;
	initial_state.last_state = -1;
	states.push(initial_state);
	is_used[state_num(initial_state)] = true;
	history[state_num(initial_state)] = initial_state;
	while (!states.empty())
	{
		State current_state = states.front();
		int current_num = state_num(current_state);
		int last_num = current_state.last_state;
		history[current_num] = current_state;
		is_used[current_num] = true;
		if (current_num == 0)
		{
			
			print_routine(0);
			cout << "succeed" << endl;
			break;
		}
		states.pop();	
		if (current_state.a[0] == 1)				//人在河的本岸
		{
			current_state.a[0] = 0;
			if (!is_used[state_num(current_state)]&&check_feasiblity(current_state))
			{
				current_state.last_state = current_num;
				states.push(current_state);
				//恢复
				current_state.last_state = last_num;
			}
			current_state.a[0] = 1;
			for (int i = 1; i <= 3; i++)
			{	
				if (current_state.a[i] == 1)
				{
					current_state.a[0] = 0;
					current_state.a[i] = 0;
					if (!is_used[state_num(current_state)] && check_feasiblity(current_state))
					{
						current_state.last_state = current_num;
						states.push(current_state);
						//恢复
						current_state.last_state = last_num;
					}
					current_state.a[0] = 1;
					current_state.a[i] = 1;
				}
			}

		}
		else                                   //人在河的对岸
		{
			current_state.a[0] = 1;
			if (!is_used[state_num(current_state)] && check_feasiblity(current_state))
			{
				current_state.last_state = current_num;
				states.push(current_state);
				//恢复
				current_state.last_state = last_num;
			}
			current_state.a[0] = 0;
			for (int i = 1; i <= 3; i++)
			{
				if (current_state.a[i] == 0)
				{
					current_state.a[0] = 1;
					current_state.a[i] = 1;
					if (!is_used[state_num(current_state)] && check_feasiblity(current_state))
					{
						current_state.last_state = current_num;
						states.push(current_state);
						//恢复
						current_state.last_state = last_num;
					}
					current_state.a[0] = 0;
					current_state.a[i] = 0;
				}
			}

		}
	}
	return 0;
}

注:
1.还没有完全解决,现在只能输出一种的
2.写的太复杂了

心得:queue队列的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值