码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3414 Pots

时间:2017-05-08 19:56:20      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:clu   can   empty   nts   bsp   res   script   turn   cas   

Pots
Time Limit: 1000MS   MemoryLimit: 65536K
        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

Northeastern Europe 2002, Western Subregion
 
倒水问题,输出步数和方案
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cstring>
using namespace std;
int a,b,z,xu;
bool v[1000000];
int solution[1000000],cnt,pre[1000000];
struct node
{
    int x,y,s,id;
}cur,nxt;
queue<node>p;
void output(int w)
{
    if(!w) return;
    else output(pre[w]);
    switch(solution[w])
    {
        case 1:printf("FILL(1)\n");break;
        case 2:printf("FILL(2)\n");break;
        case 3:printf("DROP(1)\n");break;
        case 4:printf("DROP(2)\n");break;
        case 5:printf("POUR(2,1)\n");break;
        case 6:printf("POUR(1,2)\n");
    }
}
void pan(int i,int j,int w)
{
    if(i==z||j==z)
    {
        printf("%d\n",nxt.s);
        output(w);
        exit(0);
    }
}
int main()
{
    while(scanf("%d%d%d",&a,&b,&z)==3)
    {
        if(a==z)
        {
            printf("1\n");
            printf("FILL(1)\n");
            return 0;
        }
        else if(b==z) 
        {
            printf("1\n");
            printf("FILL(2)\n");
            return 0;
        }
        while(!p.empty()) p.pop(); cnt=0;
        memset(v,0,sizeof(v));
        memset(solution,0,sizeof(solution));
        cur.x=a; cur.y=0; cur.s=1; cur.id=++cnt; solution[cnt]=1;
        p.push(cur);
        cur.x=0; cur.y=b; cur.s=1; cur.id=++cnt; solution[cnt]=2;
        p.push(cur);
        while(!p.empty())
        {
            cur=p.front();
            if(!cur.x)    
            {
                nxt.x=a; nxt.y=cur.y; nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=1;
                pan(nxt.x,nxt.y,cnt);
                if(!v[nxt.x*1000+nxt.y])        
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
            }
            if(!cur.y)        
            {
                nxt.x=cur.x; nxt.y=b; nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=2;
                pan(nxt.x,nxt.y,cnt);
                if(!v[nxt.x*1000+nxt.y])
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
            }
            if(cur.x)
            {
                nxt.x=0; nxt.y=cur.y; nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=3;
                if(!v[nxt.x*1000+nxt.y])
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
            }
            if(cur.y)
            {
                nxt.x=cur.x; nxt.y=0; nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=4;
                if(!v[nxt.x*1000+nxt.y])
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
            }
            if(cur.x||cur.y)
            {
                xu=a-cur.x;
                if(cur.y>xu)
                {
                    nxt.x=a; nxt.y=cur.y-xu;
                }
                else 
                {
                    nxt.x=cur.x+cur.y; nxt.y=0;
                }
                nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=5;
                pan(nxt.x,nxt.y,cnt);
                if(!v[nxt.x*1000+nxt.y])
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
                xu=b-cur.y;
                if(cur.x>xu)
                {
                    nxt.y=b;
                    nxt.x=cur.x-xu;
                }
                else 
                {
                    nxt.y=cur.x+cur.y;
                    nxt.x=0;
                }
                nxt.s=cur.s+1;
                nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=6;
                pan(nxt.x,nxt.y,cnt);
                if(!v[nxt.x*1000+nxt.y])
                {
                    v[nxt.x*1000+nxt.y]=true;
                    p.push(nxt);
                }
            }
            p.pop();
        }
        printf("impossible\n");
    }
}

 

poj 3414 Pots

标签:clu   can   empty   nts   bsp   res   script   turn   cas   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6826896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!