H - Pots POJ - 3414 (bfs+map)

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21332   Accepted: 9105   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

题意:给出两个杯子,容量为A,B。给出一个目标水量 C 。问,用这两个杯子取出C的水,要取几次。每次取水有6种操作,FILL(1),把杯子1倒满,FILL(2)把杯子2倒满,DROP(1)把杯子1的水倒光,DROP(2)把杯子2的水倒光,
POUR(1,2),把杯子1的水倒入杯子2,POUR(2,1)把杯子2的水倒入1.
思路:bfs,6个入口即可,就是有点长。然后注意一下细节,只要有其中一个杯子的水与目标水量相等即可。还需要记录一个标记,

vis[i][j],表示1杯水量为i,2杯水量为j的情况是否已经出现过了,避免重复计算。。直接看代码吧

    #include "iostream"
    #include "queue"
    #include "string"
    #include "map"
    using namespace std;
    struct P//用结构体记录1,2杯子里面的水量和操作路径
    {
        int x,y;
        string path;
    };
    map<int,string> mp;
    bool vis[105][105];
    int x,y,z,flag;
    void init()//初始话,用map来记录对应关系
    {
        mp.clear();
        mp[1]="FILL(1)";
        mp[2]="FILL(2)";
        mp[3]="DROP(1)";
        mp[4]="DROP(2)";
        mp[5]="POUR(1,2)";
        mp[6]="POUR(2,1)";
    }
    P work(int t,P s)//计算出在这六中情况下1,2,杯里面的水量和路径
    {
        P a;
        switch(t){
            case 1:
                a.x=x,a.y=s.y,a.path=s.path+'1';
                break;
            case 2:
                a.x=s.x,a.y=y,a.path=s.path+'2';
                break;
            case 3:
                a.x=0,a.y=s.y,a.path=s.path+'3';
                break;
            case 4:
                a.x=s.x,a.y=0,a.path=s.path+'4';
                break;
            case 5:
                if(y-s.y<s.x){
                    a.x=s.x-(y-s.y);
                    a.y=y;
                }
                else{
                    a.x=0;
                    a.y=s.x+s.y;
                }
                a.path=s.path+'5';
                break;
            case 6:
                if(x-s.x<s.y){
                    a.y=s.y-(x-s.x);
                    a.x=x;
                }
                else{
                    a.x=s.x+s.y;
                    a.y= 0;
                }
                a.path=s.path+'6';
                break;
        }
        return a;
    }
    void bfs()
    {
        queue<P> que;
        P a;
        a.x=x,a.y=0,a.path="1";//首次操作,将杯1的水加满或者将杯2的水加满
        vis[x][0]=1;
        que.push(a);
        a.x=0,a.y=y,a.path="2";
        vis[0][y]=1;
        que.push(a);
        while(!que.empty()){
            P s=que.front();
            que.pop();
            if(s.x==z||s.y==z){
                flag=1;
                cout<<s.path.size()<<endl;
                for(int i=0;i<s.path.size();i++)
                    cout<<mp[s.path[i]-'0']<<endl;
                return;
            }
            for(int i=1;i<=6;i++){//六种情况
                P a=work(i,s);
                if(!vis[a.x][a.y]){
                    vis[a.x][a.y]=1;
                    que.push(a);
                }
            }
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin>>x>>y>>z;
        flag=0;
        init();
        bfs();
        if(!flag)
            cout<<"impossible"<<endl;
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80585638
今日推荐