zoukankan      html  css  js  c++  java
  • 1.4.4 Mother's Milk

    Mother's Milk

    Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.

    Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.

    PROGRAM NAME: milk3

    INPUT FORMAT

    A single line with the three integers A, B, and C.

    SAMPLE INPUT (file milk3.in)

    8 9 10
    

    OUTPUT FORMAT

    A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.

    SAMPLE OUTPUT (file milk3.out)

    1 2 8 9 10
    

    SAMPLE INPUT (file milk3.in)

    2 5 10
    

    SAMPLE OUTPUT (file milk3.out)

    5 6 7 8 9 10

    {
    ID:makeeca1
    PROG: milk3
    LANG: PASCAL
    }
    program milk3;
    var a,b,c,x,y,z,i:longint;
        can:array[0..20]of boolean;
        f:array[0..20,0..20]of boolean;
    function max(xx,yy:longint):longint;
    begin if xx>yy then exit(xx)else exit(yy);end;
    function min(xx,yy:longint):longint;
    begin if xx>yy then exit(yy)else exit(xx);end;
    procedure dfs(x,y,z:longint);
    begin
      if f[x,y]then exit;
      f[x,y]:=true;
      if (x=0) then can[z]:=true;
      if (x>0)and(y<b) then dfs(max(0,x+y-b),min(b,x+y),z);
      if (x>0)and(z<c) then dfs(max(0,x+z-c),y,min(c,x+z));
      if (y>0)and(x<a) then dfs(min(a,y+x),max(0,y+x-a),z);
      if (y>0)and(z<c) then dfs(x,max(0,y+z-c),min(c,y+z));
      if (z>0)and(x<a) then dfs(min(a,z+x),y,max(0,z+x-a));
      if (z>0)and(y<b) then dfs(x,min(b,z+y),max(0,z+y-b));
    end;
    
    begin
      assign(input,'milk3.in');reset(input);
      assign(output,'milk3.out');rewrite(output);
      readln(a,b,c);
      fillchar(can,sizeof(can),0);
      fillchar(f,sizeof(f),0);
      dfs(0,0,c);
      for i:=0 to c-1 do if can[i]then write(i,' ');
      writeln(c);
      close(input);close(output);
    end.
  • 相关阅读:
    POJ-2528 Mayor's posters 线段树 离散化
    POJ 3468 A Simple Problem with Integers 线段树区间修改
    CodeForces
    POJ3208 Apocalypse Someday 数位DP经典绝世好题
    CodeForces114 Double Happiness 数论 二次筛法 bitset的应用
    P3146 [USACO16OPEN]248 G 区间DP 暴力DP
    【Python学习】requests短连接
    【Windows学习】免登录和自动登录
    【Linux学习】expect远程下载和上传样例
    【Linux学习】软件包管理-wget,rpm,yum,apt-get
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274519.html
Copyright © 2011-2022 走看看