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;
    }
  • 相关阅读:
    验证手机和电话号码
    oracle取字符串长度的函数length()和hengthb()
    AngularJs教程
    nop commerce文档
    根据子查询批量删除的sql语句
    AngularJs赋值问题
    PetaPoco模糊查询
    js转换 /Date(1464671903000)/ 格式的日期的方法
    jquery-cookie插件怎么读写json数据
    executssql 函数的每一句代码的意思
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5016682.html
Copyright © 2011-2022 走看看