程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 3414 Pots (bfs+路徑記錄)

poj 3414 Pots (bfs+路徑記錄)

編輯:C++入門知識

poj 3414 Pots (bfs+路徑記錄)


Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special Judge

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)

思路:一共有6種操作:把A中水倒掉,把A加滿,把B裡的水倒入A中;B和A類似。

罐子最大容積為100,設一個常量N=100, 開一個二維數組記錄狀態變化的值。

1、從水龍頭往A裡加水t,記錄-t,

2、從水龍頭往B裡加水t,記錄-t-N,

3、從B裡面往A加水t,記錄t

4、從A裡面往B加水t,記錄N+t

5、把A裡水倒掉,記錄2*N+t,(A原有水t)

6、把B裡水倒掉,記錄3*N+t,(B原有水t)

#include
#include
#include
#include
#include
using namespace std;
#define N 105
const int inf=0x1f1f1f1f;
int a,b,c,flag;
int mark[N][N];
struct node
{
    int x,y,t;
    friend bool operator<(node a,node b)
    {
        return a.t>b.t;
    }
};
void prif(int x,int y)       //遞歸輸出路徑
{
    if(x==0&&y==0)
        return ;
    if(mark[x][y]>3*N)
    {
        prif(x,mark[x][y]-3*N);
        printf("DROP(2)\n");
    }
    else if(mark[x][y]>2*N)
    {
        prif(mark[x][y]-2*N,y);
        printf("DROP(1)\n");
    }
    else if(mark[x][y]>N)
    {
        int tmp=mark[x][y]-N;
        prif(x+tmp,y-tmp);
        printf("POUR(1,2)\n");
    }
    else if(mark[x][y]>0)
    {
        int tmp=mark[x][y];
        prif(x-tmp,y+tmp);
        printf("POUR(2,1)\n");
    }
    else if(mark[x][y]>-N)
    {
        int tmp=-mark[x][y];
        prif(x-tmp,y);
        printf("FILL(1)\n");
    }
    else if(mark[x][y]<-N)
    {
        int tmp=N+mark[x][y];
        prif(x,y+tmp);
        printf("FILL(2)\n");
    }
}
void bfs()
{
    priority_queueq;
    node cur,next;
    mark[0][0]=inf;  //該狀態只能出現一次,賦值為inf防止干擾其他值
    mark[a][0]=-a;
    mark[0][b]=-b-N;
    cur.t=1;       
    cur.x=a;
    cur.y=0;
    q.push(cur);
    cur.x=0;
    cur.y=b;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        next.t=cur.t+1;
        if(cur.x==c||cur.y==c)
        {
            flag=1;
            printf("%d\n",cur.t);
            prif(cur.x,cur.y);
            return ;
        }
        if(cur.x0)      //來自B的水
            {
                int tmp=min(cur.y,a-cur.x);
                next.x=cur.x+tmp;
                next.y=cur.y-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp;
                    q.push(next);
                }
            }
        }
        if(cur.y0)      //來自A的水
            {
                int tmp=min(cur.x,b-cur.y);
                next.y=cur.y+tmp;
                next.x=cur.x-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp+N;
                    q.push(next);
                }
            }
        }
        if(cur.x>0)     //倒掉水
        {
            int tmp=cur.x;
            next.x=0;
            next.y=cur.y;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=2*N+tmp;
                q.push(next);
            }
        }
        if(cur.y>0)
        {
            int tmp=cur.y;
            next.y=0;
            next.x=cur.x;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=3*N+tmp;
                q.push(next);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=-1)
    {
        memset(mark,0,sizeof(mark));
        flag=0;
        bfs();
        if(!flag)
            printf("impossible\n");
    }
    return 0;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved