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;
        }
    }
  • 相关阅读:
    C# 多线程总结 异常处理 线程取消 锁(lock)
    C# sync/async 同步/异步
    C# Parallel 多线程并发
    C# Task TaskFactory 异步线程/异步任务
    C# ThreadPool 线程池
    Nginx基础配置
    Nginx配置通用语法
    Nginx进程间的关系
    Nginx命令行控制
    Flask框架02(路由)
  • 原文地址:https://www.cnblogs.com/dilthey/p/9873333.html
Copyright © 2011-2022 走看看