POJ-3414-两个杯子倒水问题(宽搜+回溯)

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9482   Accepted: 3983   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 ≤ i ≤ 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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


题意:
给出两个容积分别为 a 和 b 的pot,按照以下三种操作方式,求出能否在一定步数后,使者两个pot的其中一个的水量为c。
      1.FILL(i):将ipot倒满水。
      2.DROP(i):将ipot倒空水。
      3.POUR(i,j): 将ipot的水倒到jpot上,直至要么ipot为空,要么jpot为满。
思路:
BFS求最短路径步数,并在过程中利用回溯记录路径。

贴上代码:(要是你有足够的耐心,这个代码还是容易理解的)

#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct P
{
    int x;
    int y;           //x,y用来记录杯子中水的状态
    int sx;
    int sy;          //sx,sy记录路径
    int v;           //用1,2,3,4,5,6,代表倒水的操作
    int vis;         //标记
    int num;         //记录倒水操作的步骤数
}cup[110][110];

int a,b,c;
int flag;
int xx,yy;
int xxx,yyy;
int path[10000];
int bfs()
{
    queue<P>q;
    q.push(cup[0][0]);
    cup[0][0].vis=1;
    while(q.size())
    {
        P p=q.front();
        xx=p.x;
        yy=p.y;
        q.pop();
        if(xx==c || yy==c)
        {
            flag=1;
            break;
        }
        if(p.x>=0&&p.x<=a&&p.y>=0&&p.y<=b)
        {
            if(cup[a][p.y].vis==0)     //杯子a加满
            {
                cup[a][p.y].sx=p.x;
                cup[a][p.y].sy=p.y;
                cup[a][p.y].vis=1;
                cup[a][p.y].num=cup[p.x][p.y].num+1;
                cup[a][p.y].v=1;
                q.push(cup[a][p.y]);
            }
            if(cup[0][p.y].vis==0)   //杯子a倒空
            {
                cup[0][p.y].sx=p.x;
                cup[0][p.y].sy=p.y;
                cup[0][p.y].vis=1;
                cup[0][p.y].num=cup[p.x][p.y].num+1;
                cup[0][p.y].v=2;
                q.push(cup[0][p.y]);
            }
            if(cup[p.x][b].vis==0)  //杯子b加满
            {
                cup[p.x][b].sx=p.x;
                cup[p.x][b].sy=p.y;
                cup[p.x][b].vis=1;
                cup[p.x][b].num=cup[p.x][p.y].num+1;
                cup[p.x][b].v=3;
                q.push(cup[p.x][b]);
            }
            if(cup[p.x][0].vis==0)  //杯子b倒空
            {
                cup[p.x][0].sx=p.x;
                cup[p.x][0].sy=p.y;
                cup[p.x][0].vis=1;
                cup[p.x][0].num=cup[p.x][p.y].num+1;
                cup[p.x][0].v=4;
                q.push(cup[p.x][0]);
            }
            if(p.x+p.y-b>=0&&cup[p.x+p.y-b][b].vis==0)   //a倒入b,a有剩余
            {
                cup[p.x+p.y-b][b].sx=p.x;
                cup[p.x+p.y-b][b].sy=p.y;
                cup[p.x+p.y-b][b].vis=1;
                cup[p.x+p.y-b][b].num=cup[p.x][p.y].num+1;
                cup[p.x+p.y-b][b].v=5;
                q.push(cup[p.x+p.y-b][b]);
            }
            if(p.x+p.y-b<0&&cup[0][p.x+p.y].vis==0)     //a倒入b,a空
            {
                cup[0][p.x+p.y].sx=p.x;
                cup[0][p.x+p.y].sy=p.y;
                cup[0][p.x+p.y].vis=1;
                cup[0][p.x+p.y].num=cup[p.x][p.y].num+1;
                cup[0][p.x+p.y].v=5;
                q.push(cup[0][p.x+p.y]);
            }
            if(p.x+p.y-a>=0&&cup[a][p.x+p.y-a].vis==0)  //b倒入a,b有剩余
            {
                cup[a][p.x+p.y-a].sx=p.x;
                cup[a][p.x+p.y-a].sy=p.y;
                cup[a][p.x+p.y-a].vis=1;
                cup[a][p.x+p.y-a].num=cup[p.x][p.y].num+1;
                cup[a][p.x+p.y-a].v=6;
                q.push(cup[a][p.x+p.y-a]);
            }
            if(p.x+p.y-a<0&&cup[p.x+p.y][0].vis==0)    //b倒入a,b空
            {
                cup[p.x+p.y][0].sx=p.x;
                cup[p.x+p.y][0].sy=p.y;
                cup[p.x+p.y][0].vis=1;
                cup[p.x+p.y][0].num=cup[p.x][p.y].num+1;
                cup[p.x+p.y][0].v=6;
                q.push(cup[p.x+p.y][0]);
            }
        }
    }

    if(flag)
    {
        printf("%d\n",cup[xx][yy].num);
        int sum=cup[xx][yy].num;
        for(int i=sum;i>0;i--)
        {
            path[i]=cup[xx][yy].v;
            xxx=xx;
            yyy=yy;
            xx=cup[xxx][yyy].sx;
            yy=cup[xxx][yyy].sy;
        }
        for(int i=1;i<=sum;i++)
        {
           if(path[i]==1)      printf("FILL(1)\n");
           else if(path[i]==2) printf("DROP(1)\n");
           else if(path[i]==3) printf("FILL(2)\n");
           else if(path[i]==4) printf("DROP(2)\n");
           else if(path[i]==5) printf("POUR(1,2)\n");
           else if(path[i]==6) printf("POUR(2,1)\n");
        }
    }
    else
        printf("impossible\n");
}

int main()
{
    scanf("%d%d%d",&a,&b,&c);
    for(int i=0;i<110;i++)
    {
        for(int j=0;j<110;j++)
        {
            cup[i][j].x=i;
            cup[i][j].y=j;
            cup[i][j].sx=0;
            cup[i][j].sy=0;
            cup[i][j].v=0;
            cup[i][j].num=0;
            cup[i][j].vis=0;
        }
    }
    flag=0;
    memset(path,0,sizeof(path));
    bfs();
    return 0;
}



你可能感兴趣的:(回溯,宽搜)