Milk Pails(BFS)

Milk Pails

时间限制: 1 Sec  内存限制: 64 MB
提交: 16  解决: 4
[提交][状态][讨论版]

题目描述

Farmer John has received an order for exactly M units of milk (1≤M≤200) that he needs to fill right away. Unfortunately, his fancy milking machine has just become broken, and all he has are two milk pails of integer sizes X and Y (1≤X,Y≤100) with which he can measure milk. Both pails are initially empty. Using these two pails, he can perform up to K of the following types of operations (1≤K≤100):

- He can fill either pail completely to the top.

- He can empty either pail.

- He can pour the contents of one pail into the other, stopping when the former becomes empty or the latter becomes full (whichever of these happens first).

Although FJ realizes he may not be able to end up with exactly M total units of milk in the two pails, please help him compute the minimum amount of error between M and the total amount of milk in the two pails. That is, please compute the minimum value of |M−M′| such that FJ can construct M′ units of milk collectively between the two pails.

输入

The first, and only line of input, contains X, Y, K, and M.

输出

Output the smallest distance from M to an amount of milk FJ can produce.

样例输入

14 50 2 32

样例输出

18
【分析】这跟昨天的 母亲的牛奶那题特别像,两个瓶,一开始都为空,有三种操作,将一个瓶灌满,讲一个瓶清空,然后就是将一个瓶网另一个瓶倒,直到到完
或者到满。将昨天的代码稍微改了一下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=210;
int n,dp[N],len;
int w[N][N];
int g[3];
int a,b,c;
int X,Y,K,M;
set<int>p;
int minn=inf;
struct man {
    int x,y;
    int step;
};

void bfs() {
    w[0][0]=1;
    queue<man>q;
    man s;
    s.x=0;
    s.y=0;
    s.step=0;
    q.push(s);
    while(!q.empty()) {
        man t=q.front();
        q.pop();
        minn=min(minn,abs((t.x+t.y)-M));
        if(t.step==K)continue;
        int f[3];
         f[0]=t.x;
            f[1]=t.y;
         if(!w[f[0]][0]){man kk;kk.x=f[0];kk.y=0;kk.step=t.step+1;w[f[0]][0]=1;q.push(kk);}
         if(!w[f[0]][g[1]]){man kk;kk.x=f[0];kk.y=g[1];kk.step=t.step+1;w[f[0]][g[1]]=1;q.push(kk);}
         if(!w[0][f[1]]){man kk;kk.x=0;kk.y=f[1];kk.step=t.step+1;w[0][f[1]]=1;q.push(kk);}
         if(!w[g[0]][f[1]]){man kk;kk.x=g[0];kk.y=f[1];kk.step=t.step+1;w[g[0]][f[1]]=1;q.push(kk);}
        for(int i=0; i<2; i++) {
            f[0]=t.x;
            f[1]=t.y;
            if(f[i]==0)continue;
            for(int j=0; j<2; j++) {
                f[0]=t.x;
                f[1]=t.y;
                man k;
                if(i==j||f[j]==g[j])continue;
                if(f[i]+f[j]>=g[j]) {
                    f[i]=f[i]-(g[j]-f[j]);
                    f[j]=g[j];
                    // printf("@%d %d
",f[i],f[j]);
                } else if(f[i]+f[j]<g[j]) {
                    f[j]+=f[i];
                    f[i]=0;

                }
                k.x=f[0];
                k.y=f[1];
                // printf("!!!%d %d %d
",k.x,k.y,k.z);
                if(w[k.x][k.y]==0) {
                        k.step=t.step+1;
                    q.push(k);
                    w[k.x][k.y]=1;
                }
            }
        }
    }
}

int main() {
    memset(w,0,sizeof(w));

    cin>>X>>Y>>K>>M;
    g[0]=X;
    g[1]=Y;
    bfs();
   cout<<minn<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jianrenfang/p/5722876.html