POJ3414 - Pots(Java代码+注释)

发布于:2022-11-09 ⋅ 阅读:(16) ⋅ 点赞:(0) ⋅ 评论:(0)

 题目描述

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.

小明给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作

FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2);

DROP(i)        将第i个容器抽干

POUR(i,j)      将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)

现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程

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).

有且只有一行,包含3个数A,B,C(1<=A,B<=100,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’.

第一行包含一个数表示最小操作数K

随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”

Sample

Input

3 5 4

Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

 代码+注释

static class TaskA {
        int a,b,c;
        boolean[][] vis = new boolean[110][110];
        int f = 0;
        String[] ss = {"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
        static class A{
            int aa,bb;
            int step;
            String s;//路径统计
            A(int aa,int bb,int step,String s){
                this.aa = aa;
                this.bb = bb;
                this.step = step;
                this.s = s;
            }
        }
        void bfs() {
            A lmy = new A(0, 0,0,"0");
            Queue<A> q = new LinkedList();
            q.add(lmy);
            vis[0][0] = true;
            while (!q.isEmpty())  {
                A mo = new A(q.peek().aa, q.peek().bb,q.peek().step,q.poll().s);
                for (int i = 0; i < 6; i++) {
                    A next = new A(mo.aa, mo.bb, mo.step,mo.s);
                    if (i == 0) {//将第一个容器装满
                        if (mo.aa != a) {
                            next.aa = a;
                            next.step += 1;
                            next.s += "1";
                        }
                    } else if (i == 1) {//将第二个容器装满
                        if (mo.bb != b) {
                            next.bb = b;
                            next.step += 1;
                            next.s += "2";
                        }
                    } else if (i == 2) {//将第一个容器抽干
                        if (mo.aa != 0) {
                            next.aa = 0;
                            next.step += 1;
                            next.s += "3";
                        }
                    } else if (i == 3) {//将第二个容器抽干
                        if (mo.bb != 0) {
                            next.bb = 0;
                            next.step += 1;
                            next.s += "4";
                        }
                    } else if (i == 4) {//将第一个容器的水倒入第二个容器
                        if (mo.bb != b && mo.aa != 0) {
                            if (mo.aa > b - mo.bb) {
                                next.aa = mo.aa - (b - mo.bb);
                                next.bb = b;
                            } else {
                                next.aa = 0;
                                next.bb = mo.aa + mo.bb;
                            }
                            next.step += 1;
                            next.s += "5";
                        }
                    } else {//将第二个容器的水倒入第一个容器
                        if (mo.bb != 0 && mo.aa != a) {
                            if (mo.bb > a - mo.aa) {
                                next.bb = mo.bb - (a - mo.aa);
                                next.aa = a;
                            } else {
                                next.bb = 0;
                                next.aa = mo.bb + mo.aa;
                            }
                            next.step += 1;
                            next.s += "6";
                        }
                    }
                    if (next.aa == c || next.bb == c) {
                        System.out.println(next.step);
                        for (int k = 1; k <= next.step; k++) {//路径输出
                            System.out.println(ss[Integer.parseInt(next.s.substring(k,k+1))]);
                        }
                        f = 1;
                        return;
                    }
                    if(vis[next.aa][next.bb]){
                        continue;
                    }
                    vis[next.aa][next.bb] = true;//有这个情况进行标记
                    q.add(next);
                }
            }
        }
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            a = in.nextInt();
            b = in.nextInt();
            c = in.nextInt();
            bfs();
            if(f == 0){//不能满足恰好C升
                out.println("impossible");
            }
        }
    }

题目链接

Pots - POJ 3414 - Virtual Judge (vjudge.net)