计算机图形学E6——倒水问题emm和OpenGL没啥关系

其他计算机图形学实验见 链接

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;

struct node//状态结构体
{
	int a, b;
	int last;
	int step;
};

int vis[maxn][maxn];
int A, B, C;
node q[maxn];
int front, rear;

int bfs(int x, int y)
{
	node t;
	t.a = x; t.b = y; t.step = 0; t.last = -1;
	//q.push(t);
	q[front] = t;
	rear++;

	while (front < rear)
	{
		//node now = q.front(); q.pop();
		node now = q[front]; //front++;

		//cout << "front=" << front << " rear=" << rear << endl;
		//cout << now.a << " " << now.b << endl;

		if (now.a == C || now.b == C)
		{
			q[rear] = now;//记得保存最后这一次,否则输出的路径是错误结果的路径
			return now.step;
		}

		if (!vis[now.a][now.b])
		{
			vis[now.a][now.b] = 1;

			node temp;
			temp.a = 0; temp.b = 0; temp.last = -1; temp.step = 0;
			for (int i = 1; i <= 6; i++)
			{
				//1、a有水,倒空
				if (i == 1 && now.a > 0)
				{
					temp.a = 0;
					temp.b = now.b;
					temp.step = now.step + 1;
					temp.last = front;
					//q.push(temp);
					/*if (!vis[temp.a][temp.b])
					{
						q[rear] = temp; rear++;
					}*/
				}
				//2、b有水,倒空
				if (i == 2 && now.b > 0)
				{
					temp.a = now.a;
					temp.b = 0;
					temp.step = now.step + 1;
					temp.last = front;
					//q.push(temp);
					/*if (!vis[temp.a][temp.b])
					{
						q[rear] = temp; rear++;
					}*/
				}
				//3、a未满 a灌满水
				if (i == 3 && now.a < A)
				{
					temp.a = A;
					temp.b = now.b;
					temp.step = now.step + 1;
					temp.last = front;
					//q.push(temp);
					/*if (!vis[temp.a][temp.b])
					{
						q[rear] = temp; rear++;
					}*/
				}
				//4、b未满 b灌满水
				if (i == 4 && now.b < B)
				{
					temp.a = now.a;
					temp.b = B;
					temp.step = now.step + 1;
					temp.last = front;
					//q.push(temp);
					/*if (!vis[temp.a][temp.b])
					{
						q[rear] = temp; rear++;
					}*/
				}
				//5、a有水,b没满,a倒入b
				if (i == 5 && now.a > 0 && now.b < B)
				{
					if (now.a > B - now.b)
					{
						temp.a = now.a - (B - now.b);
						temp.b = B;
						temp.step = now.step + 1;
						temp.last = front;
						//q.push(temp);
						/*if (!vis[temp.a][temp.b])
						{
							q[rear] = temp; rear++;
						}*/
					}
					else
					{
						temp.a = 0;
						temp.b = now.a + now.b;
						temp.step = now.step + 1;
						temp.last = front;
						//q.push(temp);
						/*if (!vis[temp.a][temp.b])
						{
							q[rear] = temp; rear++;
						}*/
					}
				}
				//6、b有水,a没满,b倒入a
				if (i == 6 && now.a < A && now.b>0)
				{
					if (now.b > A - now.a)
					{
						temp.a = A;
						temp.b = now.b - (A - now.a);
						temp.step = now.step + 1;
						temp.last = front;
						//q.push(temp);
						/*if (!vis[temp.a][temp.b])
						{
							q[rear] = temp; rear++;
						}*/
					}
					else
					{
						temp.b = 0;
						temp.a = now.a + now.b;
						temp.step = now.step + 1;
						temp.last = front;
						//q.push(temp);
						/*if (!vis[temp.a][temp.b])
						{
							q[rear] = temp; rear++;
						}*/
					}
				}

				if (!vis[temp.a][temp.b])
				{
					q[rear] = temp; rear++;
				}
			}
		}
		front++;
	}
	return -1;
}

void displayPath(int i)
{
	if (q[i].last == -1)
	{
		cout << "(" << q[i].a << ", " << q[i].b << ") -> ";
		return;
	}
	displayPath(q[i].last);
	cout << "(" << q[i].a << ", " << q[i].b << ") ->" ;
}
//void displayPath(node x)
//{
//	if (x.last == -1)
//	{
//		cout << "(" << x.a << ", " << x.b << ") ->";
//		return;
//	}
//	displayPath(q[x.last]);
//	cout << "(" << x.a << ", " << x.b << ") -> ";
//}

int main()
{
	cin >> A >> B >> C;
	int t = bfs(0, 0);
	if (t != -1)
	{
		cout << "Yes" << endl;
		cout << "操作次数为:" << t << endl;
		//displayPath(q[rear]);
		displayPath(rear);
	}
	else
		cout << "Impossible" << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值