非常可乐

题目描述:

    有一瓶可乐,体积为S,两个杯子,体积分别为N和M,它们三个之间可以互相倒可乐(都是没有刻度的,且S==M+N,101>S>0,N>0,M>0)。如果可以平分可乐,则输出倒可乐的最少次数,否则输出"NO"。

输入:

    三个整数:S 可乐的体积 N,M为两个杯子的体积,以"0 0 0"结束。

输出:

    如果可以平分可乐,则输出倒可乐的最少次数,否则输出"NO"

样例输入:

    7 4 3
    4 1 3
    0 0 0

样例输出:

    NO
    3

示例代码:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static int poorTimes(int S,int M,int N){
        Cola_volume root = new Cola_volume(S,0,0,0);
        if (S%2!=0){return -1;}//如果总体积不是2的倍数,肯定不能平分
        int average = S/2;
        boolean[][][] isVisited = new boolean[S+1][M+1][N+1];
        Queue<Cola_volume> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            Cola_volume visit_state = queue.peek();
            int s = visit_state.s;
            int m = visit_state.m;
            int n = visit_state.n;
            int t = visit_state.time;
            if (isVisited[s][m][n]){queue.poll();continue;}//!!!!注意一定要将队列头结点删除,否则会一直循环访问队头
            else {isVisited[s][m][n]=true;}
            if (s==average&&m==average){return t;}
            if (s==average&&n==average){return t;}
            if (m==average&&n==average){return t;}
            Cola_volume stom = StoM(s,S,m,M,n,N,t);
            if (!isVisited[stom.s][stom.m][stom.n]){queue.offer(stom);}
            Cola_volume ston = StoN(s,S,m,M,n,N,t);
            if (!isVisited[ston.s][ston.m][ston.n]){queue.offer(ston);}
            Cola_volume mtos = MtoS(s,S,m,M,n,N,t);
            if (!isVisited[mtos.s][mtos.m][mtos.n]){queue.offer(mtos);}
            Cola_volume mton = MtoN(s,S,m,M,n,N,t);
            if (!isVisited[mton.s][mton.m][mton.n]){queue.offer(mton);}
            Cola_volume ntos =NtoS(s,S,m,M,n,N,t);
            if (!isVisited[ntos.s][ntos.m][ntos.n]){queue.offer(ntos);}
            Cola_volume ntom =NtoM(s,S,m,M,n,N,t);
            if (!isVisited[ntom.s][ntos.m][ntos.n]){queue.offer(ntom);}
            queue.poll();
        }
        return -1;
    }

    public static Cola_volume StoM(int s,int S,int m,int M,int n,int N,int t){
        if (s<=M-m){
            m+=s;
            s=0;
        }
        else {
            s-=M-m;
            m = M;
        }
        return new Cola_volume(s,m,n,++t);
    }

    public static Cola_volume StoN(int s,int S,int m,int M,int n,int N,int t){
        if (s<=N-n){
            n+=s;
            s=0;
        }
        else {
            s-=N-n;
            n=N;
        }
        return new Cola_volume(s,m,n,++t);
    }
    public static Cola_volume MtoS(int s,int S,int m,int M,int n,int N,int t){
        if (m<=S-s){
            s+=m;
            m=0;
        }
        else {
            m-=S-s;
            s=S;
        }
        return new Cola_volume(s,m,n,++t);
    }
    public static Cola_volume MtoN(int s,int S,int m,int M,int n,int N,int t){
        if (m<=N-n){
            n+=m;
            m=0;
        }
        else {
            m-=N-n;
            n=N;
        }
        return new Cola_volume(s,m,n,++t);
    }
    public static Cola_volume NtoS(int s,int S,int m,int M,int n,int N,int t){
        if (n<=S-s){
            s+=n;
            n=0;
        }
        else {
            n-=S-s;
            s=S;
        }
        return new Cola_volume(s,m,n,++t);
    }
    public static Cola_volume NtoM(int s,int S,int m,int M,int n,int N,int t){
        if (n<=M-m){
            m+=n;
            n=0;
        }
        else {
            n-=M-m;
            m=M;
        }
        return new Cola_volume(s,m,n,++t);
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int S = scanner.nextInt();
            int M = scanner.nextInt();
            int N = scanner.nextInt();
            if (S==0&&M==0&&N==0){break;}
            int poor_time = poorTimes(S,M,N);
            if (poor_time==-1){System.out.println("NO");}
            else {System.out.println(poor_time);}
        }
    }
}


public class Cola_volume {
    int s,m,n;//杯子S,M,N中的可乐容量
    int time;//倒可乐的次数
    public Cola_volume(int s,int m,int n,int time){
        this.s = s;
        this.m = m;
        this.n = n;
        this.time = time;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值