zoukankan      html  css  js  c++  java
  • poj3414Pots(倒水BFS)

    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13231   Accepted: 5553   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)
    #include"cstdio"
    #include"cstring"
    #include"queue"
    #include"algorithm"
    using namespace std;
    const int MAXN=105;
    struct node{
        int a,b,op,pre;
        node(int ca,int cb,int co,int cp):a(ca),b(cb),op(co),pre(cp){} 
        node(){}
    };
    const char* opit[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
    int vis[MAXN][MAXN];
    int A,B,C;
    node step[MAXN*MAXN];
    int cnt;
    void print(int now,int ans)//?????? 
    {
        node no = step[now];
        if(no.pre==-1)
        {
            printf("%d
    ",ans);
            return ;
        }
        print(no.pre,ans+1);
        printf("%s
    ",opit[no.op]);
    }
    void bfs()
    {
        memset(vis,0,sizeof(vis));
        cnt=0;
        queue<node> que;
        que.push(node(0,0,0,-1));
        while(!que.empty())
        {
            node now = que.front();que.pop();
            step[cnt++]=now;
            if(now.a==C||now.b==C)
            {
                print(cnt-1,0);
                return ;
            }
            int ta,tb;
            //第一种操作 FILL(A)
            ta=A,tb=now.b;
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,1,cnt-1));
            }
            
            //第二种操作 FILL(B)
            ta=now.a,tb=B;
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,2,cnt-1));
            }
            //第三种操作 DROP(A) 
            ta=0,tb=now.b;
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,3,cnt-1));
            }
            //第四种操作 DROP(B)
            ta=now.a,tb=0;
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,4,cnt-1));
            } 
            //第五种操作 POUR(A,B)
            ta=now.a-min(B-now.b,now.a);
            tb=now.b+min(B-now.b,now.a);
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,5,cnt-1));
            }
            //第六种操作 POUR(B,A)
            ta=now.a+min(A-now.a,now.b);
            tb=now.b-min(A-now.a,now.b);
            if(!vis[ta][tb])
            {
                vis[ta][tb]=1;
                que.push(node(ta,tb,6,cnt-1));
            } 
        }
        printf("impossible
    ");
    }
    int main()
    {
        while(scanf("%d%d%d",&A,&B,&C)!=EOF)
        {
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    值币转换编程总结
    打印沙漏编程总结
    Leetcode每日一题 面试题 17.21. 直方图的水量
    VS2017-OPENGL配置glfw+glad
    OpenGL(二) 绘制一个三角形
    OpenGL(一) 渲染循环创建窗口
    Leetcode每日一题 1006.笨阶乘
    Leetcode每日一题 90.子集 II
    Leetcode每日一题 190.颠倒二进制位
    Leetcode 第243场周赛 创新奇智&力扣
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5016682.html
Copyright © 2011-2022 走看看