BFS---倒水问题

题目:BFS—倒水问题

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i) empty the pot i to the drain;
  3. POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
    Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

思路:

给出两个罐子的容积A,B,及容积C,题目中每一个操作都代表每一步(1:向A倒满水,2:向B倒满水,3:把A的水倒空,4:把B的水倒空,5:A向B倒水(可能倒完,也可能没倒完),6:B向A倒水(可能倒完,也可能没倒完)),编写一个程序找出最短路径使A或B中的水的体积为C,如果不存在则输出impossible。,呃,也没啥具体思路,搜就完事!(注意有多少种情况)

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int a,b,c;
int book[222][222];
struct dd
{
	int a,b,s;
	int m[222];
}t,v;
void bfs()
{
	queue<dd>q;
	memset(book,0,sizeof(book));
	book[0][0]=1;
	t.a=0;
	t.b=0;
	t.s=0;
	q.push(t);
	int i,j,x,y;
	while(!q.empty())
	{
		t=q.front();
		q.pop();
		if(t.a==c||t.b==c)
		{
			printf("%d\n",t.s);
			for(i=1;i<=t.s;i++)
			{
				if(t.m[i]==1)
					printf("FILL(1)\n");
				if(t.m[i]==2)
					printf("FILL(2)\n");
				if(t.m[i]==3)
					printf("DROP(1)\n");
				if(t.m[i]==4)
					printf("DROP(2)\n");
				if(t.m[i]==5)
					printf("POUR(1,2)\n");
				if(t.m[i]==6)
					printf("POUR(2,1)\n");
			}
			return ;
		}
		if(!book[a][t.b])     //装满A,B不变
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=1;
			book[a][t.b]=1;
			v.a=a;
			v.b=t.b;
			v.s=t.s+1;
			q.push(v);
		}
		if(!book[t.a][b])       //装满B,A不变
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=2;
			book[t.a][b]=1;
			v.a=t.a;
			v.b=b;
			v.s=t.s+1;
			q.push(v);
		}
		if(!book[0][t.b])          //倒空A
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=3;
			book[0][t.b]=1;
			v.a=0;
			v.b=t.b;
			v.s=t.s+1;
			q.push(v);
		}
		if(!book[t.a][0])       //倒空B
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=4;
			book[t.a][0]=1;
			v.a=t.a;
			v.b=0;
			v.s=t.s+1;
			q.push(v);
		}
		y=min(t.b+t.a,b);
		x=max(0,t.b+t.a-b);
		if(!book[x][y])     //把A到给B,A有剩余
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=5;
			book[x][y]=1;
			v.a=x;
			v.b=y;
			v.s=t.s+1;
			q.push(v);
		}
		x=min(t.b+t.a,a);
		y=max(0,t.b+t.a-a);
		if(!book[x][y])        把B到给A,B有剩余
		{
			for(j=1;j<=t.s;j++)
				v.m[j]=t.m[j];
			v.m[j]=6;
			book[x][y]=1;
			v.a=x;
			v.b=y;
			v.s=t.s+1;
			q.push(v);
		}
	}
	printf("impossible\n");
	return ;
}
int main()
{
	scanf("%d%d%d",&a,&b,&c);
	bfs();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值