Milk Pails

Milk Pails

题目描述

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
分析:枚举所有可能情况,bfs即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,x,y,k,mi,vis[maxn][maxn];
void bfs()
{
    queue<pii >p;p.push({0,0});vis[0][0]=1;
    while(!p.empty())
    {
        int u=p.front().fi,v=p.front().se;
        p.pop();
        mi=min(mi,abs(u+v-m));
        if(vis[u][v]==k+1)continue;
        if(u<x&&!vis[x][v])p.push({x,v}),vis[x][v]=vis[u][v]+1;
        if(v<y&&!vis[u][y])p.push({u,y}),vis[u][y]=vis[u][v]+1;
        if(u<x&&v)
        {
            int mi=min(x-u,v);
            if(!vis[u+mi][v-mi])p.push({u+mi,v-mi}),vis[u+mi][v-mi]=vis[u][v]+1;
        }
        if(v<y&&u)
        {
            int mi=min(y-v,u);
            if(!vis[u-mi][v+mi])p.push({u-mi,v+mi}),vis[u-mi][v+mi]=vis[u][v]+1;
        }
        if(u&&!vis[0][v])p.push({0,v}),vis[0][v]=vis[u][v]+1;
        if(v&&!vis[u][0])p.push({u,0}),vis[u][0]=vis[u][v]+1;
    }
}
int main()
{
    int i,j,t;
    scanf("%d%d%d%d",&x,&y,&k,&m);
    mi=m;
    bfs();
    printf("%d
",mi);
    //system ("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/5722883.html