Pots (POJ

来源:互联网 发布:程序员媛 编辑:程序博客网 时间:2024/06/07 15:25

题目描述:
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 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)

最近在做搜索专题,这题一看就知道是宽搜了,和之前做的非常可乐 (HDU - 1495 )非常相似,代码冗长的原因是6个状态需要一一列举,宽搜最烦的就是这种情况,所以重要的是细心。
代码:

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;import java.util.Stack;public class Main{       static int c;    public static void main(String[] args)     {        Scanner sc=new Scanner(System.in);        int a=sc.nextInt();        int b=sc.nextInt();        c=sc.nextInt();        if(c==0)            return;        int w[]=new int[2];        int v[][]=new int[a+1][b+1];//两个杯子的水的状态标志        Queue<node> queue=new LinkedList<node>();        queue.add(new node(0,0,null,null));//入队列        v[0][0]=1;        boolean sign=true;        node ans = null;//来指向终止状态节点        while(!queue.isEmpty()&&sign)        {            node t=queue.poll();            if(v[a][t.b]==0)//考虑FILL(1)            {                if(check(a,t.b))                {                    ans=new node(a,t.b,"FILL(1)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(a,t.b,"FILL(1)",t));                    v[a][t.b]=1;                }            }            if(v[t.a][b]==0)//考虑FILL(2)            {                if(check(t.a,b))                {                    ans=new node(t.a,b,"FILL(2)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(t.a,b,"FILL(2)",t));                    v[t.a][b]=1;                }            }            if(v[0][t.b]==0)//考虑DROP(1)            {                if(check(0,t.b))                {                    ans=new node(0,t.b,"DROP(1)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(0,t.b,"DROP(1)",t));                    v[0][t.b]=1;                }            }            if(v[t.a][0]==0)//考虑DROP(2)            {                if(check(t.a,0))                {                    ans=new node(t.a,0,"DROP(2)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(t.a,0,"DROP(2)",t));                    v[t.a][0]=1;                }            }            int x=t.a,y=t.b;            while(x!=0&&y!=b)//考虑POUR(1,2)            {                x--;                y++;            }            if(v[x][y]==0)            {                if(check(x,y))                {                    ans=new node(x,y,"POUR(1,2)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(x,y,"POUR(1,2)",t));                    v[x][y]=1;                }            }            x=t.b;y=t.a;            while(x!=0&&y!=a)//考虑POUR(2,1)            {                x--;                y++;            }            if(v[y][x]==0)            {                if(check(y,x))                {                    ans=new node(y,x,"POUR(2,1)",t);                    sign=false;                    break;                }                else                {                    queue.add(new node(y,x,"POUR(2,1)",t));                    v[y][x]=1;                }            }        }        if(sign)//说明从来没有遇到过终止状态,            System.out.println("impossible");        else//关于记录路径的细节可以找之前的文章学霸的迷宫http://blog.csdn.net/coldfresh/article/details/65635859        {            Stack<node> stack=new Stack<node>();            while(ans!=null)            {                stack.add(ans);                ans=ans.last;            }            stack.pop();            System.out.println(stack.size());            while(!stack.isEmpty())                System.out.println(stack.pop().s);              }    }    static boolean check(int x,int y)//终止状态判断    {        if(x==c||y==c||x+y==c)            return true;        return false;    }}class node//状态{    int a,b;    String s;//转移发生的同时,操作也就确定了    node last;//这个用来记录当前状态是由哪个转移过来的    node(int a,int b,String s,node last)    {        this.a=a;        this.b=b;        this.s=s;        this.last=last;    }}

peace&love

0 0
原创粉丝点击