Pots【广搜,模拟】

Pots

 POJ - 3414 

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 ≤ ≤ 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 jis 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 AB, 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,A,B代表容器的体积,C代表需要得到的水的体积,每次可以进行六种操作:将A容器填满;将B容器填满:将A容器的水倒入B中;将B容器的水倒入A中;倒掉A中的水;倒掉B中的水。问最少需要多少步才能够使A,B中某容器的水恰好为C升,并将过程输出。

方法:通过广搜每次遍历六种情况,记录其前一个步骤,再通过深搜输出,博主代码过于冗长,望见谅!!!

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e6 + 5;
const ll mod = 1e9+7;
int a,b,a1,b1,c;
struct node
{
	int x,y;
	int step;
	int pre;
	int id;
	int way;
};
int ids;
int tpl;
bool vis[1000][1000];
int tmp[maxn];
int tmp1[maxn];
int bfs()
{
	node no;
	no.x=0;
	no.y=0;
	vis[0][0]=true;
	no.pre=-1;
	no.step=0;
	no.way=-1;
	no.id=0;
	tmp[no.id]=no.pre;
	tmp[no.id]=no.way;
	ids=0;
	queue<node> q;
	while(!q.empty())
		q.pop();
	q.push(no);
	while(!q.empty()) {
		node front=q.front();
		q.pop();
		for(int i=1;i<=6;i++)
		{
			node tail;
			switch(i) {
				case 1:
				{
					if(front.x<a&&vis[a][front.y]==false)
					{
						tail.x=a;
						tail.y=front.y;
						tail.step=front.step+1;
						ids++;
						tail.id=ids;
						tail.pre=front.id;
						tail.way=1;
						tmp[tail.id]=tail.pre;
						tmp1[tail.id]=tail.way;
						vis[a][front.y]=true;
						if(tail.x==c||tail.y==c)
						{
							tpl=tail.id;
							return tail.step;
						}
						q.push(tail);
						//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
					}
					break;
				}
				case 2:
				{
					if(front.y<b&&vis[front.x][b]==false)
					{
						tail.y=b;
						tail.x=front.x;
						ids++;
						tail.id=ids;
						tail.step=front.step+1;
						tail.pre=front.id;
						tail.way=2;
						tmp[tail.id]=tail.pre;
						tmp1[tail.id]=tail.way;
						vis[front.x][b]=true;
						if(tail.x==c||tail.y==c)
						{
							tpl=tail.id;
							return tail.step;
						}
						q.push(tail);
						//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
					}
					break;
				}
				case 3:
				{
					if(front.x>0&&front.y<b)
					{
						int k1=front.x-(b-front.y),k2;
						if(k1<0)
						{
							k1=0;
							k2=front.y+front.x;
						}
						else
						{
							k1=front.x-(b-front.y);
							k2=b;
						}
						if(vis[k1][k2]==false)
						{
							vis[k1][k2]=true;
							tail.x=k1;
							tail.y=k2;
							ids++;
							tail.id=ids;
							tail.step=front.step+1;
							tail.pre=front.id;
							tail.way=3;
							tmp[tail.id]=tail.pre;
							tmp1[tail.id]=tail.way;
							if(tail.x==c||tail.y==c)
							{
								tpl=tail.id;
								return tail.step;
							}
							q.push(tail);
							//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
						}
					}
					break;
				}
				case 4:
				{
					if(front.y>0&&front.x<a)
					{
						int k1=front.y-(a-front.x),k2;
						if(k1<0)
						{
							k1=0;
							k2=front.x+front.y;
						}
						else
						{
							k1=front.y-(a-front.x);
							k2=a;
						}
						if(vis[k2][k1]==false)
						{
							vis[k2][k1]=true;
							tail.y=k1;
							tail.x=k2;
							ids++;
							tail.id=ids;
							tail.step=front.step+1;
							tail.pre=front.id;
							tail.way=4;
							tmp[tail.id]=tail.pre;
							tmp1[tail.id]=tail.way;
							if(tail.x==c||tail.y==c)
							{
								tpl=tail.id;
								return tail.step;
							}
							q.push(tail);
							//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
						}
					}
					break;
				}
				case 5:
				{
					if(front.x>0&&vis[0][front.y]==false)
					{
						vis[0][front.y]=true;
						tail.x=0;
						tail.y=front.y;
						ids++;
						tail.id=ids;
						tail.step=front.step+1;
						tail.pre=front.id;
						tail.way=5;
						tmp[tail.id]=tail.pre;
						tmp1[tail.id]=tail.way;
						if(tail.x==c||tail.y==c)
						{
							tpl=tail.id;
							return tail.step;
						}
						q.push(tail);
						//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
					}
					break;
				}
				case 6:
				{
					if(front.y>0&&vis[front.x][0]==false)
					{
						vis[front.x][0]=true;
						tail.x=front.x;
						tail.y=0;
						ids++;
						tail.id=ids;
						tail.step=front.step+1;
						tail.pre=front.id;
						tail.way=6;
						tmp[tail.id]=tail.pre;
						tmp1[tail.id]=tail.way;
						if(tail.x==c||tail.y==c)
						{
							tpl=tail.id;
							return tail.step;
						}
						q.push(tail);
						//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);
					}
					break;
				}
				default :
					break;
			}
			//printf("----------------%d\n",i);
		}
	}
	return -1;
}
void dfs(int x)
{
	if(tmp[tmp[x]]!=-1)
		dfs(tmp[x]);
	switch(tmp1[x])
	{
		case 1:
		{
			printf("FILL(1)\n");
			break;
		}
		case 2:
		{
			printf("FILL(2)\n");
			break;
		}
		case 3:
		{
			printf("POUR(1,2)\n");
			break;
		}
		case 4:
		{
			printf("POUR(2,1)\n");
			break;
		}
		case 5:
		{
			printf("DROP(1)\n");
			break;
		}
		case 6:
		{
			printf("DROP(2)\n");
			break;
		}
		default :
		{
			break;
		}
	}
}

int main() 
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    scanf("%d %d %d",&a,&b,&c);
    int ans=bfs();
    if(ans==-1)
    	printf("impossible\n");
    else
    {
    	printf("%d\n",ans);
	    dfs(tpl);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值