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:

  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)

思路:一共同拥有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<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
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_queue<node>q;
    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.x<a)       //向A加水
        {
            int tmp=a-cur.x;
            next.y=cur.y;
            next.x=a;         //来自水龙头的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp;
                q.push(next);
            }
            if(cur.y>0)      //来自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.y<b)        //向B加水
        {
            int tmp=b-cur.y;
            next.x=cur.x;
            next.y=b;      //来自水龙头的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp-N;
                q.push(next);
            }
            if(cur.x>0)      //来自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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-11-02 02:03:00

poj 3414 Pots (bfs+线索)的相关文章

POJ 3414 Pots (BFS + 记录路径)

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

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   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)        f

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   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)        f

POJ 3414 Pots (BFS/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   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)        fi

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

POJ 3414 Pots (经典bfs )

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

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   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)

poj 3414 Pots【bfs+回溯路径 正向输出】

题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12080   Accepted: 5104   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operation