zoukankan      html  css  js  c++  java
  • POJ 3414

    题目链接:http://poj.org/problem?id=3414

    Time Limit: 1000MS  Memory Limit: 65536K

    Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
    DROP(i) empty the pot i to the drain;
    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 A, B, 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$ 的壶,现有三种操作:

    1. 从水龙头把壶 $i$ 倒满;
    2. 把壶 $i$ 倒空;
    3. 把壶 $i$ 里的水倒到壶 $j$ 中,直到壶 $i$ 空或者壶 $j$ 满。

    给出目标 $C$,要求最终至少某一个壶中的水量正好为 $C$,要求输出最少步数。

    题解:

    暴力搜索。

    AC代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<string>
    #include<queue>
    using namespace std;
    typedef pair<int,int> pii;
    
    int A,B,C;
    string op[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
    inline pii change(pii x,int type)
    {
        int a=x.first, b=x.second;
        switch(type)
        {
        case 1: //倒满a
            a=A;
            break;
        case 2: //倒满b
            b=B;
            break;
        case 3: //倒空a
            a=0;
            break;
        case 4: //倒空b
            b=0;
            break;
        case 5: //a倒到b
            if(a>=B-b) a-=B-b, b=B;
            else b+=a, a=0;
            break;
        case 6: //b倒到a
            if(b>=A-a) b-=A-a, a=A;
            else a+=b, b=0;
            break;
        }
        return make_pair(a,b);
    }
    
    int vis[105][105];
    pii pre[105][105];
    queue<pii> Q;
    inline pii bfs()
    {
        memset(vis,0,sizeof(vis));
        memset(pre,0,sizeof(pre));
        Q.push(make_pair(0,0));
        vis[0][0]=1;
        while(!Q.empty())
        {
            pii now=Q.front(); Q.pop();
            if(now.first==C || now.second==C) return now;
            for(int type=1;type<=6;type++)
            {
                pii nxt=change(now,type);
                if(!vis[nxt.first][nxt.second])
                {
                    vis[nxt.first][nxt.second]=type;
                    pre[nxt.first][nxt.second]=now;
                    Q.push(nxt);
                }
            }
        }
        return make_pair(-1,-1);
    }
    
    int main()
    {
        cin>>A>>B>>C;
        pii x=bfs();
        if(x.first==-1 && x.second==-1) printf("impossible
    ");
        else
        {
            vector<int> ans;
            while(x.first+x.second>0)
            {
                ans.push_back(vis[x.first][x.second]);
                x=pre[x.first][x.second];
            }
            printf("%d
    ",ans.size());
            for(int i=ans.size()-1;i>=0;i--) cout<<op[ans[i]]<<endl;
        }
    }
  • 相关阅读:
    ORACLE的专用模式和共享模式(转)
    用TSQL修改数据库的恢复模型
    Python中的数组
    hotmail是如何被劫持的?
    [收藏] vss自动备份
    在Oracle中模拟ms Sql 中的自动增加字段
    Oracle重建所有表和索引
    CentOS6.0安装PostgreSQL9.1
    linux查找文件命令find
    Linux修改网络配置
  • 原文地址:https://www.cnblogs.com/dilthey/p/9873333.html
Copyright © 2011-2022 走看看