poj 3414 Pots BFS

46 篇文章 0 订阅
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9938 Accepted: 4168 Special Judge

Description

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)

Source

Northeastern Europe 2002, Western Subregion

训练赛的时候看到这一题没做出来,没有什么好的思路,下来消化看题解发现是bfs,自己太菜。。

题意就是给你两个容器A和B,要求经过若干操作使其中一个容器的水体积为C,求最少的操作次数和操作过程。其中有三种操作:

(1)FILL(1)把1容器装满水;

(2)DROP(1)把1容器里的水倒光;

(3)POUR(1,2)把1容器里的水倒进2里面,到满为止。

根据题目意思可以知道每次操作共有六种情况可选:

(1)把A装满,B不变;(2)把B装满,A不变;(3)把A倒掉,B不变;(4)把B倒掉,A不变;(5)把A倒入B;(6)把B倒入A。

讲到这里题目就很简单了:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

bool visit[101][101];
int A,B,C;
int path[MAXN];

struct Node
{
    int a,b,step,pre,flag;
};

Node Q[MAXN];

void bfs()
{
    int i;
    memset(visit,false,sizeof(visit));
    int front=0,rear=0;
    Node st,now;
    st.a=0;st.b=0;st.step=0;st.pre=-1;
    visit[0][0]=true;
    Q[rear++]=st;
    while (front!=rear)
    {
        st=Q[front++];
        if (st.a==C||st.b==C)
            break;
        for (i=0;i<6;i++)
        {
            switch (i)
            {
                case 0:now.a=A;now.b=st.b;now.flag=0;break;
                case 1:now.a=st.a;now.b=B;now.flag=1;break;
                case 2:now.a=0;now.b=st.b;now.flag=2;break;
                case 3:now.a=st.a;now.b=0;now.flag=3;break;
                case 4:
                    if (st.a<=(B-st.b))
                    {
                        now.a=0;
                        now.b=st.a+st.b;
                        now.flag=4;
                    }
                    else
                    {
                        now.a=st.a+st.b-B;
                        now.b=B;
                        now.flag=4;
                    }
                    break;
                default :
                    if (st.b<=(A-st.a))
                    {
                        now.b=0;
                        now.a=st.a+st.b;
                        now.flag=5;
                    }
                    else
                    {
                        now.b=st.a+st.b-A;
                        now.a=A;
                        now.flag=5;
                    }
            }
            if (!visit[now.a][now.b])
            {
                visit[now.a][now.b]=true;
                now.step=st.step+1;
                now.pre=front-1;
                Q[rear++]=now;
            }
        }
    }
    if (front==rear)
    {
        printf("impossible\n");
        return ;
    }
    int number=0;
    for (int j=front-1;j>=0;)
    {
        path[number++]=j;
        j=Q[j].pre;
    }
    printf("%d\n",Q[front-1].step);
    for (i=number-1;i>=0;i--)
    {
        switch(Q[path[i]].flag)
        {
            case 0:printf("FILL(1)\n");break;
            case 1:printf("FILL(2)\n");break;
            case 2:printf("DROP(1)\n");break;
            case 3:printf("DROP(2)\n");break;
            case 4:printf("POUR(1,2)\n");break;
            case 5:printf("POUR(2,1)\n");break;
        }
    }
}

int main()
{
    scanf("%d%d%d",&A,&B,&C);
    bfs();
    return 0;
}

/*
3 5 4
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值