搜索--广搜深搜结合--H - Pots

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 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 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)

题目:

   怎么说呢,倒水输出最优解就够麻烦的了(非常可乐),现在还要输出最优解的“路径”,不得不说出题人很优秀。好了,现在切入正题。两个容器,三种方式,不难发现总共存在六种行为或者说选择:

   { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};

  而在这六种选择之中,毋容置疑后两种比较麻烦些,因为存在容器空不空的情况。解决这个问题后,来看看倒水问题,比较统一的一个小思想,那就是定义不同状态(这里所说的状态是:容器大小OR现在液体的体积),这里就是如此。结构体node定义就相当于容器,而now就相当于定义当前的液体体积,也就是实际存储的。剩下的部分也就和普通的倒水问题没什么差别了。


#include<bits/stdc++.h>
using namespace std;
int A, B, C;
char a[10][10] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
typedef struct node
{
    
    int a;
    int b;
    int step;
    int optation;
}node; 
node v[110][110];

typedef struct now
{
    int x, y;
}now; //实际储存的 

void DFS(int x, int y)
{
    if(x == 0 && y == 0)
        return ;
    DFS(v[x][y].a, v[x][y].b);
    printf("%s\n", a[ v[x][y].optation ]);

}
void BFS()
{
    queue<now> Q;
    now s, q;
    s.x=0;
    s.y=0;
    v[0][0].step = 1;
    Q.push(s);
    while(Q.size())
    {
        s = Q.front();
        Q.pop();
        if(s.x == C || s.y == C)
        {
            printf("%d\n", v[s.x][s.y].step-1);
            DFS(s.x, s.y);
            return ;
        }

        for(int i=0; i<6; i++)
        {
            q = s;

            if(i == 0)
                q.x = A;
            else if(i == 1)
                q.y = B;
            else if(i == 2)
                q.x = 0;
            else if(i == 3)
                q.y = 0;
            else if(i == 4)
            {
        
                if((q.x+q.y<=B))
                {
                	q.y=q.y+q.x;
                	q.x=0;
				}
				else
				{
					q.x=q.x-(B-q.y);
					q.y=B;
				}
            }
            else
            {
                    if(q.x+q.y<=A)
                    {
                    	q.x=q.x+q.y;
                    	q.y=0;
					}
					else
					{
						q.y=q.y-(A-q.x);
						q.x=A;
					}
            }

            if(v[q.x][q.y].step == 0)
            {
                v[q.x][q.y].step = v[s.x][s.y].step+1;
                v[q.x][q.y].a = s.x;
                v[q.x][q.y].b = s.y;
                v[q.x][q.y].optation = i;
                Q.push(q);
            }
        }
    }

    printf("impossible\n");
}

int main()
{
    while(scanf("%d%d%d", &A, &B, &C) != EOF)
    {
        memset(v, 0, sizeof(v));
        BFS();
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值