【Week2作业 B】Pour Water【bfs】

题意:

有两个杯子,容量为A,B且0<A<=B,AB互质。要求根据六个操作(倒满A,倒满B,A倒B,B倒A,倒空A,倒空B),最终得到体积为C的水。C<=B<=1000。输入包含多组数据,每组数据占一行,为A,B,C三个数。输出得到体积为C的水的一系列指令,每组数据最后要输出"success"。


思路:

1.使用visit数组保存是否访问过,oper数组保存该顶点是由什么操作得来的(0至5分别为A倒B,B倒A,倒满A,倒满B,倒空A,倒空B),beforex和beforey数组保存该顶点的上一步顶点的坐标。
2.访问(0,0)顶点,标记为已访问过。将其加入到队列中。
3.从队列中取出一个顶点,根据六个操作得到六个顶点,若没访问过,标记好visit、oper、beforex、beforey数组,然后加入到队列中。
4.重复步骤3,直到取出的顶点一个坐标为C或者队列为空。
5.从最终状态开始回溯操作。将最终顶点对应的操作oper[now.x][now.y]加入到栈中,然后访问当前顶点的上一步顶点,继续循环,直到顶点回溯到初始点(0,0)为止。
6.循环结束后,栈中保存了操作对应的数值,依次取出输出即可。


总结:

这道题是bfs隐式图问题,仅给出了初始节点、目标节点以及生成子节点的约束条件(即六种操作),要求按扩展规则应用于扩展节点的过程,找出其他节点,直到包含目标节点为止。


代码:

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

struct point{
	int x,y;
	bool operator < (const point &p) const
	{
		return x!=p.x ? x<p.x : y<p.y;
	}
	bool operator == (const point &p)const
	{
		if(x==p.x&&y==p.y)	return true;
		else return false;
	}
	point (int x1,int y1):x(x1),y(y1){	}
};
point AToB(point &p,int a,int b)
{
	int x,y;
	if(p.x+p.y>b)
	{
		x=p.x-(b-p.y);
		y=b;
	}
	else 
	{
		x=0;
		y=p.x+p.y;
	}
	return point(x,y);
}
point BToA(point &p,int a,int b)
{
	int x,y;
	if(p.x+p.y>a)
	{
		x=a;
		y=p.y-(a-p.x);
	}
	else
	{
		x=p.x+p.y;
		y=0;
	}
	return point(x,y);
}
point fillA(point &p,int a,int b)
{
	return point(a,p.y);
}
point fillB(point &p,int a,int b)
{
	return point(p.x,b);
}
point emptyA(point &p,int a,int b)
{
	return point(0,p.y);
}
point emptyB(point &p,int a,int b)
{
	return point(p.x,0);
}

int main()
{
	int a,b,c;
	while(cin>>a>>b>>c)
	{
		bool visit[b+1][b+1];
		for(int i=0;i<b+1;i++)
			for(int j=0;j<b+1;j++)
				visit[i][j]=0;
		int oper[b+1][b+1];		//操作,0至5
		int beforex[b+1][b+1],beforey[b+1][b+1];	//前一步状态 
		queue<point> Q;
		Q.push(point(0,0));
		visit[0][0]=1;
		int ansx,ansy;	//最终状态 
		while(!Q.empty())
		{
			point now=Q.front();
			Q.pop();
			if(now.x==c)
			{
				ansx=c,ansy=now.y;
				break;
			}
			else if(now.y==c)
			{
				ansx=now.x,ansy=c;
				break;
			}
			point next(0,0);
			next=AToB(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=0;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
			next=BToA(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=1;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
			next=fillA(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=2;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
			next=fillB(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=3;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
			next=emptyA(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=4;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
			next=emptyB(now,a,b);
			if(!visit[next.x][next.y])
			{
				oper[next.x][next.y]=5;
				beforex[next.x][next.y]=now.x;
				beforey[next.x][next.y]=now.y;
				visit[next.x][next.y]=1;
				Q.push(next);
			}
		}
		//输出部分
		stack<int> op;	//操作 
		point now(ansx,ansy);
		while(!(now==point(0,0)))
		{
			op.push(oper[now.x][now.y]);
			int bx=beforex[now.x][now.y],by=beforey[now.x][now.y];
			now.x=bx,now.y=by;
		}
		while(!op.empty())
		{
			int answer=op.top();
			op.pop();
			if(answer==0)	cout<<"pour A B"<<endl;
			else if(answer==1)	cout<<"pour B A"<<endl;
			else if(answer==2)	cout<<"fill A"<<endl;
			else if(answer==3)	cout<<"fill B"<<endl;
			else if(answer==4)	cout<<"empty A"<<endl;
			else if(answer==5)	cout<<"empty B"<<endl;
		}
		cout<<"success"<<endl;
	}	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值