当前位置:   article > 正文

农夫过河问题_农夫过河问题:有有农夫带一只狼、一只羊和一筐菜欲从河的左岸乘船到右岸,但受下列

农夫过河问题:有有农夫带一只狼、一只羊和一筐菜欲从河的左岸乘船到右岸,但受下列

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

我的代码:

#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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182

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

心得:queue队列的使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/378646
推荐阅读
相关标签
  

闽ICP备14008679号