Schrieffer.zsy 2021-03-11 23:07 采纳率: 93.8%
浏览 84
已结题

C++::我想知道为什么这个程序什么也不显示,是卡在哪里了?

#include<iostream>
#include<ctime>
#include<set>
using namespace std;
 
 
struct state
{
	//1表示存在,0表示不在 
	int exi_man;
	int exi_sheep;
	int exi_wolf;
	int exi_veget;
	state(){}
	state(int a,int b,int c,int d)
	{
		exi_man = a;
		exi_sheep = b;
		exi_wolf = c;
		exi_veget = d;
	}
 
	bool operator==(state a)
	{
		return a.exi_man == exi_man && a.exi_sheep == exi_sheep && a.exi_veget == exi_veget && a.exi_wolf == exi_wolf;
	}
 
	void print_state()
	{
		if (exi_man) cout << "人";
		if (exi_sheep) cout << "羊";
		if (exi_wolf) cout << "狼";
		if (exi_veget) cout << "菜";
	}
	bool check_state()
	{
		bool temp= (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 1)
			    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 0)
 
			    || (exi_man == 0 && exi_sheep == 1 && exi_wolf == 0 && exi_veget == 1)
		   	    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 1 && exi_veget == 0)
 
			    || (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 0)
			    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 1);
		return !temp;
	}
 
	void move_in(state a)
	{
		exi_man += a.exi_man;
		exi_sheep += a.exi_sheep;
		exi_wolf += a.exi_wolf;
		exi_veget += a.exi_veget;
	}
	void move_out(state a)
	{
		exi_man -= a.exi_man;
		exi_sheep -= a.exi_sheep;
		exi_wolf -= a.exi_wolf;
		exi_veget -= a.exi_veget;
	}
 
}_state(1,1,1,1);
 
//共有4种决策:啥也不带,带羊,带狼,带菜
state choice[4] = { {1,0,0,0} ,{1,1,0,0}, {1,0,1,0},{1,0,0,1} };
int record[100] = { -1 };
 
void print_choice(state a)
{
	if (a == choice[0])  cout << "空气 ";
	if (a == choice[1])  cout << "羊 ";
	if (a == choice[2])  cout << "狼 ";
	if (a == choice[3])  cout << "菜 ";
}
 
int main()
{
 
	int num_step = 0;
	while (true)
	{
		state temp = _state;
 
		if (num_step % 2 == 0)
		{
			int i = 0;
		flag1:
			temp = _state;
			temp.move_out(choice[i]);
			if (temp.exi_man < 0 || temp.exi_man > 1 || 
				temp.exi_sheep < 0 || temp.exi_sheep > 1 || 
				temp.exi_wolf < 0 || temp.exi_wolf > 1 || 
				temp.exi_veget < 0 || temp.exi_veget > 1||
				!temp.check_state()) {
				i++;
			}
			if (temp.check_state()) {
				record[num_step] = i;
				_state = temp;
				goto flag;
			}
			goto flag1;
		}
		else
		{
			int i = 0;
		flag2:
			temp = _state;
			temp.move_in(choice[i]);
			if (temp.exi_man < 0 || temp.exi_man > 1 || 
				temp.exi_sheep < 0 || temp.exi_sheep > 1 || 
				temp.exi_wolf < 0 || temp.exi_wolf > 1 || 
				temp.exi_veget < 0 || temp.exi_veget > 1|| 
				!temp.check_state()) {
				i++;
			}
			if (temp.check_state()) {
				record[num_step] = i;
				_state = temp;
				goto flag;
			}
			goto flag2;
		}
	flag:
		num_step++;
		if (_state.exi_man == 0 && _state.exi_sheep == 0 && _state.exi_wolf == 0 && _state.exi_veget == 0)
		{
			cout << "共计:" << num_step << "步" << endl;
			for (int i = 0; i < num_step; i++) {
				if (i % 2 == 0){
					cout << "船夫带走了";
					print_choice(choice[i]);
					cout << endl;
				}
				else {
					cout << "船夫带回了";
					print_choice(choice[i]);
					cout << endl;
				}
				cout << "搬运成功!" << endl;
				system("pause");
			}
			break;
		}
	}
}
#include<iostream>
#include<ctime>
#include<set>
using namespace std;



struct state
{
	//1表示存在,0表示不在 
	int exi_man;
	int exi_sheep;
	int exi_wolf;
	int exi_veget;
	state(){}
	state(int a,int b,int c,int d)
	{
		exi_man = a;
		exi_sheep = b;
		exi_wolf = c;
		exi_veget = d;
	}

	bool operator==(state a)
	{
		return a.exi_man == exi_man && a.exi_sheep == exi_sheep && a.exi_veget == exi_veget && a.exi_wolf == exi_wolf;
	}

	void print_state()
	{
		if (exi_man) cout << "人";
		if (exi_sheep) cout << "羊";
		if (exi_wolf) cout << "狼";
		if (exi_veget) cout << "菜";
	}
	bool check_state()
	{
		bool temp= (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 1)
			    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 0)

			    || (exi_man == 0 && exi_sheep == 1 && exi_wolf == 0 && exi_veget == 1)
		   	    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 1 && exi_veget == 0)

			    || (exi_man == 0 && exi_sheep == 1 && exi_wolf == 1 && exi_veget == 0)
			    || (exi_man == 1 && exi_sheep == 0 && exi_wolf == 0 && exi_veget == 1);
		return !temp;
	}

	void move_in(state a)//这里的a只代入choice[i]
	{
		exi_man += a.exi_man;
		exi_sheep += a.exi_sheep;
		exi_wolf += a.exi_wolf;
		exi_veget += a.exi_veget;
	}
	void move_out(state a)//这里的a只代入choice[i]
	{
		exi_man -= a.exi_man;
		exi_sheep -= a.exi_sheep;
		exi_wolf -= a.exi_wolf;
		exi_veget -= a.exi_veget;
	}

}_state(1,1,1,1);

//共有4种决策:啥也不带,带羊,带狼,带菜
state choice[4] = { {1,0,0,0} ,{1,1,0,0}, {1,0,1,0},{1,0,0,1} };
//如果正确合规,用以记录决策代号
int record[20] = { -1 };

void print_choice(state a)
{
	if (a == choice[0])  cout << "空气 ";
	if (a == choice[1])  cout << "羊 ";
	if (a == choice[2])  cout << "狼 ";
	if (a == choice[3])  cout << "菜 ";
}

int main()
{

	int num_step = 0;

	while (true)
	{
		state temp = _state;
		if (num_step % 2 == 0)
		{
			int i = 0;
		flag1:
			temp = _state;
			temp.move_out(choice[i]);
			if (temp.exi_man < 0 || temp.exi_man > 1 || temp.exi_sheep < 0 || temp.exi_sheep > 1 || temp.exi_wolf < 0 || temp.exi_wolf > 1 || temp.exi_veget < 0 || temp.exi_veget > 1) {
				i++;
				goto flag1;
			}
			if (_state.check_state()) {
				record[num_step] = i;
				_state = temp;
				goto flag;
			}
		}
		else
		{
			int i = 0;
		flag2:
			temp = _state;
			temp.move_in(choice[i]);
			if (temp.exi_man < 0 || temp.exi_man > 1 || temp.exi_sheep < 0 || temp.exi_sheep > 1 || temp.exi_wolf < 0 || temp.exi_wolf > 1 || temp.exi_veget < 0 || temp.exi_veget > 1) {
				i++;
				goto flag2;
			}
			if (_state.check_state()) {
				record[num_step] = i;
				_state = temp;
				goto flag;
			}
			goto flag2;
		}
	flag:
		num_step++;
		if (_state.exi_man == 0 && _state.exi_sheep == 0 && _state.exi_wolf == 0 && _state.exi_veget == 0)
		{
			cout << "共计:" << num_step << "步" << endl;
			for (int i = 0; i < num_step; i++) {
				if (i % 2 == 0){
					cout << "船夫带走了";
					print_choice(choice[i]);
					cout << endl;
				}
				else {
					cout << "船夫带回了";
					print_choice(choice[i]);
					cout << endl;
				}
				cout << "搬运成功!" << endl;
				system("pause");
			}
			break;
		}
	}
}
  • 写回答

3条回答 默认 最新

  • SoftwareTeacher 《编程之美》作者 2021-03-12 07:49
    关注

    在每一个 flag 下面 打印 i 的值, 你就知道卡在哪里了。 

    评论

报告相同问题?

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?