农夫过河问题

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

我的代码:

#include
#include
#include
#include
#include
#include
#include


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队列的使用

你可能感兴趣的:(牛客网机试)