POJ3414(BFS)

POJ3414(BFS)


Pots

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 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)
题意

​ 给出两个壶的容量A和B, 一个目标水量C,对A、B可以有3种操作,求最少经过几步操作能够在某个壶中得到目标水量C。输入A、B和C,输入最少操作数和操作过程。不可能则输出impossible

  • FILL(i),将i水壶的水填满
  • DROP(i),将水壶i中的水全部倒掉
  • POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了
思路

​ BFS搜索两个水壶A、B的状态,初始状态为<0,0>,搜索的方向是对应的六种操作。最终要输出操作的过程,即需要记录搜索的路径,需要在每个节点中记录父节点的信息。

代码
public class Pots {

    int a;
    int b;
    int c;
    List<Node> queue = new ArrayList<Node>();//队列
    int[][] visit = new int[200][200];//记录某种状态是否已经达成过

    int lastStepIndex;//最后一步的在队列中的下标

    boolean flag = false;//是否成功

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Pots pots = new Pots();
        pots.a = sc.nextInt();
        pots.b = sc.nextInt();
        pots.c = sc.nextInt();
        pots.bfs();
        if (pots.flag) {
            List<Integer> result = new ArrayList<Integer>();
            int index = pots.lastStepIndex;
            while (true) {
                if (index == -1) {
                    break;
                }
                result.add(pots.queue.get(index).getLayer());
                index = pots.queue.get(index).pre;
            }
            System.out.println(result.size() - 1);
            for (int i = result.size() - 1; i >= 0; i--) {
                switch (result.get(i)) {
                    case 0:
                        System.out.println("FILL(1)");
                        break;
                    case 1:
                        System.out.println("FILL(2)");
                        break;
                    case 2:
                        System.out.println("DROP(1)");
                        break;
                    case 3:
                        System.out.println("DROP(2)");
                        break;
                    case 4:
                        System.out.println("POUR(1,2)");
                        break;
                    case 5:
                        System.out.println("POUR(2,1)");
                        break;
                }
            }
        } else {
            System.out.println("impossible");
        }
    }

    public void bfs() {
        Node node = new Node();
        node.setA(0);//初始状态a
        node.setB(0);//初始状态b
        node.setLayer(-1);
        node.setPre(-1);
        queue.add(node);
        int rear = 1;//队尾
        int front = 0;//队头
        while (front < rear) {
            Node now_Node = queue.get(front);
            front++;//出队列
            if (now_Node.getA() == c || now_Node.getB() == c) {
                flag = true;
                lastStepIndex = front - 1;
            }
            for (int i = 0; i < 6; i++) {//遍历6中操作
                Node next = new Node();
                if (i == 0) {//fill(1)
                    next.setA(a);
                    next.setB(now_Node.getB());
                } else if (i == 1) {//fill(2)
                    next.setA(now_Node.getA());
                    next.setB(b);
                } else if (i == 2) {//drop(1)
                    next.setA(0);
                    next.setB(now_Node.getB());
                } else if (i == 3) {//drop(2)
                    next.setA(now_Node.getA());
                    next.setB(0);
                } else if (i == 4) {//pour(1,2)
                    if (now_Node.getA() + now_Node.getB() <= b) {//把A水壶的水倒入B,不能装满B
                        next.setA(0);
                        next.setB(now_Node.getA() + now_Node.getB());
                    } else {//把A水壶的水倒入B,能装满B
                        next.setA(now_Node.getA() + now_Node.getB() - b);
                        next.setB(b);
                    }
                } else if (i == 5) {//pour(2,1)
                    if (now_Node.getA() + now_Node.getB() <= a) {
                        next.setA(now_Node.getA() + now_Node.getB());
                        next.setB(0);
                    } else {
                        next.setA(a);
                        next.setB(now_Node.getA() + now_Node.getB() - a);
                    }
                }
                next.setLayer(i);
                next.setPre(front - 1);
                if (visit[next.getA()][next.getB()] == 0) {//当前状态没有进入过队列
                    visit[next.getA()][next.getB()] = 1;//标记次状态已经进入队列
                    queue.add(next);//进入队列
                    rear++;//队尾延长

                    if (next.getA() == c || next.getB() == c) {//达成目标
                        flag = true;
                        lastStepIndex = rear - 1;
                        return;
                    }
                }
            }
        }
    }

    static class Node {
        private int layer;//表示第几种操作。
        private int a;//a水壶的水量
        private int b;//b水壶的水量
        private int pre;//上一步操作的index

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }

        public int getB() {
            return b;
        }

        public void setB(int b) {
            this.b = b;
        }

        public int getLayer() {
            return layer;
        }

        public void setPre(int pre) {
            this.pre = pre;
        }

        public int getPre() {
            return pre;
        }

        public void setLayer(int layer) {
            this.layer = layer;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值